Login

Stales Cache Class Method Decorator

Author:
amitu
Posted:
October 13, 2008
Language:
Python
Version:
1.0
Score:
3 (after 3 ratings)

A companion to Cache 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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.