C # uses enyimmemcached to connect memcache

Source: Internet
Author: User

First NuGet installs enyimmemcached, launches the memcache locally, and adds the following to App. Config (the MVC project is web. config):

Configsection Add:

<name= "enyim.com"><name= " memcached "  type=" Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching " /> </ sectiongroup >

configsection addition:

<enyim.com><memcachedProtocol= "Text"><Servers><!--Make sure-the same ordering of nodes in every configuration -<AddAddress= "127.0.0.1"Port= "11211" /><!--<add address= "IP Address" port= "port number"/> -</Servers><Socketpoolminpoolsize= "5"maxpoolsize= "Ten" /></memcached></enyim.com>

One thing to note here: the tutorial <memcached protocol= "Text" > protocol can be configured as binary, but if so, it will not even memcache.

Full Configuration Example:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <configsections>    <sectiongroupname= "Enyim.com">      < Sectionname= "memcached"type= "Enyim.Caching.Configuration.MemcachedClientSection, enyim.caching" />    </sectiongroup>  </configsections>  <enyim.com>    <memcachedProtocol= "Text">      <Servers>        <!--Make sure-the same ordering of nodes in every configuration -        <AddAddress= "127.0.0.1"Port= "11211" />      </Servers>      <Socketpoolminpoolsize= "5"maxpoolsize= "Ten"  />    </memcached>  </enyim.com><!--<startup> <supportedruntime version= "v4.0" sku= ". netframework,version=v4.6.1 "/> </startup> -</Configuration>

Detailed configuration parameters for enyimmemcached: https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Configuration

Specific usage: https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Usage

Package Memcachehelper class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingenyim.caching;usingEnyim.Caching.Configuration;usingEnyim.Caching.Memcached;namespacecommon{ Public classMemcachedhelper {Private Staticmemcachedclient memclient; Static ReadOnly Objectpadlock =New Object(); //thread-Safe single-case mode         Public Staticmemcachedclient getinstance () {if(Memclient = =NULL)            {                Lock(padlock) {if(Memclient = =NULL) {memclientinit (); }                }            }            returnmemclient; }        Static voidMemclientinit () {Try{memclient=Newmemcachedclient (); }            Catch(Exception ex) {Throwex; }        }        /// <summary>        ///Insert specified value/// </summary>        /// <param name= "key" >Cache Name</param>        /// <param name= "value" >Cached Values</param>        /// <param name= "DateTime" >Expiry Time</param>        /// <returns>returns whether successful</returns>//Public static bool Set (string key, string value, int minutes = 10080)         Public Static BOOLSet (stringKeystringvalue, DateTime datetime) {Memcachedclient MC=getinstance (); vardata =MC.            Get (key); if(Data = =NULL)                returnMC.            Store (Storemode.add, key, value, DateTime); Else                returnMC.        Store (Storemode.replace, key, value, DateTime); }        /// <summary>        ///Insert specified value/// </summary>        /// <param name= "key" >Cache Name</param>        /// <param name= "value" >Cached Values</param>        /// <returns>returns whether successful</returns>        //Public static bool Set (string key, string value, int minutes = 10080)         Public Static BOOLSet (stringKeystringvalue) {Memcachedclient MC=getinstance (); vardata =MC.            Get (key); datetime datetime= DateTime.Now.AddMinutes (10080); if(Data = =NULL)                returnMC.            Store (Storemode.add, key, value, DateTime); Else                returnMC.        Store (Storemode.replace, key, value, DateTime); }        /// <summary>        ///Get Cache value/// </summary>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static ObjectGet (stringkey) {Memcachedclient MC=getinstance (); returnMC.        Get (key); }        /// <summary>        ///Delete the specified cache/// </summary>        /// <param name= "key" ></param>        /// <returns></returns>         Public Static BOOLRemove (stringkey) {Memcachedclient MC=getinstance (); returnMC.        Remove (key); }        /// <summary>        ///emptying the cache on the cache server/// </summary>         Public Static voidFlushcache () {Memcachedclient MC=getinstance (); Mc.        Flushall (); }    }}

C # uses enyimmemcached to connect memcache

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.