2012-11-02 00:23:26 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
2013-01-09 20:35:19 +01:00
|
|
|
from django.core.cache import cache as djcache
|
2013-03-11 16:23:34 +01:00
|
|
|
from django.core.cache import get_cache
|
2012-09-19 18:41:20 +02:00
|
|
|
|
|
|
|
def cache_with_key(keyfunc):
|
|
|
|
"""Decorator which applies Django caching to a function.
|
|
|
|
|
|
|
|
Decorator argument is a function which computes a cache key
|
|
|
|
from the original function's arguments. You are responsible
|
|
|
|
for avoiding collisions with other uses of this decorator or
|
|
|
|
other uses of caching."""
|
|
|
|
|
|
|
|
def decorator(func):
|
2012-11-02 00:23:26 +01:00
|
|
|
@wraps(func)
|
2012-09-19 18:41:20 +02:00
|
|
|
def func_with_caching(*args, **kwargs):
|
|
|
|
key = keyfunc(*args, **kwargs)
|
|
|
|
val = djcache.get(key)
|
|
|
|
|
|
|
|
# Values are singleton tuples so that we can distinguish
|
|
|
|
# a result of None from a missing key.
|
|
|
|
if val is not None:
|
|
|
|
return val[0]
|
|
|
|
|
|
|
|
val = func(*args, **kwargs)
|
|
|
|
djcache.set(key, (val,))
|
|
|
|
return val
|
|
|
|
|
|
|
|
return func_with_caching
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def cache(func):
|
|
|
|
"""Decorator which applies Django caching to a function.
|
|
|
|
|
|
|
|
Uses a key based on the function's name, filename, and
|
|
|
|
the repr() of its arguments."""
|
|
|
|
|
|
|
|
func_uniqifier = '%s-%s' % (func.func_code.co_filename, func.func_name)
|
|
|
|
|
2012-11-02 00:23:26 +01:00
|
|
|
@wraps(func)
|
2012-09-19 18:41:20 +02:00
|
|
|
def keyfunc(*args, **kwargs):
|
|
|
|
# Django complains about spaces because memcached rejects them
|
|
|
|
key = func_uniqifier + repr((args, kwargs))
|
|
|
|
return key.replace('-','--').replace(' ','-s')
|
|
|
|
|
|
|
|
return cache_with_key(keyfunc)(func)
|
2013-03-11 16:23:34 +01:00
|
|
|
|
|
|
|
def db_cache_with_key(keyfunc):
|
|
|
|
"""Decorator which applies Django caching to a function.
|
|
|
|
|
|
|
|
Decorator argument is a function which computes a cache key
|
|
|
|
from the original function's arguments. You are responsible
|
|
|
|
for avoiding collisions with other uses of this decorator or
|
|
|
|
other uses of caching."""
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def func_with_caching(*args, **kwargs):
|
|
|
|
key = keyfunc(*args, **kwargs)
|
|
|
|
database_cache = get_cache("database")
|
|
|
|
val = database_cache.get(key)
|
|
|
|
|
|
|
|
# Values are singleton tuples so that we can distinguish
|
|
|
|
# a result of None from a missing key.
|
|
|
|
if val is not None:
|
|
|
|
return val[0]
|
|
|
|
|
|
|
|
val = func(*args, **kwargs)
|
|
|
|
database_cache.set(key, (val,))
|
|
|
|
return val
|
|
|
|
|
|
|
|
return func_with_caching
|
|
|
|
|
|
|
|
return decorator
|