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. Cachable Class Method Decorator by amitu 4 years, 7 months ago
  2. Cache Any Function by jeffwheeler 6 years, 2 months ago
  3. Feed Reader Inclusion Tag with Caching by baumer1122 5 years, 8 months ago
  4. RESTful class dispatch by Phoenix 3 years, 11 months ago
  5. RestfulView by whiteinge 5 years, 7 months ago

Comments

amitu (on October 29, 2009):

This is now maintained as a part of dutils.

#

(Forgotten your password?)