Memcached本身对外提供的命令不多,也就add、get、set、incr、decr、replace、delete、stats等几个,客户端对这些操作进行了封装,总体来说调用还是很简单的。 初看了下EnyimMemcached结构,所有的操作都从Operation类继承,每个子类都实现自己的ExcuteAction

 
      操作:新窗口查看图片  
 
 
 

所有的操作都由MemCachedClient这个门面提供

 
      操作:新窗口查看图片  
 
 
 

在使用过程中,我需要遍历当前缓存,获取所有键值,EnyimMemCached却没有提供遍历的方法。在网上查了下,http://www.cnblogs.com/sunli/archive/2008/11/01/1324153.html 提供了遍历的思路,很简单,用"stats item"与"stats cachedump 1 0"命令即可实现遍历,我要做的只是把两个命令封装成上面提到的Operation,下面是实现代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; namespace Enyim.Caching.Memcached
{
/// <summary>
/// 扩展的获取所有键值操作 by wm
/// </summary>
internal class CacheDumpOperation : Operation
{
public CacheDumpOperation(ServerPool pool, string regex) : base(pool)
{
this.regexquery = regex;
} private ArrayList results;
/// <summary>
/// 结果键值集合
/// </summary>
public ArrayList Results
{
get { return results; }
} private string regexquery = "";
/// <summary>
/// 查询条件正则表达式
/// </summary>
public string RegexQuery
{
get { return regexquery; }
set { regexquery = value; }
} protected override bool ExecuteAction()
{
List<string> lstParams = new List<string>();
ArrayList arrKeys = new ArrayList(); foreach (MemcachedNode server in this.ServerPool.WorkingServers)
{
using (PooledSocket ps = server.Acquire()) //step1
{
if (ps != null)
{
ps.SendCommand("stats items");
while (true)
{
string line = ps.ReadResponse(); if (String.Compare(line, "END", StringComparison.Ordinal) == )
break; if (line.Length < || String.Compare(line, , "STAT ", , , StringComparison.Ordinal) != )
{
continue;
} string para = line.Split(':')[];
if (!lstParams.Contains(para))
{
lstParams.Add(para);
}
}
} } using (PooledSocket psDump = server.Acquire()) //step2
{
if (psDump != null)
{
foreach (string para in lstParams)
{
psDump.SendCommand(string.Format("stats cachedump {0} {1}", para, ));
while (true)
{
string lineDump = psDump.ReadResponse();
if (String.Compare(lineDump, "END", StringComparison.Ordinal) == )
break; if (lineDump.Length < || String.Compare(lineDump, , "ITEM ", , , StringComparison.Ordinal) != )
{
continue;
} string key = lineDump.Split(' ')[];
if (this.regexquery == "")
{
arrKeys.Add(key);
}
else
{
if (Regex.IsMatch(key, this.regexquery))
{
arrKeys.Add(key);
}
}
}
}
}
} } this.results = arrKeys; return true; }
}
}
</string></string>

在MemcachedClient中添加方法

/// <summary>
/// 获取所有键值集合
/// </summary>
/// <returns></returns>
public System.Collections.ArrayList GetKeys()
{
using (CacheDumpOperation g = new CacheDumpOperation(this.pool, ""))
{
g.Execute();
return g.Results;
}
} /// <summary>
/// 根据正则查找匹配缓存键值集合
/// </summary>
/// <param name="regex">
/// <returns></returns>
public System.Collections.ArrayList GetKeys(string regex)
{
using (CacheDumpOperation g = new CacheDumpOperation(this.pool, regex))
{
g.Execute();
return g.Results;
}
}

EnyimMemcached扩展 遍历功能的更多相关文章

  1. BrnShop开源网上商城第六讲:扩展视图功能

    在正式讲解扩展视图功能以前,我们有必要把视图的工作原理简单说明下.任何一个视图都会被翻译成一个c#类,并保存到指定的位置,然后被编译.这也就是为什么能在视图中包含c#代码片段的原因.下面我们通过一个项 ...

  2. 关于ligerui 中 grid 表格的扩展搜索功能在远程数据加载时无法使用的解决办法

    要想使用grid里的扩展搜索功能,除了要引用ligerui主要的js文件外,还必须引入下面的JS文件: 1.Source\demos\filter\ligerGrid.showFilter.js 2. ...

  3. Java基础知识强化之集合框架笔记17:List集合的特有的遍历功能

    1. List集合的特有遍历功能: size()和 get()方法结合使用 2. 代码示例: package cn.itcast_03; import java.util.ArrayList; imp ...

  4. php扩展Redis功能

    php扩展Redis功能 1 首先,查看所用php编译版本V6/V9 在phpinfo()中查看 2 下载扩展 地址:https://github.com/nicolasff/phpredis/dow ...

  5. DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件

    一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...

  6. 利用Mixins扩展类功能

    8.18 利用Mixins扩展类功能 - python3-cookbook 3.0.0 文档 https://python3-cookbook.readthedocs.io/zh_CN/latest/ ...

  7. 适用于 Windows 的虚拟机扩展和功能

    Azure 虚拟机扩展是小型应用程序,可在Azure 虚拟机上提供部署后配置和自动化任务. 例如,如果虚拟机要求安装软件.防病毒保护或进行 Docker 配置,便可以使用 VM 扩展来完成这些任务. ...

  8. 基于web的网上书城系统开发-----登录注册扩展-------验证码功能

    public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; privat ...

  9. 新增扩展程序功能打包提交新版 WARNING ITMS-90473 警告问题

    1.问题描述 自从在主应用中加入SiriShortCut功能之后,打包程序上传至 iTunes Connect 就会出现警告,看其原因描述是CFBundleVersion主应用与子应用的不一致导致的 ...

随机推荐

  1. ubuntu Linux离线安装软件包

    ubuntu Linux离线安装软件包 http://www.myir-tech.com/bbs/thread-337-1-1.html(出处: 米尔科技论坛) 方法一 在可上网的ubuntu电脑上, ...

  2. 【转】通过 ulimit 改善系统性能

    概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...

  3. 一些常用运行命令和CMD命令

    运行命令 1. 进入服务页面的命令: services.msc 2. 远程连接命令:mstsc.exe 3. 配置电脑启动项 msconfig 4. 计算器 calc.exe 5. 设定关机时间(se ...

  4. zb的生日(暴搜dfs)

    zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...

  5. ChromiumFX中js调用C#方法

    server端代码: ChromiumWebBrowser wb; wb.AddGlobalJSFunction("CfxHelloWorld").Execute += CfxHe ...

  6. C# ?? 操作符示例

    static int? GetNullableInt() { return null; } static string GetStringValue() { return null; } static ...

  7. XPath语法 在C#中使用XPath示例

    XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: <?xml version="1.0" en ...

  8. hibernate总结一

    在hibernate中查询使用List,Map和类对象定制返回类型   在使用hibernate进行查询时,使用得最多的还是通过构建hql进行查询了.在查询的过程当中,除使用经常的查询对象方法之外,还 ...

  9. 配置nginx静态资源路径

    1.修改nginx.conf文件 server节点下location节点的 root目录修改 location / { root html; index index.html index.htm; } ...

  10. maven命令/依赖/聚合

    一,编写pom.xml   首先我们看一下pom.xml   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...