2

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.

user2653663
  • 2,818
  • 1
  • 18
  • 22
sexylee
  • 21
  • 2
  • 2
    please tell people what have you done already, such as a code snippet or something, without code snippet your question will not draw attention ad no one will understand your situation correctly I doubt – Inzamam Malik Jan 22 '20 at 12:13
  • this is my first post. so I didn't know how to ask efficiently. your advice will help me. thank you for your advice. – sexylee Jan 23 '20 at 03:57

2 Answers2

3

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

Stephen O
  • 31
  • 3
0

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?

Milos K
  • 1,009
  • 8
  • 5
  • I think my question was vague. Exactly, my question was How to get the key[list] from memcache (without keys). so I cannot use the get_multi method because I don't know some keys. that's why I wanna get all the keys from memcache. but I guess the link that you gave me would be helpful. I think I should study more about the 'telnet'. Thanks! – sexylee Jan 23 '20 at 04:05