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
|
|
|
|
2013-03-20 15:31:27 +01:00
|
|
|
from utils import make_safe_digest
|
|
|
|
|
2013-03-15 19:51:19 +01:00
|
|
|
def cache_with_key(keyfunc, cache_name=None, timeout=None):
|
2012-09-19 18:41:20 +02:00
|
|
|
"""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):
|
2013-03-13 18:36:58 +01:00
|
|
|
if cache_name is None:
|
|
|
|
cache_backend = djcache
|
|
|
|
else:
|
|
|
|
cache_backend = get_cache(cache_name)
|
|
|
|
|
2012-09-19 18:41:20 +02:00
|
|
|
key = keyfunc(*args, **kwargs)
|
2013-03-13 18:36:58 +01:00
|
|
|
val = cache_backend.get(key)
|
2012-09-19 18:41:20 +02:00
|
|
|
|
|
|
|
# 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)
|
2013-03-15 19:51:19 +01:00
|
|
|
cache_backend.set(key, (val,), timeout=timeout)
|
2012-09-19 18:41:20 +02:00
|
|
|
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-13 18:49:29 +01:00
|
|
|
|
|
|
|
def message_cache_key(message_id):
|
|
|
|
return "message:%d" % (message_id,)
|
|
|
|
|
2013-03-18 20:56:32 +01:00
|
|
|
def user_profile_by_email_cache_key(email):
|
2013-03-20 15:31:27 +01:00
|
|
|
# See the comment in zephyr/lib/avatar.py:gravatar_hash for why we
|
|
|
|
# are proactively encoding email addresses even though they will
|
|
|
|
# with high likelihood be ASCII-only for the foreseeable future.
|
|
|
|
return 'user_profile_by_email:%s' % (make_safe_digest(email),)
|
2013-03-13 18:49:29 +01:00
|
|
|
|
2013-03-18 20:56:32 +01:00
|
|
|
def user_profile_by_user_cache_key(user_id):
|
2013-03-15 21:27:28 +01:00
|
|
|
return 'user_profile_by_user_id:%d' % (user_id,)
|
2013-03-13 18:49:29 +01:00
|
|
|
|
2013-03-18 17:10:45 +01:00
|
|
|
def user_profile_by_id_cache_key(user_profile_id):
|
|
|
|
return "user_profile_by_id:%s" % (user_profile_id,)
|
|
|
|
|
2013-03-13 18:49:29 +01:00
|
|
|
def user_by_id_cache_key(user_id):
|
2013-03-15 21:27:28 +01:00
|
|
|
return 'user_by_id:%d' % (user_id,)
|
2013-03-15 21:17:32 +01:00
|
|
|
|
|
|
|
# Called by models.py to flush the user_profile cache whenever we save
|
|
|
|
# a user_profile object
|
|
|
|
def update_user_profile_cache(sender, **kwargs):
|
|
|
|
user_profile = kwargs['instance']
|
|
|
|
items_for_memcached = {}
|
2013-03-18 20:56:32 +01:00
|
|
|
items_for_memcached[user_profile_by_email_cache_key(user_profile.user.email)] = (user_profile,)
|
|
|
|
items_for_memcached[user_profile_by_user_cache_key(user_profile.user.id)] = (user_profile,)
|
2013-03-18 17:10:45 +01:00
|
|
|
items_for_memcached[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
|
2013-03-15 21:17:32 +01:00
|
|
|
djcache.set_many(items_for_memcached)
|
2013-03-15 21:23:34 +01:00
|
|
|
|
|
|
|
# Called by models.py to flush the user_profile cache whenever we save
|
|
|
|
# a user_profile object
|
|
|
|
def update_user_cache(sender, **kwargs):
|
|
|
|
user = kwargs['instance']
|
|
|
|
items_for_memcached = {}
|
|
|
|
items_for_memcached[user_by_id_cache_key(user.id)] = (user,)
|
|
|
|
djcache.set_many(items_for_memcached)
|
|
|
|
|