.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\r\n";
                }
                else
                {
                    command = command + "\r\n";
                }

                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;
        /// 
        /// 是否应用MemCached
        /// 
        public bool ApplyMemCached
        {
            get
            {
                return _applyMemCached;
            }
            set
            {
                _applyMemCached = value;
            }
        }

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

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

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

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

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

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

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

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

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

        private bool _nagle;
        /// 
        /// 是否用nagle算法启动socket
        /// 
        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;
        }

        /// 
        /// 缓存服务器地址列表
        /// 
        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();
        }

        /// 
        /// 获取服务器端缓存的数据信息
        /// 
        /// 要访问的服务列表
        /// 此参数的功能暂时无效
        /// 此参数的功能暂时无效
        /// 返回信息
        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;
        }

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

        /// 
        /// 获取所有缓存键
        /// 
        /// 
        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

你可能感兴趣的:(memcached)