public interface ICache { object Get(string key); /// <summary> /// 根据 key 从缓存中读取数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> T Get<T>(string key); /// <summary> /// 根据 key 从缓存中读取数据 /// 当缓存中不存在读取的数据时, 将通过 getData function 获取数据. /// 如果 getData function 的返回数据也为 null 则不会 set 到缓存. /// 否则会将 getData function 的数据 set 到缓存, 该缓存设置按默认过期时间处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="getData"></param> /// <returns></returns> T Get<T>(string key, Func<T> getData); /// <summary> /// 根据 key 从缓存中读取数据 /// 当缓存中不存在读取的数据时,将通过 getData function 获取数据. /// 如果 getData function 的返回数据也为 null 则不会 set 到缓存. /// 否则会将 getData function 的数据 set 到缓存, 该缓存设置按 timespan 过期时间处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="getData"></param> /// <param name="timespan"></param> /// <returns></returns> T Get<T>(string key, Func<T> getData, TimeSpan timespan); /// <summary> /// 设置数据到缓存 /// 缓存的过期时间按默认时间处理 /// </summary> /// <param name="key"></param> /// <param name="data"></param> void Set(string key, object data); /// <summary> /// 设置数据到缓存,并指定过期时间 /// </summary> /// <param name="key"></param> /// <param name="data"></param> /// <param name="cacheTime"></param> void Set(string key, object data, TimeSpan cacheTime); /// <summary> /// 缓存数组中是否存在某项 key /// </summary> /// <param name="key"></param> /// <returns></returns> bool IsSet(string key); /// <summary> /// 根据key移除缓存值 /// </summary> /// <param name="key"></param> void Remove(string key); }
public class MemcachedCacheProvider : ICache { const int DefaultCacheExpiredMinutes = 20; private MemcachedClient _Client; public MemcachedCacheProvider() { _Client = new MemcachedClient(); } public object Get(string key) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); return _Client.Get(key); } public T Get<T>(string key) { object obj = this.Get(key); if (obj is T) { return (T)obj; } return default(T); } public T Get<T>(string key, Func<T> getData) { return this.Get<T>(key, getData, TimeSpan.FromMinutes(DefaultCacheExpiredMinutes)); } public T Get<T>(string key, Func<T> getData, TimeSpan timespan) { T val = this.Get<T>(key); if (val == null) { val = getData(); if (val != null) { this.Set(key, val, timespan); } } return val; } public void Set(string key, object data) { this.Set(key, data, TimeSpan.FromMinutes(DefaultCacheExpiredMinutes)); } public void Set(string key, object data, TimeSpan cacheTime) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key"); if (data == null) { return; } DateTime time = DateTime.Now.Add(cacheTime); DateTime now = DateTime.Now; if (now >= time) { throw new Exception("时间设置不正确"); } _Client.Set(key, data, time); } public bool IsSet(string key) { return _Client.KeyExists(key); } public void Remove(string key) { _Client.Delete(key); } }
// 获得客户端实例 MemcachedClient mc = new MemcachedClient(); string[] serverlist = { "10.10.20.51:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(serverlist); pool.InitConnections = 3; pool.MinConnections = 3; pool.MaxConnections = 5; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 3000; pool.MaintenanceSleep = 30; pool.Failover = true; pool.Nagle = false; pool.Initialize();