.net/c# memcached 获取指定前缀缓存键(keys)

.net 类库 memcacheddotnet_clientlib_2.0

1.增加memcacheddotnet_clientlib_2.0代码

下载好组件后,用vs打开.net类库memcacheddotnet_clientlib_2.0,打开MemCachedClient.cs,增加如下方法:

 public Hashtable Stats(ArrayList servers, string command)
        {

            // get SockIOPool instance
            SockIOPool pool = SockIOPool.GetInstance(_poolName);

            // return false if unable to get SockIO obj
            if (pool == null)
            {
                //if(log.IsErrorEnabled)
                //{
                //    log.Error(GetLocalizedString("unable to get socket pool"));
                //}
                return null;
            }

            // get all servers and iterate over them
            if (servers == null)
                servers = pool.Servers;

            // if no servers, then return early
            if (servers == null || servers.Count <= 0)
            {
                //if(log.IsErrorEnabled)
                //{
                //    log.Error(GetLocalizedString("stats no servers"));
                //}
                return null;
            }

            // array of stats Hashtables
            Hashtable statsMaps = new Hashtable();

            for (int i = 0; i < servers.Count; i++)
            {

                SockIO sock = pool.GetConnection((string)servers[i]);
                if (sock == null)
                {
                    //if(log.IsErrorEnabled)
                    //{
                    //    log.Error(GetLocalizedString("unable to connect").Replace("$$Server$$", servers[i].ToString()));
                    //}
                    continue;
                }

                // build command
                if (command == null || command.Length == 0)
                {
                    command = "stats
";
                }
                else
                {
                    command = command + "
";
                }

                try
                {
                    sock.Write(UTF8Encoding.UTF8.GetBytes(command));
                    sock.Flush();

                    // map to hold key value pairs
                    Hashtable stats = new Hashtable();

                    // loop over results
                    while (true)
                    {
                        string line = sock.ReadLine();
                        //if(log.IsDebugEnabled)
                        //{
                        //    log.Debug(GetLocalizedString("stats line").Replace("$$Line$$", line));
                        //}

                        if (line.StartsWith(STATS))
                        {
                            string[] info = line.Split(' ');
                            string key = info[1];
                            string val = info[2];

                            //if(log.IsDebugEnabled)
                            //{
                            //    log.Debug(GetLocalizedString("stats success").Replace("$$Key$$", key).Replace("$$Value$$", val));
                            //}

                            stats[key] = val;

                        }
                        else if (line.StartsWith("ITEM"))
                        {

                            string[] info = line.Split('[');
                            string key = info[0].Split(' ')[1];
                            string val = "[" + info[1];

                            stats[key] = val;
                        }
                        else if (END == line)
                        {
                            // finish when we get end from server
                            //if(log.IsDebugEnabled)
                            //{
                            //    log.Debug(GetLocalizedString("stats finished"));
                            //}
                            break;
                        }

                        statsMaps[servers[i]] = stats;
                    }
                }
                catch//(IOException e)
                {
                    //if(log.IsErrorEnabled)
                    //{
                    //    log.Error(GetLocalizedString("stats IOException"), e);
                    //}

                    try
                    {
                        sock.TrueClose();
                    }
                    catch//(IOException)
                    {
                        //if(log.IsErrorEnabled)
                        //{
                        //    log.Error(GetLocalizedString("failed to close some socket").Replace("$$Socket$$", sock.ToString()));
                        //}
                    }

                    sock = null;
                }

                if (sock != null)
                    sock.Close();
            }

            return statsMaps;
        }

2.增加MemCached 配置信息类 MemCachedConfigInfo.cs

