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