Some debugging code

Randy Wigginton krw at
Tue Oct 31 16:54:40 UTC 2006

Many times someone has asked this list "How can I see all the keys in  
my memcache server?", and the response is always, "You can't, that  
isn't the point, etc."  First, in general I agree that memcache  
operations need to be extremely fast and not run the risk of tying up  
the server for any length of time.  However, when debugging, I am not  
hitting the server very hard, and I've encountered numerous times  
when I needed to find out what was in the server.  So, if you need to  
see what your server knows about, you can add this code into the main  
command handler in memcached.c, and then you will be able to send the  
"keys" command to get a listing of all keys in that server.

This may not help anyone,and this is not recommended to be used in a  
high-volume production server.  YMMV.  First, add the following line  
to assoc.c:
item ** getHashtable() { return hashtable; }

Then add this code to memcached.c, its pretty obvious where it belongs.

     if (strcmp(command, "keys") == 0) {
         extern item** getHashtable();
         item **hashtable = getHashtable();
         unsigned long i;
         for (i=0; i<1<<20/*HASHPOWER*/; i++) {
             item *it = hashtable[i];
			int outOfMem = 0;
             while (it && !outOfMem) {
                 char *key = ITEM_key(it);
				outOfMem = add_iov(c, key, strlen(key));
				outOfMem += add_iov(c, "\r\n", 2);
                 it = it->h_next;
         add_iov(c, "END\r\n", 5);

		conn_set_state(c, conn_write);
		c->write_and_go = conn_read;

Since this code is NOT a part of the true memcache release, and has  
not been tested, please be cautious with this.  Good debugging!

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the memcached mailing list