How can I get the expire time for the particular item in memcached
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.
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