sexylee
sexylee

Reputation: 31

How to get all the keys from memcache using python

I'm trying to find how to get all the keys from memcache but I can not find the answer. I saw some answer codes that using telnet but finally it didn't work. I want to know exactly how to get keys at once, using telnet or other things. It should be done in python.

Upvotes: 3

Views: 3571

Answers (3)

Nishant
Nishant

Reputation: 21914

This gets all the keys in recent versions (>= 1.4.31) of Memcached (going by this answer):

with Telnet(host=self.server, port=self.port) as telnet:
    telnet.write(b"lru_crawler metadump all\n")
    raw = telnet.read_until(b"\nEND")

data = ensure_str(raw).split("\n")

It can be parsed and used accordingly.

Upvotes: 0

Stephen O
Stephen O

Reputation: 41

A bit late, but here's how I was able to do it. client is a pymemcached client, ensure_str converts bytes to unicode (six.ensure_str would work):

keys = {}
for key, val in client.stats('items').items():
    _, slab, field = ensure_str(key).split(':')
    if field != 'number' or val == 0:
        continue
    item_request = client.stats('cachedump', slab, str(val + 10))
    for record, details in item_request.items():
        keys[ensure_str(record)] = ensure_str(details)

Ends up with a format as follows:

>>> print('\n'.join(f'{k}: {v}' for k, v in keys.items()))

local/_action/eb4f0b5c-7dcb-457a-ae8a-1aad1aef1ce0: [192 b; 1631502402 s]
local/_action/d7ffc698-0845-4662-8860-6a82877c0672: [192 b; 1631502338 s]
local/_action/8fc360e4-36ed-4474-bc4d-4ecbb4513d17: [201 b; 1631501746 s]
local/_action/7ee34ab4-408e-4bb1-91eb-56e7d6d131bd: [265 b; 1631499358 s]
local/_action/7662cdf4-6eb5-4a1c-b7bf-3d7971db12e6: [330 b; 1631501876 s]

Theory mostly taken from https://stackoverflow.com/a/19562199/4001895

Upvotes: 4

Milos K
Milos K

Reputation: 1207

Having in mind that Memcache acts as dict there is:

cache.get_multi(keys)

As seen on:

https://pymemcache.readthedocs.io/en/latest/apidoc/pymemcache.client.base.html#pymemcache.client.base.Client.get_multi

There is answer how to do this using telnet protocol. As far as I know there are no commands in Python Library for doing the same:

How to export all keys and values from memcached with python-memcache?

Upvotes: 0

Related Questions