遍历Memcached服务器的KEY列表

2009年11月25日  |  4:58 下午分类:Develop  |  标签:  |  

Memcached.ClientLibrary没有提供获取所有缓存KEY的方法,但有时候,我们又需要得知当前Memcached服务器上有哪些缓存key。

于是,我修改了Memcached.ClientLibrary源代码,实现了该需求。

主要通过发送stats items命令和stats cachedump x x来实现,可以先telnet上去看看

ScreenShot00068

摸黑输入 stats items回车

ScreenShot00069

STAT items:7:number 1
表示item7这个内存块中有一个hashkey,用stats cachedump 7 0命令即可查看

ScreenShot00070

OK,开始编码吧

//更多代码
static Regex ItemReg = new Regex(@"items:(\d+):number\s+(\d+)", RegexOptions.IgnoreCase);

		public List<string> KeyMaps()
		{
			List<string> list = new List<string>();
			List<string> commands = new List<string>();

			SockIOPool pool = SockIOPool.GetInstance(_poolName);
			ArrayList servers;

			if (pool == null)
			{
				if (log.IsErrorEnabled)
				{
					log.Error(GetLocalizedString("unable to get socket pool"));
				}
				return null;
			}
		   servers = pool.Servers;

			if (servers == null || servers.Count <= 0)
			{
				if (log.IsErrorEnabled)
				{
					log.Error(GetLocalizedString("stats no servers"));
				}
				return null;
			}
			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;
				}

				string command = "stats items\r\n";

				try
				{
					sock.Write(UTF8Encoding.UTF8.GetBytes(command));
					sock.Flush();

					int lineNum = 0;
					int num = 0;
					Match match = null;
					while (true)
					{
						string line = sock.ReadLine();
						if (line == END) break;
						match = ItemReg.Match(line);
						if (match.Length>0)
						{
							lineNum = Convert.ToInt32(match.Groups[1].Value);
							num = Convert.ToInt32(match.Groups[2].Value);
							command = string.Format("stats cachedump {0} 0\r\n", lineNum);
							if (commands.Contains(command) == false)
								commands.Add(command);

						}
					}

					foreach (string item in commands)
					{
						sock.Write(UTF8Encoding.UTF8.GetBytes(item));
						sock.Flush();
						while (true)
						{
							string tmpline = sock.ReadLine();
							if (tmpline == END) break;
							if (tmpline!=ERROR)
							{
								string[] array = tmpline.Split(' ');
								if (!list.Contains(array[1]))
								{
									list.Add(array[1]);
								}
							}

						}
					}

				}
				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 list;
		}
//更多代码

只需调用mc.KeyMaps()方法即可。

转载时务必以超链接形式标明文章原始出处和作者信息。

相关文章

  • 暂无相关日志

3 位领导已批示 关于 “遍历Memcached服务器的KEY列表”

  1. overred 发表于: 十二月 21st, 2009 1:51 下午

    去用用Shuttler.Net的Memcached吧。。。
    http://shuttler.codeplex.comface:17

    [回复Ta]

    overred 回复于:十二月 21st, 2009 1:51 下午

    @overred,
    yun,
    http://shuttler.codeplex.com

    [回复Ta]

    幻想曲 回复于:十二月 21st, 2009 3:09 下午

    @overred,
    很NB15

    [回复Ta]


发表您的评论

2412151751091918111201478131663