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