.NET Memcached分布式缓存帮助类
1 using Memcached.ClientLibrary; 2 using System; 3 using System.Collections; 4 using System.Collections.Generic; 5 using System.Configuration; 6 using System.Linq; 7 using System.Text; 8 9 namespace Components.Helper 10 { 11 ///分布式缓存Memcach帮助类 12 public class MemcachHelper 13 { 14 private static MemcachedClient _client; 15 ///默认缓存时间(默认20分钟) 16 public static int DefaultCacheTime = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultCacheTime"]) ? Convert.ToInt32(ConfigurationManager.AppSettings["DefaultCacheTime"]) : 1200000); 17 18 ///19 /// 是否启用分布式缓存 20 /// 21 public static bool IsEnableScatteredCache = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["IsEnableScatteredCache"]) ? Convert.ToBoolean(ConfigurationManager.AppSettings["IsEnableScatteredCache"]) : true); 22 static MemcachHelper() 23 { 24 string[] serverlist = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Memcached.ServerList"]) ? ConfigurationManager.AppSettings["Memcached.ServerList"].Split(',') : null); 25 26 if (null == serverlist) 27 { 28 serverlist = new string[] { "127.0.0.1:11211" }; 29 } 30 31 SockIOPool pool = SockIOPool.GetInstance("First"); 32 pool.SetServers(serverlist); 33 pool.Initialize(); 34 35 //初始化 36 _client = new MemcachedClient(); 37 _client.PoolName = "First"; 38 _client.EnableCompression = false; 39 } 40 41 #region Add 42 public static bool Add(string key, object value) 43 { 44 DateTime m_expiryTime = DateTime.Now.AddMilliseconds(DefaultCacheTime); 45 return _client.Add(key, value, m_expiryTime); 46 } 47 public static bool Add(string key, object value, DateTime expiry) 48 { 49 return _client.Add(key, value, expiry); 50 } 51 public static bool Add(string key, object value, int hashCode) 52 { 53 return _client.Add(key, value, hashCode); 54 } 55 public static bool Add(string key, object value, DateTime expiry, int hashCode) 56 { 57 return _client.Add(key, value, expiry, hashCode); 58 } 59 #endregion 60 61 #region Delete 62 ///删除缓存 63 /// 64 ///65 public static bool Delete(string key) 66 { 67 return _client.Delete(key); 68 } 69 70 /// 删除缓存 71 /// 72 /// 73 ///74 public static bool Delete(string key, DateTime expiry) 75 { 76 return _client.Delete(key, expiry); 77 } 78 79 /// 删除缓存 80 /// 81 /// 82 /// 83 ///84 public static bool Delete(string key, object hashCode, DateTime expiry) 85 { 86 return _client.Delete(key, hashCode, expiry); 87 } 88 89 #endregion 90 91 #region Get 92 /// 获取缓存 93 /// 94 ///95 public static object Get(string key) 96 { 97 return _client.Get(key); 98 } 99 /// 获取缓存 100 /// 101 /// 102 ///103 public static object Get(string key, int hashCode)104 {105 return _client.Get(key, hashCode);106 }107 /// 获取缓存 108 /// 109 /// 110 /// 111 ///112 public static object Get(string key, object hashCode, bool asString)113 {114 return _client.Get(key, hashCode, asString);115 }116 #endregion117 118 #region Replace119 /// 120 /// 替换更新121 /// 122 /// 123 /// 124 ///125 public static bool Replace(string key, object value)126 {127 return _client.Replace(key, value);128 }129 /// 130 /// 替换更新131 /// 132 /// 133 /// 134 /// 135 ///136 public static bool Replace(string key, object value, DateTime expiry)137 {138 return _client.Replace(key, value, expiry);139 }140 /// 141 /// 替换更新142 /// 143 /// 144 /// 145 /// 146 ///147 public static bool Replace(string key, object value, int hashCode)148 {149 return _client.Replace(key, value, hashCode);150 }151 /// 152 /// 替换更新153 /// 154 /// 155 /// 156 /// 157 /// 158 ///159 public static bool Replace(string key, object value, DateTime expiry, int hashCode)160 {161 return _client.Replace(key, value, expiry, hashCode);162 }163 #endregion164 165 #region Set166 public static bool Set(string key, object value)167 {168 return _client.Set(key, value);169 }170 public static bool Set(string key, object value, DateTime expiry)171 {172 return _client.Set(key, value, expiry);173 }174 public static bool Set(string key, object value, int hashCode)175 {176 return _client.Set(key, value, hashCode);177 }178 public static bool Set(string key, object value, DateTime expiry, int hashCode)179 {180 return _client.Set(key, value, expiry, hashCode);181 }182 #endregion183 184 #region Stats185 public static Hashtable Stats()186 {187 return _client.Stats();188 }189 190 public static Hashtable Stats(ArrayList servers)191 {192 return _client.Stats(servers);193 }194 #endregion195 196 /// 判断指定Key的缓存是否存在 197 /// 198 ///199 public static bool KeyExists(string key)200 {201 return _client.KeyExists(key);202 }203 204 /// 205 /// 移除缓存,针对空间206 /// 207 /// 208 public static void RemoveRegionCache(string regionName)209 {210 211 }212 }213 }