How invalidate beaker_cache in Pylons?

587 views Asked by At

Need to invalidate a cache created by beaker_cache decorator for a specific controller action:

from pylons.decorators.cache import beaker_cache

class SampleController(BaseController):

    @beaker_cache()
    def home(self):
        c.data = expensive_call()
        return render('/home.myt')

    def __clear_home_cache(self):
        pass

Can I use region_invalidate() inside __clear_home_cache function?

1

There are 1 answers

0
Pēteris Caune On BEST ANSWER

One way to find out how to invalidate stuff cached by beaker_cache decorator is to look at how it works and what it does. It is defined in pylons.decorators.cache module, here's the corresponding source file on GitHub.

Basically you're looking for the logic that chooses namespace and cache key for a given controller action. This is done by create_cache_key() function in that file. And, incidentally, that function has a helpful comment:

Example:
    from pylons import cache
    from pylons.decorators.cache import create_cache_key
    namespace, key = create_cache_key(MyController.some_method)
    cache.get_cache(namespace).remove(key)