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 5 years, 4 months ago
  2. Conditional cache decorator by alexisbellido 1 year, 7 months ago
  3. per-function cache decorator by nicois 6 years, 2 months ago
  4. Caching Decorator by spenczar 1 year, 11 months ago
  5. Cache decorator by xyb 6 years, 4 months ago

Comments

(Forgotten your password?)