Memcached缓存帮助类

.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     /// <summary>分布式缓存Memcach帮助类</summary>
 12     public class MemcachHelper
 13     {
 14         private static MemcachedClient _client;
 15         /// <summary>默认缓存时间(默认20分钟)</summary>
 16         public static int DefaultCacheTime = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultCacheTime"]) ? Convert.ToInt32(ConfigurationManager.AppSettings["DefaultCacheTime"]) : 1200000);
 17 
 18         /// <summary>
 19         /// 是否启用分布式缓存
 20         /// </summary>
 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         /// <summary>删除缓存</summary>
 63         /// <param name="key"></param>
 64         /// <returns></returns>
 65         public static bool Delete(string key)
 66         {
 67             return _client.Delete(key);
 68         }
 69 
 70         /// <summary>删除缓存</summary>
 71         /// <param name="key"></param>
 72         /// <param name="expiry"></param>
 73         /// <returns></returns>
 74         public static bool Delete(string key, DateTime expiry)
 75         {
 76             return _client.Delete(key, expiry);
 77         }
 78 
 79         /// <summary>删除缓存</summary>
 80         /// <param name="key"></param>
 81         /// <param name="hashCode"></param>
 82         /// <param name="expiry"></param>
 83         /// <returns></returns>
 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         /// <summary>获取缓存</summary>
 93         /// <param name="key"></param>
 94         /// <returns></returns>
 95         public static object Get(string key)
 96         {
 97             return _client.Get(key);
 98         }
 99         /// <summary>获取缓存</summary>
100         /// <param name="key"></param>
101         /// <param name="hashCode"></param>
102         /// <returns></returns>
103         public static object Get(string key, int hashCode)
104         {
105             return _client.Get(key, hashCode);
106         }
107         /// <summary>获取缓存</summary>
108         /// <param name="key"></param>
109         /// <param name="hashCode"></param>
110         /// <param name="asString"></param>
111         /// <returns></returns>
112         public static object Get(string key, object hashCode, bool asString)
113         {
114             return _client.Get(key, hashCode, asString);
115         }
116         #endregion
117 
118         #region Replace
119         /// <summary>
120         /// 替换更新
121         /// </summary>
122         /// <param name="key"></param>
123         /// <param name="value"></param>
124         /// <returns></returns>
125         public static bool Replace(string key, object value)
126         {
127             return _client.Replace(key, value);
128         }
129         /// <summary>
130         /// 替换更新
131         /// </summary>
132         /// <param name="key"></param>
133         /// <param name="value"></param>
134         /// <param name="expiry"></param>
135         /// <returns></returns>
136         public static bool Replace(string key, object value, DateTime expiry)
137         {
138             return _client.Replace(key, value, expiry);
139         }
140         /// <summary>
141         /// 替换更新
142         /// </summary>
143         /// <param name="key"></param>
144         /// <param name="value"></param>
145         /// <param name="hashCode"></param>
146         /// <returns></returns>
147         public static bool Replace(string key, object value, int hashCode)
148         {
149             return _client.Replace(key, value, hashCode);
150         }
151         /// <summary>
152         /// 替换更新
153         /// </summary>
154         /// <param name="key"></param>
155         /// <param name="value"></param>
156         /// <param name="expiry"></param>
157         /// <param name="hashCode"></param>
158         /// <returns></returns>
159         public static bool Replace(string key, object value, DateTime expiry, int hashCode)
160         {
161             return _client.Replace(key, value, expiry, hashCode);
162         }
163         #endregion
164 
165         #region Set
166         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         #endregion
183 
184         #region Stats
185         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         #endregion
195 
196         /// <summary>判断指定Key的缓存是否存在</summary>
197         /// <param name="key"></param>
198         /// <returns></returns>
199         public static bool KeyExists(string key)
200         {
201             return _client.KeyExists(key);
202         }
203 
204         /// <summary>
205         /// 移除缓存,针对空间
206         /// </summary>
207         /// <param name="key"></param>
208         public static void RemoveRegionCache(string regionName)
209         {
210 
211         }
212     }
213 }
MemcachHelper

 

posted @ 2016-05-07 10:30  不二囧青年  阅读(267)  评论(0编辑  收藏  举报