17

In runtime, I want to retrieve the expire time info about some items in memcached. I didn't find any related interface on memcached. Can I do this? something like: mc.get_expire_time('key')

Thank you

maguschen
  • 765
  • 2
  • 8
  • 12

4 Answers4

26

Python memcache API doesn't provide such functionalities. However you can telnet into memcached to dump all keys and expiration time.

> telnet localhost 11211

stats items show the slabs that contain your data.

stats items
STAT items:12:number 1108
...
END

Then use stats cachedump slab_id count to see the key and expiration time. Set count to 0 to retrieve all keys.

stats cachedump 12 1
ITEM abc [100 b; 1528336485 s]
END
Ko-Chih Wu
  • 682
  • 7
  • 11
  • A blog post explain we can only dump one page per slab class (1MB of data). In my cases it means 32771 keys: https://lzone.de/blog/How-to%20Dump%20Keys%20from%20Memcache – mcoolive Apr 04 '18 at 15:33
10

Annoyingly, this information only seems to be provided in the slab stats. Start with this:

[$]> (sleep 1; echo "stats cachedump 1 0"; sleep 1; echo "quit";) | telnet localhost 11211 | grep 'my_key'

and increment the slab (the first number after 'cachedump') until you find the appropriate slab. Once you get a result, it'll be of the form

ITEM my_key [2 b; 1389767076 s]

The last number there (1389767076 in this case) is the unixtime when the key will expire. You can convert this number to something more human-readable with Python's time.localtime() or on-the-fly using Wolfram Alpha.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • There's some code here http://codereview.stackexchange.com/questions/636/review-request-python-class-w-telnet-interface-to-memcached that takes care of almost everything required to follow this approach. – Mark Chackerian Jan 24 '14 at 22:36
  • first line returns: -bash: syntax error near unexpected token `(' – Nathan B Aug 23 '22 at 20:18
8

According to memcache protocol (both text and binary) niether get nor gets return expiration time. And there is no other method to retrieve it. But sure you can pack expiration time into value along with what you store now when you set/add it to make it retrievable.

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • the link for the text documentation is broken, perhaps this is useful: https://github.com/memcached/memcached/blob/master/doc/protocol.txt – lastYorsh Aug 26 '20 at 08:30
0

If you are not critical on accuracy, I have thought about creating your own client that talks via binary protocol and storing expiration time in the Flag field of ExtraLength. https://github.com/memcached/memcached/wiki/BinaryProtocolRevamped#set-add-replace

Here, in the 8 bytes extra, the last four are storing TTL, you can use the first 4 to store expiration time. And when you GET it back, it's still in that flag field.

The down side of this is:

  • Writing your own client.
  • You have to base on local clock to calculate expiration time
  • You only have 4 bytes to store an expiration time in integer.
JasonShao
  • 563
  • 1
  • 4
  • 5