.net/c# memcached缓存获取所有缓存键的方法步骤
程序员文章站
2024-02-26 18:32:46
使用组件memcached 1.2.6.net 类库 memcacheddotnet_clientlib-1.1.5
1.增加memcacheddotnet_client...
使用组件
memcached 1.2.6
.net 类库 memcacheddotnet_clientlib-1.1.5
1.增加memcacheddotnet_clientlib-1.1.5代码
下载好组件后,用vs打开.net类库memcacheddotnet_clientlib-1.1.5,打开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 文章中有getstats方法,将它修改如下:
复制代码 代码如下:
/// <summary>
/// 获取服务器端缓存的数据信息
/// </summary>
/// <param name="serverarraylist">要访问的服务列表</param>
/// <param name="statscommand">此参数的功能暂时无效</param>
/// <param name="param">此参数的功能暂时无效</param>
/// <returns>返回信息</returns>
public static ilist<string> getstats(ilist<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)
if (param.isintarr(' '))// utils.isnumericarray(statsparams)
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.toarray());
hashtable stats = memcachedmanager.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;
}
3.最后增加如下方法
复制代码 代码如下:
/// <summary>
/// 获取所有缓存键
/// </summary>
/// <returns></returns>
public static ilist<string> getallkeys()
{
ilist<int> idlist = new list<int>();
ilist<string> list = memcachedmanager.getstats(memcachedmanager.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);
}
}
}
ilist<string> keys = new list<string>();
foreach (int item in idlist)
{
ilist<string> cachearr = memcachedmanager.getstats(memcachedmanager.serverlist, memcachedstats.cacheddump, item + " 0");
foreach (string itemcache in cachearr)
{
string[] tmparr = itemcache.split(':');
if (tmparr.length > 1)
{
if (tmparr[1] == "__")
{
continue;
}
keys.add(tmparr[0]);
}
}
}
return keys;
}
调用方法
复制代码 代码如下:
ilist<string> list = memcachedmanager.getallkeys();
foreach (var item in list)
{
response.write(item + "<br />");
}
上一篇: 修改MYSQL密码的几种常用方法总结