惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 老D

SQL Server 2005 中新CTE语法 递归性能测试 合并 GridView 的单元格 在Crystal Report中将数字转为英文 连接远程服务器共享 在代码中恢复sql server 数据库 - 老D 获取同一网段内的SQL SERVER实例 C#动态加载DLL Asp.net 文件下载 在网页处理按键事件 - 老D - 博客园 SQL语句导入导出大全 跨应用程序进行 Forms 身份验证 GridView导出Excel ASP.NET数据库连接字符串的加密与解密 ASP.NET中GridView动态绑定数据实现编辑更新 ASp.NET 2.0中Page事件的执行顺序 批量insert数据 简繁转换 ASP.NET应用程序开发 经典算法-C#四种排序算法
.NET Memcached Client 扩展获取所有缓存Key
老D · 2014-04-20 · via 博客园 - 老D

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

下载源码

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
    }
}