mirror of
https://github.com/zulip/zulip.git
synced 2025-11-14 10:57:58 +00:00
[manual] Cache results of the Twitter API in the database.
This should substantially improve the repeat-rendering time for pages with large numbers of tweets since we don't need to go all the way to twitter.com, which can take like a second, to render tweets properly. To deploy this commit properly, one needs to run ./manage.py createcachetable third_party_api_results (imported from commit 01b528e61f9dde2ee718bdec0490088907b6017e)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from functools import wraps
|
||||
|
||||
from django.core.cache import cache as djcache
|
||||
from django.core.cache import get_cache
|
||||
|
||||
def cache_with_key(keyfunc):
|
||||
"""Decorator which applies Django caching to a function.
|
||||
@@ -44,3 +45,31 @@ def cache(func):
|
||||
return key.replace('-','--').replace(' ','-s')
|
||||
|
||||
return cache_with_key(keyfunc)(func)
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user