Working with Memcached via CLI

12 Jul 2014 in TIL

A lot of the time when I'm working on a project that uses memcached, I'll want to have a look at the data in the cache without using my application to make sure that it's doing what I expect. I didn't expect that wanting to list all keys on a memcached server was such a big request, but it looked like there wasn't any easy way to do it.

I did start a project named mempp to provide a CLI friendly interface to memcached via the Python bindings, but as it turns out you don't even need this. libmemcached ships with everything you need.

First, you'll need to install libmemcached-tools

bash
sudo apt-get install libmemcached-tools

Then, if you want to list all keys stored on a server, you can use memcdump

bash
$ memcdump --servers localhost:11211
key_one
key_two_123

If you want to see the values of those keys, use memccat

bash
$ memccat --servers localhost:11211 key_one
FooBar

If you want to remove a key, use memcrm

bash
$ memcrm --servers localhost:11211 key_one

With a combination of memcdump and memccat, plus memcrm you can poke around your memcached instance without needing to use telnet or list slabs or anything like that, which most posts advise you to do.