Unable to connect:sport:0 C # Servicestack.redis access to Redis

Source: Internet
Author: User

Requirements: Caching of continuously crawled articles in the database, which requires timed access to the data, written to the cache

Error found in the caught Exception log: Unable to connect:sport:0

The access method used is the thread pool way: Pooledredisclientmanager

It has been tested that the probability of this exception occurring in the case of concurrent access to the Redis service is high,

Workaround

1th: To use using (it is said that access efficiency in high concurrency will have an impact, the simple test is true, but now the business does not reach high concurrency, speed or fast drop)

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;            }

2nd: Set the ConnectTimeout property (guess unit should be milliseconds), to set a larger value, I set to: 1000 * 60 * 20, yes 20 minutes, I found that the setting is small or not work

Do these two points, tested to find 200 concurrency per second without pressure

Attach a helper class for the package:

/// <summary>    ///Data Caching/// </summary>     Public classRedishelper {         Public StaticPooledredisclientmanager instance; StaticRedishelper () {} Public StaticPooledredisclientmanager Instance {Get            {                returninstance; }        } Public Static voidInitclient (stringREDIS_IP,intRedis_port,stringRedis_pass) {Instance=NewPooledredisclientmanager (10000,                                           Ten,                                           New string[] {string. Format ("{0}@{1}:{2}", Redis_pass, Redis_ip, Redis_port)}) {ConnectTimeout= +* -* -            }; }                 Public StaticT get<t> (stringKeyintSeconds, 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)); }                returnT; }        }         Public StaticT get<t> (stringkey) {            using(Iredisclient redisclient =instance.getreadonlyclient ()) {T T= redisclient.get<t>(key); returnT; }        }         Public StaticBoolean set<t> (stringKeyintseconds, T T) {            using(Iredisclient redisclient =instance.getclient ()) {                returnRedisclient.set<t>(key, T, DateTime.Now.AddSeconds (seconds)); }        }         Public Static voidClearkey (stringkey) {            using(Iredisclient redisclient =instance.getclient ()) {IEnumerable<string> keys =Redisclient.getallkeys (); foreach(varIteminchkeys) {                    if(item.                    Contains (key)) {Redisclient.remove (item); }                }            }        }    }

Unable to connect:sport:0 C # Servicestack.redis access to Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.