本文共 4859 字,大约阅读时间需要 16 分钟。
阿里云的开放式分布式缓存(OCS)简化了缓存的运维管理,使用起来很方便,官方推荐的.NET访问客户端类库为 Enyim.Caching,下面对此做一个封装。
首先引用最新版本 Enyim.Caching ,比如2.13.2.0 版本。
先按照阿里云的示例代码,封装一个基础的 MemCached访问类:
public sealed class MemCached { 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() { //初始化缓存 string host = System.Configuration.ConfigurationManager.AppSettings["MemoryCacheServer"]; MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration(); IPAddress newaddress = IPAddress.Parse(Dns.GetHostEntry (host).AddressList[0].ToString());//your_ocs_host替换为OCS内网地址 IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211); // 配置文件 - ip memConfig.Servers.Add(ipEndPoint); // 配置文件 - 协议 memConfig.Protocol = MemcachedProtocol.Binary; // 配置文件-权限 memConfig.Authentication.Type = typeof(PlainTextAuthenticator); memConfig.Authentication.Parameters["zone"] = ""; memConfig.Authentication.Parameters["userName"] = System.Configuration.ConfigurationManager.AppSettings["CacheServerUID"]; memConfig.Authentication.Parameters["password"] = System.Configuration.ConfigurationManager.AppSettings["CacheServerPWD"]; //下面请根据实例的最大连接数进行设置 memConfig.SocketPool.MinPoolSize = 5; memConfig.SocketPool.MaxPoolSize = 200; MemClient=new MemcachedClient(memConfig); //如果采用配置文件,可以注释上面代码,直接下面一行代码: //MemClient = new MemcachedClient(); } }
再定义一个缓存访问接口:
public interface ICacheManager { ////// 是否启用缓存 /// bool IsEnabled { get; } ////// 根据key取缓存对象 /// T Get(string key); object Get(string key); /// /// 放入缓存 /// bool Set(string key, object data, int cacheTime); ////// 是否在缓存中 /// bool IsSet(string key); ////// 从缓存删除 /// void Remove(string key); ////// 根据规则删除 /// void RemoveByPattern(string pattern); ////// 清空所有缓存 /// void Clear(); }
最后封装 OCSManager:
public class OCSManager : ICacheManager { private static readonly MemcachedClient cache; private static readonly OCSManager instance; private OCSManager() { } static OCSManager() { cache = MemCached.getInstance(); instance = new OCSManager(); } private static bool isServerConnected = true; public static ICacheManager Instance { get { return instance; } } #region ICacheManager 成员 public bool IsEnabled { get { return cache != null && isServerConnected; } } public T Get(string key) { //var result = cache.ExecuteGet (key); //return result.Value; return cache.Get (key); } public object Get(string key) { return cache.Get(key); } public bool Set(string key, object data, int cacheTime) { if (data == null) return false; //var result= cache.ExecuteStore (Enyim.Caching.Memcached.StoreMode.Add,key,data,DateTime.Now + TimeSpan.FromMinutes(cacheTime)); //return result.Success; return cache.Store(Enyim.Caching.Memcached.StoreMode.Set, key, data, DateTime.Now + TimeSpan.FromMinutes(cacheTime)); } public bool IsSet(string key) { object obj; return cache.TryGet(key, out obj); } public void Remove(string key) { cache.Remove(key); } public void RemoveByPattern(string pattern) { //throw new NotImplementedException(); } public void Clear() { cache.FlushAll(); } #endregion }
最后,在应用程序配置文件,增加上如下的配置节点:
注意:OCS使用的时候,会分配指定的用户名和密码, userName="" password="" ,请在实际使用之前做好这个配置。
本文转自深蓝医生博客园博客,原文链接:http://www.cnblogs.com/bluedoctor/p/5642650.html,如需转载请自行联系原作者