Unable to Connect: sPort: 0 C# ServiceStack.Redis 訪問 redis

來源:互聯網
上載者:User

標籤:sharp   form   har   services   shel   協助   資料庫   nec   pass   

需求:  對資料庫中的不斷抓取的文章進行緩衝,因此需要定時訪問資料,寫入緩衝中

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

 

Unable to Connect: sPort: 0 C# ServiceStack.Redis 訪問 redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.