需求: 對數據庫中的不斷抓取的文章進行緩存,因此需要定時訪問數據,寫入緩存中
在捕獲到的異常日志發現錯誤:Unable to Connect: sPort: 0
使用的訪問方式是線程池的方式:PooledRedisClientManager
經過測試發現在並發訪問redis服務的情況下出現該異常的概率比較高,
解決方法
第一點:要使用using(據說訪問效率在高並發的時候會有影響,簡單的測試過了確實是這樣,不過現在的業務達不到高並發量,速度還是很快滴)
using (IRedisClient redisClient = Instance.GetClient()) { T t = redisClient.Get<T>(key); if (t == null && func != null) { t = func(); redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds)); } return t; }
第二點:設置ConnectTimeout屬性(猜測單位應該是毫秒),要設置一個比較大的值,我設置為:1000 * 60 * 20 ,沒錯是20分鍾,我發現設置小了還是不管用
做好這兩點,經測試發現每秒鍾200個並發量 毫無壓力
附上封裝的一個幫助類:
/// <summary> /// 數據緩存 /// </summary> public class RedisHelper { public static PooledRedisClientManager instance; static RedisHelper() { } public static PooledRedisClientManager Instance { get { return instance; } }public static void InitClient(string redis_ip, int redis_port, string redis_pass) { instance = new PooledRedisClientManager(10000, 10, new string[] { string.Format("{0}@{1}:{2}", redis_pass, redis_ip, redis_port) }) { ConnectTimeout = 1000 * 60 * 20 }; } public static T Get<T>(string key, int seconds, Func<T> func) { using (IRedisClient redisClient = Instance.GetClient()) { T t = redisClient.Get<T>(key); if (t == null && func != null) { t = func(); redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds)); } return t; } } public static T Get<T>(string key) { using (IRedisClient redisClient = Instance.GetReadOnlyClient()) { T t = redisClient.Get<T>(key); return t; } } public static Boolean Set<T>(string key, int seconds, T t) { using (IRedisClient redisClient = Instance.GetClient()) { return redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds)); } } public static void ClearKey(string key) { using (IRedisClient redisClient = Instance.GetClient()) { IEnumerable<string> keys = redisClient.GetAllKeys(); ; foreach (var item in keys) { if (item.Contains(key)) { redisClient.Remove(item); } } } } }