Mike
Mike

Reputation: 7914

How do I view the data in memcache?

I've installed memcache and now how do I really view the data in memcache?

Is there any way to see the data present in cache inside memcache?

How do I really know whether memcache is getting data stored inside it?

Note: I don't want to write any program to see the data inside memcache. Basically, memcache server is already installed in my environment and it is caching the data as well. But I would like to know if there are any utility programs available which will show me the cached data inside memcache or if there is any command which will show me the data cached so far.

Upvotes: 18

Views: 43471

Answers (4)

MicGer
MicGer

Reputation: 385

As of 1.4.31 per the release notes, memcache provides the lrw_crawler metadump all command to view all keys in the cache. Note that memcache must be started with the -o modern flag to enable this feature.

Connect to memcache using telnet:

telnet <host> 11211

Then enter

lru_crawler metadump all

Memcache will respond with

key=mykey1 exp=1736890705 la=1736889706 cas=2 fetch=no cls=1 size=66 flags=0
... repeated for each key
END

This command is more reliable than the stats cachedump command mentioned in other answers, which may not return all keys in the slab. See Is memcached’s cachedump expected to be consistent? and https://github.com/memcached/memcached/issues/405

Upvotes: 1

Julesezaar
Julesezaar

Reputation: 3396

First list the items (so we can see the ids)

echo "stats items" | nc 127.0.0.1 11211

Then you get something that looks like this:

STAT items:13:number 1
STAT items:13:number_hot 0
STAT items:13:number_warm 0
...

The id is 13 in this example. (There can be multiple ids)

Then dump data for the first 100 items for id 13:

echo "stats cachedump 13 100" | nc 127.0.0.1 11211

Then the content of an item looks like this

ITEM sf2abhftbcvdgtegieoghetdgeddf [1302 b; 1707820192 s]

Which tells:

  • the size is 1302 bytes
  • the date is 1707820192 EPOCH time -> 2024/02/13 10:29:52

Upvotes: 0

kenorb
kenorb

Reputation: 166359

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

memdump --servers=localhost

To get value of item, use netcat:

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

or in Bash:

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

To dump all objects:

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

or in Bash:

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

Upvotes: 12

Steve
Steve

Reputation: 1865

There is no way to get memcached to report which keys it holds. I believe that this was a design choice as to do so would have a negative impact on performance.

However, you can use any telnet client application to connect the memcached server and type in commands. Doing this to get or set a particular key.

For example,

stats

or:

get MY_KEY

Upvotes: 17

Related Questions