Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete memcache by key name start with a specific string (gae,python)

i build a blog with gae, and stored many items in memcache, including the paged entries.

the key to store these pages is use query object and pageindex:

@property
def _query_id(self):
    if not hasattr(self, '__query_id'):
        hsh = hashlib.md5()
        hsh.update(repr(self.query))
        self.__query_id = hsh.hexdigest()
    return self.__query_id

def _get_cache_key(self, page):
    return '%s%s' % (self._query_id, page)

it'll show in admin console like: NDB9:xxxxxx, beside this, I stored any other item start with sitename-obj.

In some case, I want to only clear all the paged cache, but I don't know how. I wonder if there is a way to delete memcache by key name which start with NDB9?

yes, I'v found such function,

delete_multi(keys, seconds=0, key_prefix='', namespace=None)

but it seems that the key_prefix is just add to every key in the first argument, and I want to only delete memcache by key_prefix.

like image 295
walker Avatar asked Feb 02 '26 04:02

walker


1 Answers

You cannot delete keys by prefix; you can only delete specific keys, or flush all the of the cache.

In this case, you'd have to loop over all page ids to produce all possible keys. Pass those to delete_multi().

The key_prefix argument is just a convenience method; you can send shorter 'keys' if they all have the same prefix. If all your keys start with NDB9, use that as the key prefix, and send a list of keys over without that prefix. The prefix will be added to each key by the memcached server when looking for what keys to delete.

like image 176
Martijn Pieters Avatar answered Feb 04 '26 19:02

Martijn Pieters