using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Web; using System.IO; using Memcached.ClientLibrary; namespace CB.Cache.MemCached { /// /// MemCache管理操作类 /// public sealed class MemCachedManager { private static MemcachedClient mc = null; private static SockIOPool pool = null; private static string[] serverList = null; private static MemCachedConfigInfo memCachedConfigInfo = MemCachedConfig.GetConfig(); static MemCachedManager() { CreateManager(); } private static void CreateManager() { serverList = memCachedConfigInfo.ServerList; pool = SockIOPool.GetInstance(memCachedConfigInfo.PoolName); pool.SetServers(serverList); pool.InitConnections = memCachedConfigInfo.InitConnections;//初始化链接数 pool.MinConnections = memCachedConfigInfo.MinConnections;//最少链接数 pool.MaxConnections = memCachedConfigInfo.MaxConnections;//最大连接数 pool.SocketConnectTimeout = memCachedConfigInfo.SocketConnectTimeout;//Socket链接超时时间 pool.SocketTimeout = memCachedConfigInfo.SocketTimeout;// Socket超时时间 pool.MaintenanceSleep = memCachedConfigInfo.MaintenanceSleep;//维护线程休息时间 pool.Failover = memCachedConfigInfo.FailOver; //失效转移(一种备份操作模式) pool.Nagle = memCachedConfigInfo.Nagle;//是否用nagle算法启动socket pool.HashingAlgorithm = HashingAlgorithm.NewCompatibleHash; pool.Initialize(); mc = new MemcachedClient(); mc.PoolName = memCachedConfigInfo.PoolName; mc.EnableCompression = false; } public enum Stats { Reset, Malloc, Maps, Sizes, Slabs, Items, CachedDump, Detail } /// /// 直接对memcached操作socket /// /// /// /// /// public static ArrayList GetStats(ArrayList serverArrayList, Stats statsCommand, string param) { ArrayList statsArray = new ArrayList(); param = string.IsNullOrEmpty(param) ? "" : param.Trim().ToLower(); string commandstr = "stats"; //转换stats命令参数 switch (statsCommand) { case Stats.Reset: { commandstr = "stats reset"; break; } case Stats.Malloc: { commandstr = "stats malloc"; break; } case Stats.Maps: { commandstr = "stats maps"; break; } case Stats.Sizes: { commandstr = "stats sizes"; break; } case Stats.Slabs: { commandstr = "stats slabs"; break; } case Stats.Items: { commandstr = "stats items"; break; } case Stats.CachedDump: { string[] statsparams = param.Split(' '); if (statsparams.Length == 2) commandstr = "stats cachedump " + param; break; } case Stats.Detail: { if (string.Equals(param, "on") || string.Equals(param, "off") || string.Equals(param, "dump")) commandstr = "stats detail " + param.Trim(); break; } default: { commandstr = "stats"; break; } } //加载返回值 Hashtable stats = MemCachedManager.CacheClient.Stats(serverArrayList, commandstr); foreach (string key in stats.Keys) { statsArray.Add(key); Hashtable values = (Hashtable)stats[key]; foreach (string key2 in values.Keys) { statsArray.Add(key2 + ":" + values[key2]); } } return statsArray; } /// /// 获取所有缓存key /// /// public static List GetKeys() { List list = new List(); ArrayList itemarr = new ArrayList(); ArrayList arrayList = new ArrayList(); foreach (string server in MemCachedManager.ServerList) { arrayList.Add(server); } ArrayList arr = GetStats(arrayList, MemCachedManager.Stats.Items, null); foreach (string a in arr) { string[] tmparr = a.Split(':'); if (tmparr.Length > 1) { int item_id = 0; int.TryParse(tmparr[1], out item_id); bool find = false; foreach (int item in itemarr) { if (item == item_id) find = true; } if (!find && item_id > 0 && item_id != 11211) itemarr.Add(item_id); } } foreach (int item in itemarr) { ArrayList cachearr = MemCachedManager.GetStats(arrayList, MemCachedManager.Stats.CachedDump, "" + item + " 10"); foreach (string cache in cachearr) { if (cache.IndexOf("11211") == -1) { string[] strs = cache.Split(':'); list.Add(strs[0]); } } } return list; } /// /// 缓存服务器地址列表 /// public static string[] ServerList { set { if (value != null) serverList = value; } get { return serverList; } } /// /// 客户端缓存操作对象 /// public static MemcachedClient CacheClient { get { if (mc == null) CreateManager(); return mc; } } public static void Dispose() { if (pool != null) pool.Shutdown(); } } }