需求: 对数据库中的不断抓取的文章进行缓存,因此需要定时访问数据,写入缓存中
在捕获到的异常日志发现错误: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); } } } } }
时间: 2024-11-04 14:46:15