using System; using System.Web; using System.Linq; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using System.Configuration; using Memcached.ClientLibrary; namespace HomeService.Utility { public class CacheHelper { #region 变量和构造函数 //缓存服务器地址和端口,这样就实现了分布式缓存。服务器可以多个,用Socket读写数据"127.0.0.1:12345", private static Dictionary Servers = new Dictionary(); static MemcachedClient mc = null; //服务器缓存 static Dictionary Servers1 = new Dictionary(); static object LOCK_OBJECT = new object();//安全锁定 /// /// 静态构造函数 /// static CacheHelper() { InitServer(); List keys = Servers.Keys.ToList(); foreach (var k in Servers.Keys) { SockIOPool pool = SockIOPool.GetInstance(k); string[] s = new string[] { Servers[k] }; pool.SetServers(s);//设置服务器 pool.MaxConnections = 10000; pool.MinConnections = 10; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 100; pool.Initialize();//初始化缓存线程池 } //默认池 List defaultServerList=new List(); foreach (var k in Servers.Keys) defaultServerList.Add(Servers[k]); SockIOPool defaultPool = SockIOPool.GetInstance("DefaultPool"); defaultPool.SetServers(defaultServerList.ToArray());//设置服务器 defaultPool.MaxConnections = 10000; defaultPool.MinConnections = 10; defaultPool.SocketConnectTimeout = 1000; defaultPool.SocketTimeout = 100; defaultPool.Initialize(); //初始化默认线程池 mc = new MemcachedClient(); mc.PoolName = "DefaultPool"; } /// /// 初始化服务器列表,这里默认两台服务器. /// static void InitServer(){ //这里可以写复杂灵活点,动态从配置文件获取 Servers.Add("Svr1", ConfigurationManager.AppSettings["Svr1"]); Servers.Add("Svr2", ConfigurationManager.AppSettings["Svr2"]); } private CacheHelper() { } #endregion #region 获取客户端 public static MemcachedClient GetClient(string server) { MemcachedClient current = Singleton.Instance; current.PoolName = server; return current; } #endregion #region 默认 #region 写(Set) /// /// 设置数据缓存 /// /// 键 /// 值 public static void Set(string key, object value) { mc.Set(key, value); } /// /// 设置数据缓存 /// /// 键 /// 值 /// 哈希码 public static void Set(string key, object value, int hashCode) { mc.Set(key, value, hashCode); } /// /// 设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void Set(string key, object value, DateTime expiry) { try { mc.Set(key, value, expiry); } catch (Exception ex) { throw ex; } } /// /// 设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void Set(string key, object value, DateTime expiry, int hashCode) { mc.Set(key, value, expiry, hashCode); } #endregion #region 读(Get) #region 返回泛型 /// /// 读取数据缓存 /// /// 键 public static T Get(string key) { return (T)mc.Get(key); } /// /// 读取数据缓存 /// /// 键 /// 哈希码 public static T Get(string key, int hashCode) { return (T)mc.Get(key, hashCode); } /// /// 读取数据缓存 /// /// 键 /// 是否把值作为字符串返回 public static T Get(string key, object value, bool asString) { return (T)mc.Get(key, value, asString); } #endregion /// /// 读取数据缓存 /// /// 键 public static object Get(string key) { return mc.Get(key); } /// /// 读取数据缓存 /// /// 键 /// 哈希码 public static object Get(string key, int hashCode) { return mc.Get(key, hashCode); } /// /// 读取数据缓存 /// /// 键 /// 是否把值作为字符串返回 public static object Get(string key, object value, bool asString) { return mc.Get(key, value, asString); } #endregion #region 批量写(Set) /// /// 批量设置数据缓存 /// /// 键 /// 值 public static void SetMultiple(string[] keys, object[] values) { for (int i = 0; i < keys.Length; i++) { mc.Set(keys[i], values[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 哈希码 public static void SetMultiple(string[] keys, object[] values, int[] hashCodes) { for (int i = 0; i < keys.Length; i++) { mc.Set(keys[i], values[i], hashCodes[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void SetMultiple(string[] keys, object[] values, DateTime[] expirys) { for (int i = 0; i < keys.Length; i++) { mc.Set(keys[i], values[i], expirys[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void Set(string[] keys, object[] values, DateTime[] expirys, int[] hashCodes) { for (int i = 0; i < keys.Length; i++) { mc.Set(keys[i], values[i], expirys[i], hashCodes[i]); } } #endregion #region 批量读取(Multiple),返回哈希表 Hashtable /// /// 批量读取数据缓存 /// /// 键集合 public static Hashtable GetMultiple(string[] keys) { return mc.GetMultiple(keys); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static Hashtable GetMultiple(string[] keys, int[] hashCodes) { return mc.GetMultiple(keys, hashCodes); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static Hashtable GetMultiple(string[] keys, int[] hashCodes, bool asString) { return mc.GetMultiple(keys, hashCodes, asString); } #endregion #region 批量读取(Multiple),返回对象数组object[] /// /// 批量读取数据缓存 /// /// 键集合 public static object[] GetMultipleArray(string[] keys) { return mc.GetMultipleArray(keys); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static object[] GetMultipleArray(string[] keys, int[] hashCodes) { return mc.GetMultipleArray(keys, hashCodes); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static object[] GetMultipleArray(string[] keys, int[] hashCodes, bool asString) { return mc.GetMultipleArray(keys, hashCodes, asString); } #endregion #region 批量读取(Multiple),返回泛型集合List[T] /// /// 批量读取数据缓存 /// /// 键集合 public static List GetMultipleList(string[] keys) { object[] obj = mc.GetMultipleArray(keys); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static List GetMultipleList(string[] keys, int[] hashCodes) { object[] obj = mc.GetMultipleArray(keys, hashCodes); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static List GetMultipleList(string[] keys, int[] hashCodes, bool asString) { object[] obj = mc.GetMultipleArray(keys, hashCodes, asString); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } #endregion #region 替换更新(Replace) /// /// 替换更新数据缓存 /// /// 键 /// 值 public static void Replace(string key, object value) { mc.Replace(key, value); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 哈希码 public static void Replace(string key, object value, int hashCode) { mc.Replace(key, value, hashCode); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 过期时间 public static void Replace(string key, object value, DateTime expiry) { mc.Replace(key, value, expiry); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 过期时间 public static void Replace(string key, object value, DateTime expiry, int hashCode) { mc.Replace(key, value, expiry, hashCode); } #endregion #region 删除(Delete) /// ///删除指定条件缓存 /// /// 键 public static bool Delete(string key) { return mc.Delete(key); } /// /// 删除指定条件缓存 /// /// 键 /// 哈希码 /// 过期时间 public static bool Delete(string key, int hashCode, DateTime expiry) { return mc.Delete(key, hashCode, expiry); } /// /// 删除指定条件缓存 /// /// 键 /// 过期时间 public static bool Delete(string key, DateTime expiry) { return mc.Delete(key, expiry); } /// /// 移除全部缓存 /// public static void RemovAllCache() { mc.FlushAll(); } /// /// 移除全部缓存 /// /// 移除指定服务器缓存 public static void RemovAllCache(ArrayList list) { mc.FlushAll(list); } #endregion #region 是否存在(Exists) /// /// 判断指定键的缓存是否存在 /// /// 键 /// public static bool IsExists(string key) { return mc.KeyExists(key); } #endregion #region 数值增减 #region 存储一个数值元素 /// /// 存储一个数值元素 /// /// 键 /// public static bool StoreCounter(string key, long counter) { return mc.StoreCounter(key, counter); } /// /// 存储一个数值元素 /// /// 键 /// 增长幅度 /// 哈希码 /// public static bool StoreCounter(string key, long counter, int hashCode) { return mc.StoreCounter(key, counter, hashCode); } #endregion #region 获取一个数值元素 /// /// 获取一个数值元素 /// /// 键 /// public static long GetCounter(string key) { return mc.GetCounter(key); } /// /// 获取一个数值元素 /// /// 键 /// 哈希码 /// public static long GetCounter(string key, int hashCode) { return mc.GetCounter(key, hashCode); } #endregion #region 增加一个数值元素的值(Increment) /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// public static long Increment(string key) { return mc.Increment(key); } /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// 增长幅度 /// public static long Increment(string key, long inc) { return mc.Increment(key, inc); } /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// 增长幅度 /// 哈希码 /// public static long Increment(string key, long inc, int hashCode) { return mc.Increment(key, inc, hashCode); } #endregion #region 减小一个数值元素的值(Decrement) /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// public static long Decrement(string key) { return mc.Decrement(key); } /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// 增长幅度 /// public static long Decrement(string key, long inc) { return mc.Decrement(key, inc); } /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// 增长幅度 /// 哈希码 /// public static long Decrement(string key, long inc, int hashCode) { return mc.Decrement(key, inc, hashCode); } #endregion #endregion #endregion #region 指定服务器 #region 获取(Get) /// /// 从指定服务器获取 /// /// 服务器,Svr1,Svr2 /// 键 /// public static object GetFrom(string server,string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Get(key); } /// /// 从指定服务器获取 /// /// 服务器,Svr1,Svr2 /// 键 /// 哈希码 public static object GetFrom(string server, string key, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Get(key, hashCode); } /// /// 从指定服务器获取 /// /// 服务器,Svr1,Svr2 /// 键 /// 是否把值作为字符串返回 public static object GetFrom(string server, string key, object value, bool asString) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Get(key, value, asString); } #endregion #region 写入(Set) /// /// 设置数据缓存 /// /// 服务器,格式为Svr1,Svr2,Svr3,对应配置文件host /// 键 /// 值 public static void SetTo(string server,string key, object value) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Set(key, value); } /// /// 设置数据缓存 /// /// 服务器,格式为Svr1,Svr2,Svr3,对应配置文件host /// 键 /// 值 /// 哈希码 public static void SetTo(string server, string key, object value, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Set(key, value, hashCode); } /// /// 设置数据缓存 /// /// 服务器,格式为Svr1,Svr2,Svr3,对应配置文件host /// 键 /// 值 /// 过期时间 public static void SetTo(string server, string key, object value, DateTime expiry) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Set(key, value, expiry); } /// /// 设置数据缓存 /// /// 服务器,格式为Svr1,Svr2,Svr3,对应配置文件host /// 键 /// 值 /// 过期时间 public static void SetTo(string server, string key, object value, DateTime expiry, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Set(key, value, expiry, hashCode); } #endregion #region 批量写(Set) /// /// 批量设置数据缓存 /// /// 键 /// 值 public static void SetMultipleTo(string server, string[] keys, object[] values) { MemcachedClient client = GetClient(server); client.PoolName = server; for (int i = 0; i < keys.Length; i++) { client.Set(keys[i], values[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 哈希码 public static void SetMultipleTo(string server, string[] keys, object[] values, int[] hashCodes) { MemcachedClient client = GetClient(server); client.PoolName = server; for (int i = 0; i < keys.Length; i++) { client.Set(keys[i], values[i], hashCodes[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void SetMultipleTo(string server, string[] keys, object[] values, DateTime[] expirys) { MemcachedClient client = GetClient(server); client.PoolName = server; for (int i = 0; i < keys.Length; i++) { client.Set(keys[i], values[i], expirys[i]); } } /// /// 批量设置数据缓存 /// /// 键 /// 值 /// 过期时间 public static void SetMultipleTo(string server, string[] keys, object[] values, DateTime[] expirys, int[] hashCodes) { MemcachedClient client = GetClient(server); client.PoolName = server; for (int i = 0; i < keys.Length; i++) { client.Set(keys[i], values[i], expirys[i], hashCodes[i]); } } #endregion #region 批量读取(Multiple),返回哈希表 Hashtable /// /// 批量读取数据缓存 /// /// 键集合 public static Hashtable GetMultipleFrom(string server,string[] keys) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultiple(keys); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static Hashtable GetMultipleFrom(string server, string[] keys, int[] hashCodes) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultiple(keys, hashCodes); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static Hashtable GetMultipleFrom(string server, string[] keys, int[] hashCodes, bool asString) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultiple(keys, hashCodes, asString); } #endregion #region 批量读取(Multiple),返回对象数组object[] /// /// 批量读取数据缓存 /// /// 键集合 public static object[] GetMultipleArrayFrom(string server, string[] keys) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultipleArray(keys); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static object[] GetMultipleArrayFrom(string server, string[] keys, int[] hashCodes) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultipleArray(keys, hashCodes); } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static object[] GetMultipleArrayFrom(string server, string[] keys, int[] hashCodes, bool asString) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetMultipleArray(keys, hashCodes, asString); } #endregion #region 批量读取(Multiple),返回泛型集合List[T] /// /// 批量读取数据缓存 /// /// 键集合 public static List GetMultipleListFrom(string server, string[] keys) { MemcachedClient client = GetClient(server); client.PoolName = server; object[] obj = client.GetMultipleArray(keys); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 public static List GetMultipleListFrom(string server, string[] keys, int[] hashCodes) { MemcachedClient client = GetClient(server); client.PoolName = server; object[] obj = client.GetMultipleArray(keys, hashCodes); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } /// /// 批量读取数据缓存 /// /// 键集合 /// 哈希码集合 /// 所有值返回字符 public static List GetMultipleListFrom(string server, string[] keys, int[] hashCodes, bool asString) { MemcachedClient client = GetClient(server); client.PoolName = server; object[] obj = client.GetMultipleArray(keys, hashCodes, asString); List list = new List(); foreach (object o in obj) { list.Add((T)o); } return list; } #endregion #region 替换更新(Replace) /// /// 替换更新数据缓存 /// /// 键 /// 值 public static void ReplaceFrom(string server, string key, object value) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Replace(key, value); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 哈希码 public static void ReplaceFrom(string server, string key, object value, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Replace(key, value, hashCode); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 过期时间 public static void ReplaceFrom(string server, string key, object value, DateTime expiry) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Replace(key, value, expiry); } /// /// 替换更新数据缓存 /// /// 键 /// 值 /// 过期时间 public static void ReplaceFrom(string server, string key, object value, DateTime expiry, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; client.Replace(key, value, expiry, hashCode); } #endregion #region 删除(Delete) /// ///删除指定条件缓存 /// /// 键 public static bool DeleteFrom(string server, string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Delete(key); } /// /// 删除指定条件缓存 /// /// 键 /// 哈希码 /// 过期时间 public static bool DeleteFrom(string server, string key, int hashCode, DateTime expiry) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Delete(key, hashCode, expiry); } /// /// 删除指定条件缓存 /// /// 键 /// 过期时间 public static bool DeleteFrom(string server, string key, DateTime expiry) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Delete(key, expiry); } /// /// 移除全部缓存 /// public static void RemovAllCacheFrom(string server) { MemcachedClient client = GetClient(server); client.PoolName = server; client.FlushAll(); } /// /// 移除全部缓存 /// /// 移除指定服务器缓存 public static void RemovAllCacheFrom(string server, ArrayList list) { MemcachedClient client = GetClient(server); client.PoolName = server; client.FlushAll(list); } #endregion #region 是否存在(Exists) /// /// 判断指定键的缓存是否存在 /// /// 键 /// public static bool IsExists(string server,string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.KeyExists(key); } #endregion #region 数值增减 #region 存储一个数值元素 /// /// 存储一个数值元素 /// /// 键 /// public static bool StoreCounterTo(string server,string key, long counter) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.StoreCounter(key, counter); } /// /// 存储一个数值元素 /// /// 键 /// 增长幅度 /// 哈希码 /// public static bool StoreCounterTo(string server, string key, long counter, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.StoreCounter(key, counter, hashCode); } #endregion #region 获取一个数值元素 /// /// 获取一个数值元素 /// /// 键 /// public static long GetCounterFrom(string server, string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetCounter(key); } /// /// 获取一个数值元素 /// /// 键 /// 哈希码 /// public static long GetCounterFrom(string server, string key, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.GetCounter(key, hashCode); } #endregion #region 增加一个数值元素的值(Increment) /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// public static long IncrementTo(string server, string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Increment(key); } /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// 增长幅度 /// public static long IncrementTo(string server, string key, long inc) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Increment(key, inc); } /// /// 将一个数值元素增加。 如果元素的值不是数值类型,将其作为0处理 /// /// 键 /// 增长幅度 /// 哈希码 /// public static long IncrementTo(string server, string key, long inc, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Increment(key, inc, hashCode); } #endregion #region 减小一个数值元素的值(Decrement) /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// public static long DecrementFrom(string server, string key) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Decrement(key); } /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// 增长幅度 /// public static long DecrementFrom(string server, string key, long inc) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Decrement(key, inc); } /// /// 减小一个数值元素的值,减小多少由参数offset决定。 如果元素的值不是数值,以0值对待。如果减小后的值小于0,则新的值被设置为0 /// /// 键 /// 增长幅度 /// 哈希码 /// public static long DecrementFrom(string server, string key, long inc, int hashCode) { MemcachedClient client = GetClient(server); client.PoolName = server; return client.Decrement(key, inc, hashCode); } #endregion #endregion #endregion } }