现在流行的redis连接客户端有StackExchange.Redis和ServiceStack.Redis,为什么选择csredis而不是这两个?
- .net 最有名望的 ServiceStack.Redis 早已沦为商业用途,在 .NETCore 中使用只能充值;
- 后来居上的 StackExchange.Redis 虽然能用,但线上各种 Timeout 错误把人坑到没脾气,两年多两年多两年多都不解决,最近发布的 2.0 版本不知道是否彻底解决了底层。
- csredis支持.net40/.net45/.netstandard2.0,基本上满足了常见运行平台,而上面两个基本已经放弃.net40了。
- csredis所有方法名与redis-cli保持一持,很容易上手!!!
环境:
- redis6.0.6
- window 10
- vs2019
- csredis3.6.5(https://github.com/2881099/csredis)
- .net4/.net 4.5/.netcore 3.1
csredis 源码地址: https://github.com/2881099/csredis
1.在.net项目中引入CSRedisCore
包安装命令:
1
|
Install-Package CSRedisCore -Version 3.6.5 |
2.使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main( string [] args) { //根据连接信息构造客户端对象 var redis = new CSRedis.CSRedisClient( "192.168.3.42:6500,password=123456,defaultDatabase=0,poolsize=500,ssl=false,writeBuffer=10240,prefix=test_" ); //redis中的string存取 redis.Set( "name" , "小明" ); var name= redis.Get( "name" ); Console.WriteLine($ "name={name}" ); redis.Set( "birth" , DateTime.Now); var birth = redis.Get<DateTime>( "birth" ); Console.WriteLine($ "birth={birth}" ); redis.Set( "info" , new {sex= "female" ,age= "20" }); var info = redis.Get< object >( "info" ); Console.WriteLine($ "info={info}" ); Console.WriteLine( "ok" ); Console.ReadLine(); } } } |
3.高级玩法:发布订阅
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//普通订阅 rds.Subscribe( ( "chan1" , msg => Console.WriteLine(msg.Body)), ( "chan2" , msg => Console.WriteLine(msg.Body))); //模式订阅(通配符) rds.PSubscribe( new [] { "test*" , "*test001" , "test*002" }, msg => { Console.WriteLine($ "PSUB {msg.MessageId}:{msg.Body} {msg.Pattern}: chan:{msg.Channel}" ); }); //模式订阅已经解决的难题: //1、分区的节点匹配规则,导致通配符最大可能匹配全部节点,所以全部节点都要订阅 //2、本组 "test*", "*test001", "test*002" 订阅全部节点时,需要解决同一条消息不可执行多次 //发布 rds.Publish( "chan1" , "123123123" ); //无论是分区或普通模式,rds.Publish 都可以正常通信 |
4.高级玩法:缓存壳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//不加缓存的时候,要从数据库查询 var t1 = Test.Select.WhereId(1).ToOne(); //一般的缓存代码,如不封装还挺繁琐的 var cacheValue = rds.Get( "test1" ); if (! string .IsNullOrEmpty(cacheValue)) { try { return JsonConvert.DeserializeObject(cacheValue); } catch { //出错时删除key rds.Remove( "test1" ); throw ; } } var t1 = Test.Select.WhereId(1).ToOne(); rds.Set( "test1" , JsonConvert.SerializeObject(t1), 10); //缓存10秒 //使用缓存壳效果同上,以下示例使用 string 和 hash 缓存数据 var t1 = rds.CacheShell( "test1" , 10, () => Test.Select.WhereId(1).ToOne()); var t2 = rds.CacheShell( "test" , "1" , 10, () => Test.Select.WhereId(1).ToOne()); var t3 = rds.CacheShell( "test" , new [] { "1" , "2" }, 10, notCacheFields => new [] { ( "1" , Test.Select.WhereId(1).ToOne()), ( "2" , Test.Select.WhereId(2).ToOne()) }); |
5.高级玩法:管道
使用管道模式,打包多条命令一起执行,从而提高性能。
1
2
3
4
5
|
var ret1 = rds.StartPipe().Set( "a" , "1" ).Get( "a" ).EndPipe(); var ret2 = rds.StartPipe(p => p.Set( "a" , "1" ).Get( "a" )); var ret3 = rds.StartPipe().Get( "b" ).Get( "a" ).Get( "a" ).EndPipe(); //与 rds.MGet("b", "a", "a") 性能相比,经测试差之毫厘 |
6.高级玩法:多数据库
1
2
3
4
5
6
|
var connectionString = "127.0.0.1:6379,password=123,poolsize=10,ssl=false,writeBuffer=10240,prefix=key前辍" ; var redis = new CSRedisClient[14]; //定义成单例 for (var a = 0; a< redis.Length; a++) redis[a] = new CSRedisClient(connectionString + "; defualtDatabase=" + a); //访问数据库1的数据 redis[1].Get( "test1" ); |
7.性能比拼
以上就是c#使用csredis操作redis的示例的详细内容,更多关于c# csredis操作redis的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/tuyile006/p/14051569.html