---恢复内容开始---
缓存服务作为系统中重要的一部分,我相信大家肯定不陌生。缓存的概念和作用这里就不做赘述,这篇文章只讲我是如何封装,如何并购入我的SOA框架以及如何使用。我采用的是使用量很广的一个缓存服务:Memcached。如果大家对Memcached 不太熟悉,可以百度,这里不做详细的介绍。下面详细的介绍使用方法。
1.下载Memcached服务端,命令行输入 'c:memcachedmemcached.exe -d install' 进行服务安装
2.下载Enyim.Caching源代码,在需要封装的地方引用Enyim.Caching.dll
3.在我SOA框架中加入
<ServiceAddressInfo>
<ServiceType>CacheService</ServiceType>
<Name>缓存服务</Name>
<Ip>127.0.0.1</Ip>
<Port>11211</Port>
<Alias>Service</Alias>
</ServiceAddressInfo>
4.对Memcached客户端代码进行封装:
1 /// <summary> 2 3 /// 缓存服务器代理 4 5 /// </summary> 6 7 public class MemcachedProxy 8 9 { 10 11 private static MemcachedClient _memcachedClient = null; 12 13 private static MemcachedClientConfiguration _config = null; 14 15 /// <summary> 16 17 /// 获取一个新的Session,操作完成后需要关闭 18 19 /// </summary> 20 21 /// <returns>返回一个新的Session。</returns> 22 23 public static MemcachedClient NewSession() 24 25 { 26 27 try 28 29 { 30 31 if (_memcachedClient == null) 32 33 { 34 35 return new MemcachedClient(_config); 36 37 } 38 39 return _memcachedClient; 40 41 } 42 43 catch (Exception ex) 44 45 { 46 47 LogHelper.WriteModuleLog<MemcachedProxy>(ex); 48 49 throw new Exception("连接缓存服务失败"); 50 51 } 52 53 } 54 55 static MemcachedProxy() 56 57 { 58 59 //缓存服务 这个地方获取的是我服务里面是缓存服务的地址 60 61 var cacheAddress = ServiceProxyFactory.AllServices.Find(r => r.ServiceType == ServiceTypeDefine.CacheService); 62 63 var mcc = new MemcachedClientConfiguration(); 64 65 mcc.Servers.Add(new IPEndPoint(IPAddress.Parse(cacheAddress.Ip), cacheAddress.Port)); 66 67 mcc.NodeLocator = typeof(DefaultNodeLocator); 68 69 mcc.KeyTransformer = typeof(SHA1KeyTransformer); 70 71 mcc.Transcoder = typeof(DefaultTranscoder); 72 73 mcc.SocketPool.MinPoolSize = 10; 74 75 mcc.SocketPool.MaxPoolSize = 500; 76 77 mcc.SocketPool.ConnectionTimeout = new TimeSpan(0, 0, 10); 78 79 mcc.SocketPool.DeadTimeout = new TimeSpan(0, 0, 30); 80 81 } 82 83 /// <summary> 84 85 /// 获取缓存数据 86 87 /// </summary> 88 89 /// <typeparam name="T"></typeparam> 90 91 /// <param name="key"></param> 92 93 /// <returns></returns> 94 95 public static T Get<T>(string key) 96 97 { 98 99 try 100 101 { 102 103 using (var cache = NewSession()) 104 105 { 106 107 return cache.Get<T>(key); 108 109 } 110 111 } 112 113 catch (Exception ex) 114 115 { 116 117 LogHelper.WriteDefaultLog(string.Format("获取缓存失败:{0}", ex.ToString())); 118 119 return default(T); 120 121 } 122 123 } 124 125 /// <summary> 126 127 /// 存储缓存数据 128 129 /// </summary> 130 131 /// <param name="data">数据</param> 132 133 /// <param name="key">键值</param> 134 135 public static void Store(object data,string key) 136 137 { 138 139 try 140 141 { 142 143 using (var cache = NewSession()) 144 145 { 146 147 //默认存储10分钟 148 149 cache.Store(StoreMode.Set, key, data, new TimeSpan(0, GlobalParameter.CacheDataSaveTime, 0)); 150 151 } 152 153 } 154 155 catch (Exception ex) 156 157 { 158 159 LogHelper.WriteDefaultLog(string.Format("存储缓存数据失败:{0}", ex.ToString())); 160 161 } 162 163 } 164 165 }
5.使用方法:
1 public void Execute(RequestModel rm, ResponseModel resM) 2 { 3 var data = rm.Parameters.ToObject<List<object>>(); 4 5 //先从缓存服务取得数据 6 var cacheData = MemcachedProxy.Get<List<PortalsNoticeInfo>>(CacheParameter.Cache_PortalsNotice); 7 8 //读取缓存数据失败 9 if (cacheData == null || cacheData.Count == 0) 10 { 11 //从数据库获取 12 var result = _noticeManage.GetPublishedNotice(Int32.Parse(data[0].ToString()), Int32.Parse(data[1].ToString())); 13 14 //写入缓存 15 MemcachedProxy.Store(result, CacheParameter.Cache_PortalsNotice); 16 17 //返回结果 18 resM.ResultData = result.ToJson(); 19 resM.State = ResponseStateDefine.Success; 20 } 21 else 22 { 23 //返回结果 24 resM.ResultData = cacheData.ToJson(); 25 resM.State = ResponseStateDefine.Success; 26 } 27 }
(由于代码较多,可能很多地方没诠释明白,需要了解更多或者需要服务和源代码的朋友可以私下联系我)