日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis

發布時間:2024/10/12 C# 104 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求: ?對數據庫中的不斷抓取的文章進行緩存,因此需要定時訪問數據,寫入緩存中

在捕獲到的異常日志發現錯誤: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);}}}}}

?

轉載于:https://www.cnblogs.com/kongkonglonglong/p/7449175.html

總結

以上是生活随笔為你收集整理的Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。