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
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
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.
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
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.
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.
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:
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.
© 2022 - 2024 — McMap. All rights reserved.