c#memcached缓存获取所有缓存键的方法步骤-创新互联
这篇文章主要讲解了“c# memcached缓存获取所有缓存键的方法步骤”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“c# memcached缓存获取所有缓存键的方法步骤”吧!
创新互联公司是一家集网站建设,黄陵企业网站建设,黄陵品牌网站建设,网站定制,黄陵网站建设报价,网络营销,网络优化,黄陵网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。使用组件
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方法,将它修改如下:
复制代码 代码如下:
///
/// 获取服务器端缓存的数据信息
///
/// 要访问的服务列表
/// 此参数的功能暂时无效
/// 此参数的功能暂时无效
///
public static IList
{
IList
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.最后增加如下方法
复制代码 代码如下:
///
/// 获取所有缓存键
///
///
public static IList
{
IList
IList
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
foreach (int item in idList)
{
IList
foreach (string itemCache in cachearr)
{
string[] tmpArr = itemCache.Split(':');
if (tmpArr.Length > 1)
{
if (tmpArr[1] == "__")
{
continue;
}
keys.Add(tmpArr[0]);
}
}
}
return keys;
}
调用方法
复制代码 代码如下:
IList
foreach (var item in list)
{
Response.Write(item + "
");
}
感谢各位的阅读,以上就是“c# memcached缓存获取所有缓存键的方法步骤”的内容了,经过本文的学习后,相信大家对c# memcached缓存获取所有缓存键的方法步骤这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!
网站栏目:c#memcached缓存获取所有缓存键的方法步骤-创新互联
分享链接:http://pwwzsj.com/article/dcgiii.html