Redis Server deployment: Is that clear??
Code Usage Section
Assemblies that need to be referenced: StackExchange.Redis.StrongName.dll
Interface for Redis connections
Public Interface iredisconnectionwrapper:idisposable { idatabase Database (intnull); IServer Server (EndPoint EndPoint); Endpoint[] Getendpoints (); void FLUSHDB (intnull); }
Cache specific Interfaces
Public Interfaceicachemanager:idisposable {/// <summary> ///get values by key/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "key" ></param> /// <returns></returns>T get<t> (stringkey); /// <summary> ///Add a key-value pair cache/// </summary> /// <param name= "key" ></param> /// <param name= "Data" ></param> /// <param name= "CacheTime" ></param> voidSet (stringKeyObjectDataintcacheTime); /// <summary> ///gets whether the value corresponding to the key exists/// </summary> /// <param name= "key" ></param> /// <returns></returns> BOOLIsSet (stringkey); /// <summary> ///removing a key-value pair cache/// </summary> /// <param name= "key" ></param> voidRemove (stringkey); voidRemovebypattern (stringpattern); /// <summary> ///empty all cached data/// </summary> voidClear (); }
View CodeRealize
Public Partial classRediscachemanager:icachemanager {#regionFiledsPrivate ReadOnlyIredisconnectionwrapper _connectionwrapper; Private ReadOnlyidatabase _db; Private ReadOnlyIcachemanager _perrequestcachemanager; #endregion #regionCtor PublicRediscachemanager (calabashconfig config, Iredisconnectionwrapper connectionwrapper) {if(string.isnullorempty (config. rediscachingconnectionstring))Throw NewException ("Redis Connection string is empty"); This. _connectionwrapper =Connectionwrapper; This. _db =_connectionwrapper.database (); This. _perrequestcachemanager = enginecontext.current.resolve<icachemanager>(); } #endregion #regionUtilities/// <summary> ///Serialization of/// </summary> /// <param name= "item" ></param> /// <returns></returns> protected Virtual byte[] Serialize (ObjectItem) { varJsonstring =jsonconvert.serializeobject (item); returnEncoding.UTF8.GetBytes (jsonstring); } /// <summary> ///deserialization/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "Serializedobject" ></param> /// <returns></returns> protected VirtualT deserialize<t> (byte[] serializedobject) { if(Serializedobject = =NULL) return default(T); varJsonstring =Encoding.UTF8.GetString (Serializedobject); returnJsonconvert.deserializeobject<t>(jsonstring); } #endregion #regionMethods PublicT get<t> (stringkey) { if(_perrequestcachemanager.isset (key))return_perrequestcachemanager.get<t>(key); varRValue =_db. Stringget (key); if(!rvalue.hasvalue)return default(T); varresult = Deserialize<t>(RValue); _perrequestcachemanager.set (Key,result,0); returnresult; } Public voidSet (stringKeyObjectDataintcacheTime) { if(data==NULL) return; varEntrybytes =Serialize (data); varExpiresin =timespan.fromminutes (cacheTime); _db. Stringset (Key, Entrybytes, Expiresin); } Public BOOLIsSet (stringkey) { if(_perrequestcachemanager.isset (key))return true; return_db. Keyexists (key); } Public voidRemove (stringkey) {_db. Keydelete (key); _perrequestcachemanager.remove (key); } Public voidRemovebypattern (stringpattern) { foreach(varEpinch_connectionwrapper.getendpoints ()) { varServer =_connectionwrapper.server (EP); varKeys = Server. Keys (Pattern:"*"+ Pattern +"*"); foreach(varKeyinchkeys) {_db. Keydelete (key); } } } Public voidClear () {foreach(varKeyinch_connectionwrapper.getendpoints (). Select (ep = _connectionwrapper.server (EP)). Select (server = server. Keys ()). SelectMany (keys =keys)) {_db. Keydelete (key); } } Public voidDispose () {}#endregion }
View Code
The Redis configuration is written in Webconfig.
Redis in C #