Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 645
sexylee Avatar asked Aug 21 '26 10:08

sexylee


1 Answers

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

like image 157
Stephen O Avatar answered Aug 22 '26 23:08

Stephen O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!