CSRedisCore 在net core中的使用


背景:与net core配套的StackExchange.Redis客户端总是间歇性的发生timeout异常。 由complexer单例对象创建的IDatabase对象,在产生Timeout异常后会导致这个complexer单例对象超时,即由其创建的新的IDatabase对象也会继续Timeout,实际使用过程中,由于网络各种原因,出现Timeout异常难免,但是无法恢复确实很麻烦。测试complexer对象无法dispose,close后重建。暂时没有好的解决方案,所以转战CSRedis。

 使用方法:

             1.网上git源码或者动态链接库。

             2.项目引用

             3.具体使用方式如下:

                 (1)创建读写连接客户端(单例模式)

 1   public class RedisBase
 2     {
 3         protected static string _connect_str_write;
 4         protected static string _connect_str_read;
 5 
 6 
 7         protected static CSRedisClient  rds_write=null;
 8         protected static CSRedisClient rds_read = null;
 9 
10         private static object _lockObj_write = new object();
11         private static object _lockObj_read = new object();
12 
13         public static CSRedisClient GetWriteClient()
14         {
15             if (string.IsNullOrEmpty(_connect_str_write))
16             {
17                 Console.WriteLine("Write Connectstring is not set");
18             }
19             if (rds_write == null)
20             {
21                 lock (_lockObj_write)
22                 {
23                     if (rds_write == null)
24                     {
25                         rds_write = new CSRedisClient($"{_connect_str_write},poolsize=50,ssl=false,writeBuffer=10240");
26                         RedisHelper.Initialization(rds_write);
27                     }
28                 }
29             }
30             return rds_write;
31     }
32         public static CSRedisClient GetReadClient()
33         {
34             if (string.IsNullOrEmpty(_connect_str_read))
35             {
36                 Console.WriteLine("Read Connectstring is not set");
37             }
38             if (rds_read == null)
39             {
40                 lock (_lockObj_read)
41                 {
42                     if (rds_read == null)
43                     {
44                         rds_read = new CSRedisClient($"{_connect_str_read},poolsize=50,ssl=false,writeBuffer=10240");
45                     }
46                 }
47             }
48             return rds_read;
49         }
50     }

          (2)调用方式:

           

            if (rds_write == null)
                   rds_write = GetWriteClient();
            rds_write.Del(key);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM