背景:與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);