如何使用 python-memcache 从 memcached 导出所有键和值?

发布于 2024-11-03 01:52:48 字数 103 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

恍梦境° 2024-11-10 01:52:48

这将为您提供 memcached 服务器上的所有键,您可以使用任何 memcached 客户端库来获取每个键的值。

import telnetlib

def get_all_memcached_keys(host='127.0.0.1', port=11211):
    t = telnetlib.Telnet(host, port)
    t.write('stats items STAT items:0:number 0 END\n')
    items = t.read_until('END').split('\r\n')
    keys = set()
    for item in items:
        parts = item.split(':')
        if not len(parts) >= 3:
            continue
        slab = parts[1]
        t.write('stats cachedump {} 200000 ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s] END\n'.format(slab))
        cachelines = t.read_until('END').split('\r\n')
        for line in cachelines:
            parts = line.split(' ')
            if not len(parts) >= 3:
                continue
            keys.add(parts[1])
    t.close()
    return keys

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.

import telnetlib

def get_all_memcached_keys(host='127.0.0.1', port=11211):
    t = telnetlib.Telnet(host, port)
    t.write('stats items STAT items:0:number 0 END\n')
    items = t.read_until('END').split('\r\n')
    keys = set()
    for item in items:
        parts = item.split(':')
        if not len(parts) >= 3:
            continue
        slab = parts[1]
        t.write('stats cachedump {} 200000 ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s] END\n'.format(slab))
        cachelines = t.read_until('END').split('\r\n')
        for line in cachelines:
            parts = line.split(' ')
            if not len(parts) >= 3:
                continue
            keys.add(parts[1])
    t.close()
    return keys
倒带 2024-11-10 01:52:48

使用 memdumpmemcat 来自 libmemcached 套件的实用程序。他们不能保证您会获得所有数据,但它们很容易使用。

注意:在 ubuntu/debian 上,您可以通过安装 libmemcached-tools 软件包来获取这些工具,它们称为 memcdumpmemccat.

转储所有键:

memcdump --servers=localhost

转储所有值:

memccat --servers=localhost `memcdump --servers=localhost`

当然,您仍然必须匹配键和值 - 我建议将键转储到文件中,然后将其用作 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 called memcdump and memccat.

dump all keys:

memcdump --servers=localhost

dump all values:

memccat --servers=localhost `memcdump --servers=localhost`

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...

迟到的我 2024-11-10 01:52:48

没有办法做到这一点。 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.

诺曦 2024-11-10 01:52:48

最简单的方法是使用 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.

黄昏下泛黄的笔记 2024-11-10 01:52:48

正如其他人在很多地方提到的,在一般情况下,没有办法列出存储在 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

£噩梦荏苒 2024-11-10 01:52:48

在 Bash 中,此脚本可能会有所帮助:

while read -r key; do
    memccat --servers=localhost $key > "$key.dump";
done < <(memcdump --server localhost)

您还可以直接连接到端口并发送 get SOME-KEY 命令(使用 Netcat 的示例:echo ",而不是 memccat)获取 $key" | nc localhost 11211)。

相关:

In Bash, this script may help:

while read -r key; do
    memccat --servers=localhost $key > "$key.dump";
done < <(memcdump --server localhost)

Instead of memccat, you can also connect to port directly and send get SOME-KEY command (example using Netcat: echo "get $key" | nc localhost 11211).

Related:

怕倦 2024-11-10 01:52:48

您正在寻找“flush_all”memcache 命令:http://code.google .com/p/memcached/wiki/NewCommands#flush_all

使用 python-memcached,它看起来像这样:

>>> import memcache
>>> c = memcache.Client(('127.0.0.1:11211',))
>>> c.flush_all()

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:

>>> import memcache
>>> c = memcache.Client(('127.0.0.1:11211',))
>>> c.flush_all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文