How can I get the expire time for the particular item in memcached
Asked Answered
D

5

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

Differ answered 1/4, 2010 at 7:10 Comment(2)
It's not limited to python, if there are any kind language or lib could do this.Differ
Python API's on this page: code.google.com/p/memcached/wiki/ClientsDeste
G
9

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.

Grating answered 1/4, 2010 at 12:28 Comment(1)
the link for the text documentation is broken, perhaps this is useful: github.com/memcached/memcached/blob/master/doc/protocol.txtSerg
T
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
Tercel answered 2/4, 2012 at 3:45 Comment(1)
A blog post explain we can only dump one page per slab class (1MB of data). In my cases it means 32771 keys: lzone.de/blog/How-to%20Dump%20Keys%20from%20MemcacheLaparotomy
P
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.

Parodic answered 23/1, 2014 at 1:56 Comment(2)
There's some code here codereview.stackexchange.com/questions/636/… that takes care of almost everything required to follow this approach.Fanestil
first line returns: -bash: syntax error near unexpected token `('Misspeak
G
9

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.

Grating answered 1/4, 2010 at 12:28 Comment(1)
the link for the text documentation is broken, perhaps this is useful: github.com/memcached/memcached/blob/master/doc/protocol.txtSerg
C
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.
Cunha answered 22/3, 2019 at 18:42 Comment(0)
A
0

Though it is a very old question, but anyone troubled with same may find this helpful. With the versions of memcached dated after Dec 2021, it is possible to achieve it using Meta commands. Refer to the following documentation: Memcached MetaCommands

mg <key> t c v

Will give you remaining expiration time, CASID, and data

Though for Python I'm not sure if the library you are using supports it, you may have to tweak it a little for it to support the MetaCommand.

Amass answered 30/4 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.