Use Components
Memcached 1.2.6
. Net class library memcacheddotnet_clientlib-1.1.5
1. Add memcacheddotnet_clientlib-1.1.5 code
Download the component, open the. NET class library memcacheddotnet_clientlib-1.1.5 with vs, open memcachedclient. CS, add the following method:
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. Read this article http://www.cnblogs.com/daizhj/archive/2009/03/23/1386652.html
The getstats method is available in this article. modify it as follows:
/// <Summary> /// obtain the server-side cached data // </Summary> /// <Param name = "serverarraylist"> list of services to be accessed </param> /// <Param name = "statscommand"> the function of this parameter is temporarily invalid </param> /// <Param name = "Param"> the function of this parameter is temporarily invalid </param> // <returns> returned information </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"; // convert the stats command parameter 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;} // originally returned 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. Add the following method.
/// <Summary> /// obtain all cache keys /// </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 ;}
Call Method
IList<string> list = MemcachedManager.GetAllKeys(); foreach (var item in list) { Response.Write(item + "<br />"); }