python 如何获取 memcached 中特定项目的过期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2558706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the expire time for the particular item in memcached
提问by maguschen
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')
在运行时,我想检索有关 memcached 中某些项目的过期时间信息。我在memcached上没有找到任何相关的接口。我可以这样做吗?类似于: mc.get_expire_time('key')
Thank you
谢谢
采纳答案by Denis Otkidach
According to memcache protocol (both textand 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.
根据 memcache 协议(文本和二进制)get
也不gets
返回过期时间。并且没有其他方法可以检索它。但是请确保您可以将到期时间与您现在存储的内容一起打包成价值,以便您set
/add
它可以检索它。
回答by Ko-Chih Wu
Python memcache API doesn't provide such functionalities. However you can telnet into memcached to dump all keys and expiration time.
Python 内存缓存 API 不提供此类功能。但是,您可以 telnet 进入 memcached 以转储所有密钥和到期时间。
> telnet localhost 11211
stats items
show the slabs that contain your data.
stats items
显示包含您的数据的平板。
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 slab_id count
查看key和过期时间。将计数设置为 0 以检索所有键。
stats cachedump 12 1
ITEM abc [100 b; 1528336485 s]
END
回答by Xiong Chiamiov
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
并增加slab('cachedump'之后的第一个数字),直到找到合适的slab。一旦你得到一个结果,它就会是这样的形式
ITEM my_key [2 b; 1389767076 s]
The last number there (1389767076
in this case) is the unixtimewhen 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.
那里的最后一个数字(1389767076
在这种情况下)是密钥到期的unixtime。您可以使用 Pythontime.localtime()
或使用Wolfram Alpha将这个数字转换为更易读的数字。
回答by JasonShao
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
如果您对准确性不挑剔,我已经考虑过创建您自己的客户端,通过二进制协议进行对话并将过期时间存储在 ExtraLength 的 Flag 字段中。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.
这里额外的8个字节中,后4个是存放TTL,可以用前4个来存放过期时间。当你取回它时,它仍然在那个标志字段中。
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.
- 编写自己的客户端。
- 您必须根据本地时钟来计算到期时间
- 您只有 4 个字节来存储整数的到期时间。