Redis in C #

Source: Internet
Author: User
Tags redis server

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 #

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.