1, these two days to study Redis to engage in distributed session problem, Online find the information are used Servicestack.redis to achieve, but in the performance test found the latest V4 version has a limit of up to 6,000 times per child, because the official website began commercialization to charge, good pit dad said, fortunately I got a performance test in the early days, otherwise the problem after the online trouble. After looking for a nservicekit.redis (as if Servicestack.redis's V3 version) to replace the v4 of the paid version.
2, the solution is the Redis+cookie way to record the user login status
Cookie: Store the user's ID, which is encrypted, and the background can be decrypted by the key.
Redis:key/value mode storage, key storage such as: user_1. Value holds the user entity object.
3, first install a redis,windows version of the local test, later on-line replacement of the Linux system redis replace the IP can be.
4. Add a Session Management class
Public classSessionhelper {Private Const intSecondstimeout = -* -;//default expiration Time 20 minutes per second PublicRedishelper Redis =NewRedishelper (false); PublicLoginuserinfo This[stringKey] { Get { stringWebcookie =Webhelper.getcookie (key); if(Webcookie = ="") { return NULL; } Key= key +"_"+Securehelper.aesdecrypt (Webcookie); //How many seconds before the expiration time LongL =Redis.ttl (key); if(L >=0) {Redis.expire (key, secondstimeout); } returnRedis.get<loginuserinfo>(key); } Set{setsession (key, value); } } Public voidSetsession (stringkey, Loginuserinfo value) { if(string. Isnullorwhitespace (key)) {Throw NewException ("Key is Null or epmty"); } webhelper.setcookie (Key, Securehelper.aesencrypt (value.id. ToString ())); Key= key +"_"+value.id; Redis.set<LoginUserInfo>(key, value, secondstimeout); } /// <summary> ///Remove Session/// </summary> /// <param name= "key" ></param> /// <returns></returns> Public BOOLRemove (stringkey) { varrs = redis.remove (key +"_"+Securehelper.aesdecrypt (Webhelper.getcookie (key))); Webhelper.deletecookie (key); returnrs; } }
5. Redis Operation class
Public classredishelper:idisposable { PrivateRedisclient Redis =NewRedisclient ("127.0.0.1",6379); //Cache PoolPooledredisclientmanager PRCM =NewPooledredisclientmanager (); //Default cache expiration time unit seconds Public intSecondstimeout = -* -; /// <summary> ///Buffer Pool/// </summary> /// <param name= "readwritehosts" ></param> /// <param name= "readonlyhosts" ></param> /// <returns></returns> Public StaticPooledredisclientmanager Createmanager (string[] readwritehosts,string[] readonlyhosts) { return NewPooledredisclientmanager (readwritehosts, readonlyhosts,NewRedisclientmanagerconfig {maxwritepoolsize= Readwritehosts.length *5, Maxreadpoolsize= Readonlyhosts.length *5, AutoStart=true, }); } /// <summary> ///constructor Function/// </summary> /// <param name= "Openpooledredis" >whether to open the buffer pool</param> PublicRedishelper (BOOLOpenpooledredis =false) { if(Openpooledredis) {PRCM= Createmanager (New string[] {"127.0.0.1:6379"},New string[] {"127.0.0.1:6379" }); Redis= PRCM. Getclient () asredisclient; } } /// <summary> ///How many seconds before the expiration time/// </summary> /// <param name= "key" ></param> /// <returns></returns> Public LongTTL (stringkey) { returnRedis.ttl (key); } /// <summary> ///Set Expiration Time/// </summary> /// <param name= "key" ></param> /// <param name= "Timeout" ></param> Public voidExpire (stringKeyintTimeout =0) { if(Timeout >=0) { if(Timeout >0) {Secondstimeout=timeout; } redis.expire (key, secondstimeout); } } #regionKey/value Storage/// <summary> ///setting up the cache/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "key" >Cache Build</param> /// <param name= "T" >Cached Values</param> /// <param name= "Timeout" >expiration time, in seconds,-1: No period, 0: Default Expiration Time</param> /// <returns></returns> Public BOOLSet<t> (stringKey, T T,intTimeout =0) {Redis.set<T>(key, T); if(Timeout >=0) { if(Timeout >0) {Secondstimeout=timeout; } redis.expire (key, secondstimeout); } return true; } /// <summary> ///Get/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "key" ></param> /// <returns></returns> PublicT get<t> (stringkey) { returnRedis.get<t>(key); } /// <summary> ///Delete/// </summary> /// <param name= "key" ></param> /// <returns></returns> Public BOOLRemove (stringkey) { returnRedis.remove (key); } #endregion //Freeing Resources Public voidDispose () {if(Redis! =NULL) {redis.dispose (); Redis=NULL; } GC. Collect (); } }
ASP. NET MVC implements distributed cluster sharing session with Redis.