Stales Cache Class Method Decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.core.cache import cache
def stales_cache(cache_key):
    def paramed_decorator(func):
        def decorated(self, *args, **kw):
            key = cache_key % self.__dict__
            cache.delete(key)
            return func(self, *args, **kw)
        decorated.__doc__ = func.__doc__
        decorated.__dict__ = func.__dict__
        return decorated
    return paramed_decorator

# usage

class SomeClass(models.Model):
    # fields
    name = CharField(...)

    @stales_cache("SomeClass_some_key_that_depends_on_name_%(name)")
    @stales_cache("SomeClass_some_other_key_that_depends_on_name_%(name)")
    def update_name(self, new_name):
        self.name = new_name
        self.save()

More like this

  1. Cache Decorator by ericmoritz 3 years, 5 months ago
  2. Cachable Class Method Decorator by amitu 3 years, 7 months ago
  3. Method Caching by bryanhelmig 10 months, 4 weeks ago
  4. per-function cache decorator by nicois 4 years, 4 months ago
  5. Caching Decorator by spenczar 1 month ago

Comments

amitu (on October 29, 2009):

This is now maintained as a part of dutils.

#

(Forgotten your password?)