.NET Memcached Client 扩展获取所有缓存Key
.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 > )
{
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('[') - );
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 = ;
private const int _keyIndex = ;
private readonly byte[] _commandStatsItems = UTF8Encoding.UTF8.GetBytes("stats items\r\n");
#endregion
}
}
.NET Memcached Client 扩展获取所有缓存Key的更多相关文章
- Memcached Client的释疑
1.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的.与之对应的带d的memcached是建立在libmemcached的基础 ...
- 分布式缓存系统Memcached简介与实践(.NET memcached client library)
缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...
- memcached能获取所有的key吗
memcached能获取所有的key吗 Memcache 查看列出所有key方法 Memcached中获取所有的key 特别要注意:memcached保存的值需要序列化,否则是无法保存的,而且是不会报 ...
- Memcached通用类(基于Memcached Client Library)
分享下自己编写的Memcached通用类.欢迎大家帮忙指点下哈~ 使用的是.NET memcached client library 客户端+Memcached Providers using Sys ...
- Memcached Client 使用手册
Memcached Client 使用手册 Author: cenwenchu Email: wenchu.cenwc@alibaba-inc.com Blog:http://blog.csdn.ne ...
- memcached基于socket访问memcache缓存服务器
memcached基于socket访问memcache缓存服务器 操作memcache常用三种方法: .memcache基于php_memcache.dll扩展(php扩展) .memcached基于 ...
- memcached实例(enyim.com Memcached Client)
在上一篇文章,我们讲了,为什么要使用memched做为缓存服务器(没看的同学请点这里).下面让我们以memcached-1.2.1-win32版本的服务组件(安装后是以一个windows服务做daem ...
- .NET Core MemoryCache缓存获取全部缓存键
在Core中不能使用原HttpRuntime.Cache缓存,改为MemoryCache(Microsoft.Extensions.Caching.Memory). 现MemoryCache新版为2. ...
- Memcached集群:Magent缓存代理使用
小结: 先启动memcached 然后启动magent memcached -d -p 11211 -u memcached -m 64 -c 5120 memcached -d -p 11212 - ...
随机推荐
- 【C/C++】assert
摘自http://www.cplusplus.com assert : macro #include <assert.h> void assert (int expression); 如果 ...
- django autocommit的一个坑,读操作的事务占用导致锁表
版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/580618150/ 缘由:有一个django守护进程Daemon ...
- bzoj 1185 最小矩形覆盖 —— 旋转卡壳
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 枚举一条边,维护上.左.右方的点: 上方点到这条边距离最远,所以用叉积求面积维护: 左 ...
- Centos6.5上的iptables
1.Centos6.5默认开启了iptables 当Centos6.5上安装了MySQL后,在远程连接它,如果出现10060的错误,说明iptables在起作用. 关闭iptables即可,sudo ...
- 基于ftp服务实现yum网络共享
安装ftp服务:yum install vsftpd 安装后: CentOS7 启动服务:systemctl start vsftpd 设置开机启动:systemctl enable vsftpd 同 ...
- paramiko使用1
- C++中的友元小结
我们知道,在一个类总可以有公有的(public)成员和私有的(private)成员.在类外可以访问公用成员,只有本类中的函数可以访问本类的私有成员. 现在,我们学习一种新的情况--友元. 在C++中, ...
- KickStart安装CentOS,同时安装和配置hadoop
声明:这篇文章是前面是拾人牙慧,我是结合 http://www.111cn.net/sys/linux/59969.htm 和 http://www.cnblogs.com/mchina/p/cent ...
- RStudio版本管理 整合Git
本文为原创,转载注明出处. 系统环境: win7 x64 R-3.1.0-win.exe RStudio-0.98.507.exe 前置条件:必须拥有github仓库: 如:https://githu ...
- 27.【转载】挖洞技巧:如何绕过URL限制
大家对URL任意跳转都肯定了解,也知道他的危害,这里我就不细说了,过~ 大家遇到的肯定都是很多基于这样的跳转格式:http://www.xxx.xxx/xxx?xxx=http://www.xxx.x ...