from functools import wraps from django core cache import cache from d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from functools import wraps
from django.core.cache import cache
from django.db.models import signals
def cachedel(keyfunc, sender):
def inner(instance, *args, **kwargs):
key = keyfunc(instance)
cache.delete(key)
signals.post_save.connect(inner, sender=sender)
signals.post_delete.connect(inner, sender=sender)
def cached(keyfunc):
def wrapper(func):
@wraps(func)
def inner(*args, **kwargs):
key = keyfunc(*args, **kwargs)
out = cache.get(key)
if out is None:
out = func(*args, **kwargs)
cache.set(key, out)
return out
return inner
return wrapper