如何使用 python-memcache 从 memcached 导出所有键和值?
我想使用 python-memcache 从 memcached 服务器导出所有键和值。 该模块中没有这样的功能。那该怎么办呢?
也许需要一些涉及“套接字”模块的更复杂的东西。
I would like to export all keys and values from a memcached server, using python-memcache.
There is no such function in that module. How to do it then?
Perhaps something more complicated involving the "socket" module would be needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这将为您提供 memcached 服务器上的所有键,您可以使用任何 memcached 客户端库来获取每个键的值。
This will get you all the keys on a memcached server, you can use any memcached client library to get the value for each key.
使用 memdump 和 memcat 来自 libmemcached 套件的实用程序。他们不能保证您会获得所有数据,但它们很容易使用。
注意:在 ubuntu/debian 上,您可以通过安装
libmemcached-tools
软件包来获取这些工具,它们称为memcdump
和memccat.
转储所有键:
转储所有值:
当然,您仍然必须匹配键和值 - 我建议将键转储到文件中,然后将其用作
memcat
的输入(这确保一致性)。然后,当然您需要拆分值 - 我相信句号是分隔符 - 然后按顺序将键和值配对。可能某个地方有一个脚本...use the memdump and memcat utilities from the libmemcached suite. They can't guarantee you'll get all the data but they're easy to use.
Note: On ubuntu/debian you can get these by intstalling the
libmemcached-tools
package and they are calledmemcdump
andmemccat
.dump all keys:
dump all values:
of course you still have to match up the keys and values - I'd suggest dumping the keys to a file and then using that as the input for
memcat
(this ensures consistency). Then of course you need to split the values - a full stop is the delimiter I believe - and then pair the keys and values sequentially. There's probably a script out there somewhere...没有办法做到这一点。 Memcache 协议 没有定义任何迭代键的命令。您必须知道检索值的密钥。
There is no way to do that. Memcache protocol does not define any command to iterate over keys. You have to know the key to retrieve value.
最简单的方法是使用 python-memcached-stats 包, https://github.com/abstatic /python-memcached-stats
keys() 方法应该可以帮助你。
The easiest way is to use python-memcached-stats package, https://github.com/abstatic/python-memcached-stats
The keys() method should get you going.
正如其他人在很多地方提到的,在一般情况下,没有办法列出存储在 memcached 实例中的所有键。例如,Memcached:列出所有密钥,无法通过 telnet 客户端检索所有 memcache 密钥
但是,您可以,列出类似于前 1Meg 密钥的内容,这通常足以了解开发过程中 memcache 服务器中存储的内容。基本上,您可以有两种选择从 memcache 服务器提取项目:
(1) 要检索键和值的子集,您可以使用上面介绍的方法使用 @lrd
但是,当数据非常大时(例如,数百万条记录) ),这种方法可能非常耗时。更重要的是,此方法只能检索键和键的子集。价值观。
(2) 如果您想要迭代 memcached 服务器的所有项目,则在向 memcache 服务器添加/设置/cas 项目时记录密钥是一种更便宜的解决方案。然后您可以读取日志文件以获取所有键并从 memcache 服务器获取值。正如此邮件列表中所述:列出 memcached 中的所有对象
As mentioned by others in many places, in the general case, there is no way to list all the keys that stored in a memcached instance. E.g., Memcached: List all keys, Couldn't retrieve all the memcache keys via telnet client
You can, however, list something like the first 1Meg of keys, which is usually enough to have an idea about what are stored in memcache server during development. Basically, you can have two option to extract items from memcache server:
(1) To retrieve a subset of keys and values, you can the method introduced above by use @lrd
However, when the data is very large (e.g., millions of records), this method can be very time-consuming. What's more, this method can only retrieve only a subset of the keys & values.
(2) In cases where you would like to iterate all the items of the memcached server, logging the keys when one adds/set/cas items to memcache server is a much cheaper solution. Then you can read through the log file to get all the keys and get the values from memcache server. As discussed in this mailing list: List all objects in memcached
在 Bash 中,此脚本可能会有所帮助:
您还可以直接连接到端口并发送
get SOME-KEY
命令(使用 Netcat 的示例:echo ",而不是
)。memccat
)获取 $key" | nc localhost 11211相关:
In Bash, this script may help:
Instead of
memccat
, you can also connect to port directly and sendget SOME-KEY
command (example using Netcat:echo "get $key" | nc localhost 11211
).Related:
您正在寻找“flush_all”memcache 命令:http://code.google .com/p/memcached/wiki/NewCommands#flush_all
使用 python-memcached,它看起来像这样:
You're looking for the 'flush_all' memcache command: http://code.google.com/p/memcached/wiki/NewCommands#flush_all
With python-memcached, it looks something like this: