1. Michael Bayer
  2. dogpile.cache
  3. Issues
Issue #25 resolved

A way to "tag" cache objects

Anonymous created an issue

I would like a way to tag a set of methods that I cache on arguments in the same identifier so that I can bulk invalidate them.

For example, I have a User who has a bunch of documents. If the user object changes, I want to invalidate the cache of all his documents. I don't want to be aware of all methods and their arguments they are cached at, I just want to say "invalidate bulk cache for user id 23" or something like that.

Is this possible?

Comments (3)

  1. Michael Bayer repo owner

    you'd have to build a layer on top which does this. you can pass in your own "key generator" which generates and tracks keys generated:

    from dogpile.cache import make_region
    from itertools import count
    user_keys = set()
    def my_key_generator(namespace, fn):
        fname = fn.__name__
        def generate_key(*arg):
            # generate a key template:
            # "namespace_fname_%d_arg1_arg2_arg3..."
            key_template = fname + "_" + \
                                "%d" + \
                                "_".join(str(s) for s in arg[1:])
            # store key template
            # return cache key
            user_id = arg[0]
            return key_template % user_id
        return generate_key
    def invalidate_user_id(region, user_id):
        for key in user_keys:
            region.delete(key % user_id)
    region = make_region(
    counter = count()
    def user_fn_one(user_id):
        return "user fn one: %d, %d" % (next(counter), user_id)
    def user_fn_two(user_id):
        return "user fn two: %d, %d" % (next(counter), user_id)
    def user_fn_three(user_id):
        return "user fn three: %d, %d" % (next(counter), user_id)
    print user_fn_one(5)
    print user_fn_two(5)
    print user_fn_three(7)
    print user_fn_two(7)
    invalidate_user_id(region, 5)
    print "invalidated:"
    print user_fn_one(5)
    print user_fn_two(5)
    print user_fn_three(7)
    print user_fn_two(7)
  2. Log in to comment