首先nuget安装EnyimMemcached,本地启动memcache,往app.config(mvc项目则是web.config)加入以下内容:
configSection内加入:
<sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup>
configSection外加入:
<enyim.com> <memcached protocol="Text"> <servers> <!-- make sure you use the same ordering of nodes in every configuration you have --> <add address="127.0.0.1" port="11211" /> <!-- <add address="ip address" port="port number" />--> </servers> <socketPool minPoolSize="5" maxPoolSize="10" /> </memcached> </enyim.com>
这里要注意一点:教程上写<memcached protocol="Text">中protocol可配置为Binary,但是如果这么配的话必定会连不上memcache。
完整配置示例:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> </configSections> <enyim.com> <memcached protocol="Text"> <servers> <!-- make sure you use the same ordering of nodes in every configuration you have --> <add address="127.0.0.1" port="11211" /> </servers> <socketPool minPoolSize="5" maxPoolSize="10" /> </memcached> </enyim.com> <!-- <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup>--> </configuration>
EnyimMemcached的详细配置参数:https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Configuration
具体用法:https://github.com/enyim/EnyimMemcached/wiki/MemcachedClient-Usage
封装MemcacheHelper类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Enyim.Caching; using Enyim.Caching.Configuration; using Enyim.Caching.Memcached; namespace Common { public class MemcachedHelper { private static MemcachedClient MemClient; static readonly object padlock = new object(); //线程安全的单例模式 public static MemcachedClient getInstance() { if (MemClient == null) { lock (padlock) { if (MemClient == null) { MemClientInit(); } } } return MemClient; } static void MemClientInit() { try { MemClient = new MemcachedClient(); } catch (Exception ex) { throw ex; } } /// <summary> /// 插入指定值 /// </summary> /// <param name="key">缓存名称 </param> /// <param name="value">缓存值</param> /// <param name="dateTime">过期时间</param> /// <returns>返回是否成功</returns> // public static bool Set(string key, string value, int minutes = 10080) public static bool Set(string key, string value, DateTime dateTime) { MemcachedClient mc = getInstance(); var data = mc.Get(key); if (data == null) return mc.Store(StoreMode.Add, key, value, dateTime); else return mc.Store(StoreMode.Replace, key, value, dateTime); } /// <summary> /// 插入指定值 /// </summary> /// <param name="key">缓存名称 </param> /// <param name="value">缓存值</param> /// <returns>返回是否成功</returns> // public static bool Set(string key, string value, int minutes = 10080) public static bool Set(string key, string value) { MemcachedClient mc = getInstance(); var data = mc.Get(key); DateTime dateTime = DateTime.Now.AddMinutes(10080); if (data == null) return mc.Store(StoreMode.Add, key, value, dateTime); else return mc.Store(StoreMode.Replace, key, value, dateTime); } /// <summary> /// 获取缓存值 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object Get(string key) { MemcachedClient mc = getInstance(); return mc.Get(key); } /// <summary> /// 删除指定缓存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Remove(string key) { MemcachedClient mc = getInstance(); return mc.Remove(key); } /// <summary> /// 清空缓存服务器上的缓存 /// </summary> public static void FlushCache() { MemcachedClient mc = getInstance(); mc.FlushAll(); } } }