pythoncachingdiskcache

How to get all elements by tag?


I use diskcache to persist my data. I save users cache.add(key=k, value=v, tag="users"), now i want to get all users by tag, but there is no such method.

How i can do this?

I've found only one way to do this:

def _get_all(self):
    r = []
    for k in list(self._cache.iterkeys()):
        r.append(self._cache.get(key=k))

But this way does not assume tag as argument, so i can not persist in 1 diskcache instance different items and filter them by tag.


Solution

  • Looking at the source code of python-diskcache, the sole purpose of the tag is be used in an SQLite index which is meant to enable fast cache eviction/culling on the basis of a tag value.

    The only SQL statement that tag is ever used in is in the .evict() method.

    There is no official API to get cache entries by tag, and the library is not designed to do that. The whole underlying setup of the database and the item-retrieval mechanics are key-centered.