We all know that using the system default session will have such a problem if the user too many words will automatically die, and can not support the distribution and cluster.
This series of blogs mainly explains how to solve the user's excessive session auto-extinction, and distributed cluster
Examples of Use
session["test" Woodpecker
Completely does not change the system's use style, can upgrade the system directly;
Here we mainly use the Httpruntime.cache and memcache. I hope the reader will follow my train of thought and design a custom session step-by-step.
First of all, we think that since there are two data access tools, there must be an interface to decouple the use of his class, so that it is easy to switch the data storage tool without affecting the class that uses it
The interface is as follows:
PublicInterfaceICache {///<summary>///Data is added to the cache and the expiration time of the global configuration is used///</summary>///<param name= "Key" >Key</param>///<param name= "obj" >Data</param>void Put (String key,ObjectOBJ);///<summary>///Data is added to the cache and specifies the expiration time (in minutes)///</summary>///<param name= "Key" >Key</param>///<param name= "obj" >Data</param>///<param name= "Expireminutes" >Expiry time</param>void Put (String key,Object obj,IntExpireminutes);///<summary>///Take out the cached data///</summary> / <param name= "key" ></param> ///<returns></returns> object Get (string< Span style= "color: #000000;" > key); ///<summary> // / manually delete cached data ///</ summary> ///<param name= "key" ></param > void Delete (string key); }
Now let's implement these two data storage tool classes
One is the system comes with the Httpruntime.cache
PublicClassRuntimecache:icache {ReadOnlystatic System.Web.Caching.Cache Httpruntimecache =System.Web.HttpRuntime.Cache;ReadOnlyStaticint _expireminutes =20;//ConfigConstants.ConfigManager.Config.Cache_ExpireMinutes; Configure default number of seconds obsoletePublicvoid Put (String key,Objectobj) {httpruntimecache.insert (key,obj);}public void Put (string Key, object obj, intnull public object Get ( key" {returnpublic void Delete ( String key) {Httpruntimecache.remove (key);}}
Above this tool I want you to know, if you do not know can Baidu Httpruntime.cache to learn, here I will not explain (in fact, there are a lot of design ideas can learn)
The other one is Memcache,redis, and I'm going to add it back.
PublicClassMemcachecache:icache {PrivateStaticReadOnlyMemcachedclient m_memcachedclient;PrivateStaticString m_memcachestr = ConfigConstants.FrameConfigManager.Config.MemcacheStr??"127.0.0.1:11211";StaticMemcachecache () {string[] Servers = M_memcachestr.split (‘,‘);//Try{//Initialize Pool Sockiopool swimming =Sockiopool.getinstance (); Pool. Setservers (servers); Pool. Initconnections =3; Pool. Minconnections =3; Pool. MaxConnections =5; Pool. Socketconnecttimeout =1000; Pool. Sockettimeout =3000; Pool. Maintenancesleep =30; Pool. Failover =True; Pool. Nagle =False; Pool. Initialize (); M_memcachedclient =NewMemcached.ClientLibrary.MemcachedClient (); M_memcachedclient.enablecompression =False; }Catch(Exception ex) {int i =0; } }Publicvoid Put (String key,object obj) {m_memcachedclient.set (key, obj);} public void Put (string Key, object obj, intpublic object Get (string key) {return M_memcachedclient.get (key);} public void Delete (string key) {M_memcachedclient.delete (key);} }
Memcache detailed configuration, you can find other information to learn
So far, it feels a bit like our factory pattern, yes, it's going to be a factory model.
We're going to create a new class called
///<summary>///Cache Manager///</summary>PublicClass cachemanager:singleton<cachemanager>, ICache {#region Private variablesPrivateStaticString _cacheprovider = ConfigConstants.FrameConfigManager.Config.Cache_Provider??"Runtimecache";PrivateICache _icache;#endregion#region Construction Method///<summary>///Class construction method, which does not support the creation of its instance object///</summary>StaticCacheManager () {}PrivateCacheManager () {Switch(_cacheprovider.tolower ()) {Case"Runtimecache": _icache =NewRuntimecache ();Break;Case"Memcachecache": _icache =NewMemcachecache ();Break;Default:ThrowNew ArgumentException ("Cache providers only support Runtimecache and Rediscache"); } }#endregionPublicvoid Put (string key, object obj) {_icache.put (key, obj);} public void Put (string key, object obj, int expireminutes) {_icache.put (key,obj,expireminutes) ; } Public object Get (string key) { return _icache.get (key);} Public void Delete (string key) {_icache.delete (key);}}
I think this class everyone can see understand, only this singleton<cachemanager> everyone may be a little can not understand, because this is my own sub-assembly of the Singleton mode, as long as inherit him, this class is simple interest, I will next time blog to understand how to split. Here we realize the use and provide decoupling, I want to tell you that the design pattern will be based on the amount of your code to follow the progressive, do not deliberately look at design patterns
We've got these preparations, and the next one is about customizing the session.
ASP. NET MVC Custom session (i),