public class MemCachedConfigInfo
    {
        private bool _applyMemCached;
        /// <summary>
        /// 是否应用MemCached
        /// </summary>
        public bool ApplyMemCached
        {
            get
            {
                return _applyMemCached;
            }
            set
            {
                _applyMemCached = value;
            }
        }

        private string _serverList;
        /// <summary>
        /// 链接地址
        /// </summary>
        public string ServerList
        {
            get
            {
                return _serverList;
            }
            set
            {
                _serverList = value;
            }
        }

        private string _poolName;
        /// <summary>
        /// 链接池名称
        /// </summary>
        public string PoolName
        {
            get
            {
                return string.IsNullOrEmpty(_poolName) ? "ShoeSafeCycle_MemCached" : _poolName;
            }
            set
            {
                _poolName = value;
            }
        }

        private int _intConnections;
        /// <summary>
        /// 初始化链接数
        /// </summary>
        public int IntConnections
        {
            get
            {
                return _intConnections > 0 ? _intConnections : 3;
            }
            set
            {
                _intConnections = value;
            }
        }

        private int _minConnections;
        /// <summary>
        /// 最少链接数
        /// </summary>
        public int MinConnections
        {
            get
            {
                return _minConnections > 0 ? _minConnections : 3;
            }
            set
            {
                _minConnections = value;
            }
        }

        private int _maxConnections;
        /// <summary>
        /// 最大连接数
        /// </summary>
        public int MaxConnections
        {
            get
            {
                return _maxConnections > 0 ? _maxConnections : 5;
            }
            set
            {
                _maxConnections = value;
            }
        }

        private int _socketConnectTimeout;
        /// <summary>
        /// Socket链接超时时间
        /// </summary>
        public int SocketConnectTimeout
        {
            get
            {
                return _socketConnectTimeout > 1000 ? _socketConnectTimeout : 1000;
            }
            set
            {
                _socketConnectTimeout = value;
            }
        }

        private int _socketTimeout;
        /// <summary>
        /// socket超时时间
        /// </summary>
        public int SocketTimeout
        {
            get
            {
                return _socketTimeout > 1000 ? _maintenanceSleep : 3000;
            }
            set
            {
                _socketTimeout = value;
            }
        }

        private int _maintenanceSleep;
        /// <summary>
        /// 维护线程休息时间
        /// </summary>
        public int MaintenanceSleep
        {
            get
            {
                return _maintenanceSleep > 0 ? _maintenanceSleep : 30;
            }
            set
            {
                _maintenanceSleep = value;
            }
        }

        private bool _failOver;
        /// <summary>
        /// 链接失败后是否重启,详情参见http://baike.baidu.com/view/1084309.htm
        /// </summary>
        public bool FailOver
        {
            get
            {
                return _failOver;
            }
            set
            {
                _failOver = value;
            }
        }

        private bool _nagle;
        /// <summary>
        /// 是否用nagle算法启动socket
        /// </summary>
        public bool Nagle
        {
            get
            {
                return _nagle;
            }
            set
            {
                _nagle = value;
            }
        }
    }

3.增加MemCached 管理基类

