42

Are there any tools to explore what is currently cached inside a memcached pool? Not some much graphs, but the actual key/values currently stored.

Tom
  • 736
  • 2
  • 9
  • 23

7 Answers7

23

The correct answer would be echo "stats cachedump SLABS_ID LIMIT" | nc HOSTNAME PORT

eg. echo "stats cachedump 15 4" | nc 127.0.0.1 11211

This would give the output on the lines of:

ITEM cache_path-comments%2Fpage%2F2 [2211 b; 1337195558 s]
ITEM cache_path-comments%2Fpage%2F5 [2205 b; 1337195558 s]
ITEM cache_path-comments%2Fpage%2F6 [2179 b; 1337195558 s]
ITEM cache_path-comments [2164 b; 1337195558 s]
END

Note: This is an undocumented command that is not supported by the memcached team and can be removed in any version. For the complete reference, check out Understanding Memcached stats cachedump command.

Jagtesh Chadha
  • 330
  • 2
  • 6
  • cachedump is limited to an undocumented amount of data, not greated than 1-2mb so it's not reliable – John Feb 08 '19 at 02:43
17

memcached-tool

In the recent version of memcached there is also memcached-tool perl script, e.g. usage:

memcached-tool localhost:11211 dump | less

which dumps all keys and values.

memdump

To dump a list of keys from a server, use memcdump/memdump tool, e.g.

memcdump --servers=localhost 

To dump all objects:

memcdump --servers=localhost | xargs -L1 -I% sh -c 'echo "get %" | nc localhost 11211'

To dump all key values into separate files:

while read -r key; do [ -f "$key" ] || echo "get $key" | nc localhost 11211 > "$key.dump"; done < <(memcdump --server localhost)

memccat

To print a key value, you can use memccat command, e.g.

memccat CACHE-KEY

Bash

To dump all keys in Bash shell, try:

exec {memcache}<>/dev/tcp/localhost/11211; printf "stats items\nquit\n" >&${memcache}; cat <&${memcache}

netcat

Here is example to get value of single item using netcat:

echo "get 13456_-cache-some_object" | nc 127.0.0.1 11211

Python

See: How to export all keys and values from memcached with Python?

kenorb
  • 5,943
  • 1
  • 44
  • 53
  • 2
    This answer helped me find the _actual_ memcached tool : **memcdump**. See this answer : http://stackoverflow.com/a/14491419/720360 – mmuller Jun 24 '15 at 08:38
  • 1
    I first had to `apt install libmemcached-tools`, but then it still wasn't on my path, so `/usr/share/memcached/scripts/memcached-tool localhost:11211 dump`. Ubuntu 18. – Camille Goudeseune Feb 15 '19 at 22:25
  • On Mac OS X you can install `memdump` with `brew install libmemcached` – Chris Bloom Jun 24 '21 at 16:39
9

Try stats items - i.e.

echo "stats items" | nc 127.0.0.1 11211
danlefree
  • 2,873
  • 1
  • 18
  • 20
7

Try using telnet command, e.g.:

$ telnet 0 11211
stats
stats items
set key 1 23 8
get key
kenorb
  • 5,943
  • 1
  • 44
  • 53
silviud
  • 2,677
  • 2
  • 16
  • 19
7

Install libmemcached-tools and then you can use this command to get all the keys:

memcdump --server=127.0.0.1

To see the contents, you can ask for a specific key:

memccat --server=127.0.0.1 SOME_KEY

or you can loop over the list of keys in Bash:

for key in $(memcdump --server=127.0.0.1); do echo ------ $key ------; memccat --server=127.0.0.1 $key; done

Make sure to use 127.0.0.1 (if you are running the command on the same host) because localhost does not seem to work.

Tobia
  • 1,143
  • 1
  • 12
  • 19
2

In basic memcached I do not think there is any way of querying for keys stored in the server. The only think you can get is statistics related to storage and stored items.

Some vendors that have developed memcached compliant solutions, e.g. Gear6, have however added functionality to allow querying.

Chris
  • 181
  • 3
0
for key in $(memcdump --server=127.0.0.1);
  do echo -n "KEY :  $key ------> VALUE : ";
  memccat --server=127.0.0.1 $key;
done
Glorfindel
  • 1,213
  • 3
  • 15
  • 22