首页 > Web开发 > 详细

.NET Memcached Client 扩展获取所有缓存Key

时间:2014-04-20 15:49:45      阅读:512      评论:0      收藏:0      [点我收藏+]

  .NET Memcached Client默认实现中并没有获取所有已经缓存Key的方法,但在业务中有时候需求中需要通过正则删除符合条件的缓存内容,所以就要通过读取已经缓存Key进行相关的匹配,然后删除。通过flush只会让缓存过容过期,但获取Key时还会获取得到,缓存的内容过期同样。但调用Get(key)后,Key才会删除

 

下载源码

 

bubuko.com,布布扣
namespace Memcached.ClientLibrary
{
    public partial class MemcachedClient
    {

        #region Methods
        public void DeleteByPattern(string pattern)
        {
            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled);
            var keysToRemove = new List<String>();

            foreach (var item in this.Keys)
                if (regex.IsMatch(item))
                    keysToRemove.Add(item);

            foreach (var key in keysToRemove)
                Delete(key);
        }
        #endregion

        #region Property
        /// <summary>
        /// 获取所有缓存的Key
        /// </summary>
        public List<String> Keys
        {
            get
            {
                List<String> keys = new List<string>();

                SockIOPool pool = SockIOPool.GetInstance(_poolName);
                if (pool != null && pool.Servers != null && pool.Servers.Count > 0)
                {
                    foreach (var server in pool.Servers)
                    {
                        var sock = pool.GetConnection((string)server);
                        if (sock == null) continue;

                        try
                        {
                            sock.Write(_commandStatsItems);
                            sock.Flush();
                            string line;
                            var items = new List<string>();
                            while (!END.Equals((line = sock.ReadLine()), StringComparison.Ordinal))
                            {
                                var id = line.Substring(_idIndex, line.LastIndexOf(:) - _idIndex);
                                if (!items.Contains(id))
                                    items.Add(id);
                            }

                            foreach (var id in items)
                            {
                                sock.Write(UTF8Encoding.UTF8.GetBytes(string.Concat("stats cachedump ", id, " 0\r\n")));
                                sock.Flush();

                                while (!END.Equals((line = sock.ReadLine()), StringComparison.Ordinal))
                                {
                                    var key = line.Substring(_keyIndex, line.LastIndexOf([) - 6);
                                    keys.Add(key);
                                }
                            }
                        }
                        catch
                        {
                            try
                            {
                                sock.TrueClose();
                            }
                            catch(IOException)
                            {
                                if(log.IsErrorEnabled)
                                    log.Error(GetLocalizedString("failed to close some socket").Replace("$$Socket$$", sock.ToString()));  
                            }
                        }
                        finally
                        {
                            if (sock != null)
                                sock.Close();
                        }

                    }


                }

                return keys;
            }
        }
        #endregion

        #region Fields
        private const int _idIndex = 11;
        private const int _keyIndex = 5;
        private readonly byte[] _commandStatsItems = UTF8Encoding.UTF8.GetBytes("stats items\r\n");
        #endregion
    }
}
bubuko.com,布布扣

 

.NET Memcached Client 扩展获取所有缓存Key,布布扣,bubuko.com

.NET Memcached Client 扩展获取所有缓存Key

原文:http://www.cnblogs.com/elzero/p/Memcached_Client_Extensions_All_Keys.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!