使用组件
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 />");
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]