C#进行Redis操作

由于一些程序是用.net来写的,刚好要访问以前的redis库,于是就找一些redis的.net客户端。


在redis主页上,提供了好多个利用.net封装redis的库:

csredis       Async (and sync) client for Redis and Sentinel
Nhiredis     A lightweight wrapper around the C client hiredis.
redis-sharp        
redisboost     Thread-safe async Redis client. Offers high performance and simple api
ServiceStack.Redis   This is a fork and improvement of the original C# client written by Miguel De Icaza.
Sider       Minimalistic client for C#/.NET 4.0
StackExchange.Redis   This .NET client was developed by Stack Exchange for very high performance needs (replacement to the earlier BookSleeve).
TeamDev Redis Client       Redis Client is based on redis-sharp for the basic communication functions, but it offers some differences.

看了下各个的比较,最终选择排名第一的csredis。一看例子,非常简单,就它了。


下载后编译一测试,果然代码非常简洁易读。

如进行如下代码的执行:

 private void SetRediskey()
        {
            using (var redis = new RedisClient("localhost"))
            {
                string ping = redis.Ping();
                string echo = redis.Echo("hello world");
                DateTime time = redis.Time();
                Console.WriteLine(ping);
                Console.WriteLine(echo);
                Console.WriteLine(time);


                redis.StartPipeTransaction();
                redis.Set("key", "value");
                redis.Set("key2", "value2");
                object[] result2 = redis.EndPipe(); // transaction is EXEC'd automatically if DISCARD was not called first
                foreach (object c in result2)
                    Console.WriteLine("redis result:" + c.ToString());
            }
        }


输出结果为:

PONG
hello world
04/16/2016 02:31:31
redis result:OK
redis result:OK


而在redis端进行查询,效果如下:非常简单。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值