skip to Main Content

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.

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search