mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 06:53:25 +00:00
Add caching decorators
(imported from commit 7a4d9257ea8c6a86a5ffb498f726c4c2eb42bc9f)
This commit is contained in:
44
zephyr/lib/cache.py
Normal file
44
zephyr/lib/cache.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import django.core.cache
|
||||
|
||||
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):
|
||||
djcache = django.core.cache.cache
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user