public sealed class MemCachedManager
    {

        #region 静态方法和属性
        private static MemcachedClient mc = null;

        private static SockIOPool pool = null;

        private static MemCachedConfigInfo memCachedConfigInfo = new MemCachedConfigInfo();

        private static string[] serverList = { System.Configuration.ConfigurationManager.AppSettings["Base_URL_Memcached"].ToString() };
        #endregion

        static MemCachedManager()
        {
            CreateManager();
        }

        private static void CreateManager()
        {
            pool = SockIOPool.GetInstance(memCachedConfigInfo.PoolName);
            pool.SetServers(serverList);
            pool.InitConnections = memCachedConfigInfo.IntConnections;//初始化链接数
            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;
        }

        /// <summary>
        /// 缓存服务器地址列表
        /// </summary>
        public static string[] ServerList
        {
            set
            {
                if (value != null)
                    serverList = value;
            }
            get { return serverList; }
        }

        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static MemcachedClient CacheClient
        {
            get
            {
                if (mc == null)
                    CreateManager();

                return mc;
            }
        }

        public static void Dispose()
        {
            if (pool != null)
                pool.Shutdown();
        }

        /// <summary>
        /// 获取服务器端缓存的数据信息
        /// </summary>
        /// <param name="serverArrayList">要访问的服务列表</param>
        /// <param name="statsCommand">此参数的功能暂时无效</param>
        /// <param name="param">此参数的功能暂时无效</param>
        /// <returns>返回信息</returns>
        public static IList<string> GetStats(string[] serverArrayList, MemcachedStats statsCommand, string param)
        {
            IList<string> statsArray = new List<string>();
            if (param == null)
                param = "";
            else
            {
                param = param.Trim().ToLower();
            }

            string commandstr = "stats";
            //转换stats命令参数
            switch (statsCommand)
            {
                case MemcachedStats.Reset: { commandstr = "stats reset"; break; }
                case MemcachedStats.Malloc: { commandstr = "stats malloc"; break; }
                case MemcachedStats.Maps: { commandstr = "stats maps"; break; }
                case MemcachedStats.Sizes: { commandstr = "stats sizes"; break; }
                case MemcachedStats.Slabs: { commandstr = "stats slabs"; break; }
                case MemcachedStats.Items: { commandstr = "stats items"; break; }//此处原先是返回stats
                case MemcachedStats.CachedDump:
                    {
                        string[] statsparams = param.Split(' ');
                        if (statsparams.Length == 2)

                            commandstr = "stats cachedump  " + param;

                        break;
                    }
                case MemcachedStats.Detail:
                    {
                        if (string.Equals(param, "on") || string.Equals(param, "off") || string.Equals(param, "dump"))
                            commandstr = "stats detail " + param.Trim();

                        break;
                    }
                default: { commandstr = "stats"; break; }
            }

            ArrayList arr = new ArrayList(serverArrayList);

            Hashtable stats = CacheClient.Stats(arr, commandstr);

            foreach (string key in stats.Keys)
            {
                statsArray.Add("server:__:" + key);//此处也改了
                Hashtable values = (Hashtable)stats[key];
                foreach (string key2 in values.Keys)
                {
                    statsArray.Add(key2 + ":" + values[key2]);
                }
            }
            return statsArray;
        }

        /// <summary>
        /// Stats命令行参数
        /// </summary>
        public enum MemcachedStats
        {
            /// <summary>
            /// stats : 显示服务器信息, 统计数据等
            /// </summary>
            Default = 0,
            /// <summary>
            /// stats reset : 清空统计数据
            /// </summary>
            Reset = 1,
            /// <summary>
            /// stats malloc : 显示内存分配数据
            /// </summary>
            Malloc = 2,
            /// <summary>
            /// stats maps : 显示"/proc/self/maps"数据
            /// </summary>
            Maps = 3,
            /// <summary>
            /// stats sizes
            /// </summary>
            Sizes = 4,
            /// <summary>
            /// stats slabs : 显示各个slab的信息,包括chunk的大小,数目,使用情况等
            /// </summary>
            Slabs = 5,
            /// <summary>
            /// stats items : 显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)
            /// </summary>
            Items = 6,
            /// <summary>
            /// stats cachedump slab_id limit_num : 显示某个slab中的前 limit_num 个 key 列表
            /// </summary>
            CachedDump = 7,
            /// <summary>
            /// stats detail [on|off|dump] : 设置或者显示详细操作记录   on:打开详细操作记录  off:关闭详细操作记录 dump: 显示详细操作记录(每一个键值get,set,hit,del的次数)
            /// </summary>
            Detail = 8
        }

        /// <summary>
        /// 获取所有缓存键
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllKeys(string prefix_key)
        {
            IList<int> idList = new List<int>();
            IList<string> list = GetStats(serverList, MemcachedStats.Items, null);
            foreach (var item in list)
            {
                string[] tmpArr = item.Split(':');
                if (tmpArr.Length > 1)
                {
                    int itemID = 0;
                    if (tmpArr[1] == "__") continue;

                    int.TryParse(tmpArr[1], out itemID);
                    if (itemID <= 0) continue;

                    bool find = false;
                    foreach (int item1 in idList)
                    {
                        if (item1 == itemID)
                        {
                            find = true;
                            break;
                        }
                    }

                    if (!find)
                    {
                        idList.Add(itemID);
                    }
                }
            }

            List<string> keys = new List<string>();
            foreach (int item in idList)
            {
                IList<string> cachearr = GetStats(serverList, MemcachedStats.CachedDump, item + " 0");
                foreach (string itemCache in cachearr)
                {
                    string[] tmpArr = itemCache.Split(':');
                    if (tmpArr.Length > 1)
                    {
                        if (tmpArr[1] == "__")
                        {
                            continue;
                        }

                        if (tmpArr[0].Contains(prefix_key))
                            keys.Add(tmpArr[0]);
                    }
                }
            }

            return keys;
        }
    }

4.调用

   List<string> list = MemCachedManager.GetAllKeys("ShoeSafeCycle_");

PS. 参考文章地址:

http://www.cnblogs.com/suger/archive/2011/08/11/2134548.html

http://www.cnblogs.com/daizhj/archive/2009/03/23/1386652.html

原文地址:https://www.cnblogs.com/FH-cnblogs/p/4353865.html