2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-03-10 17:15:34 +01:00
|
|
|
from __future__ import print_function
|
2013-04-23 18:51:17 +02:00
|
|
|
|
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
|
2013-05-30 21:02:12 +02:00
|
|
|
from django.conf import settings
|
2014-02-26 00:12:14 +01:00
|
|
|
from django.db.models import Q
|
2012-09-19 18:41:20 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.utils import statsd, statsd_key, make_safe_digest
|
2013-04-19 00:00:33 +02:00
|
|
|
import time
|
2013-05-30 21:02:12 +02:00
|
|
|
import base64
|
|
|
|
import random
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import hashlib
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2016-03-31 03:23:21 +02:00
|
|
|
remote_cache_time_start = 0.0
|
2016-01-26 04:08:05 +01:00
|
|
|
memcached_total_time = 0.0
|
2013-04-19 00:00:33 +02:00
|
|
|
memcached_total_requests = 0
|
|
|
|
|
2016-03-31 03:23:21 +02:00
|
|
|
def get_remote_cache_time():
|
2013-04-19 00:00:33 +02:00
|
|
|
return memcached_total_time
|
|
|
|
|
2016-03-31 03:24:05 +02:00
|
|
|
def get_remote_cache_requests():
|
2013-04-19 00:00:33 +02:00
|
|
|
return memcached_total_requests
|
|
|
|
|
2013-05-10 16:57:06 +02:00
|
|
|
def memcached_stats_start():
|
2016-03-31 03:23:21 +02:00
|
|
|
global remote_cache_time_start
|
|
|
|
remote_cache_time_start = time.time()
|
2013-05-10 16:57:06 +02:00
|
|
|
|
|
|
|
def memcached_stats_finish():
|
|
|
|
global memcached_total_time
|
|
|
|
global memcached_total_requests
|
2016-03-31 03:23:21 +02:00
|
|
|
global remote_cache_time_start
|
2013-05-10 16:57:06 +02:00
|
|
|
memcached_total_requests += 1
|
2016-03-31 03:23:21 +02:00
|
|
|
memcached_total_time += (time.time() - remote_cache_time_start)
|
2013-04-16 22:58:21 +02:00
|
|
|
|
2013-05-30 21:02:12 +02:00
|
|
|
def get_or_create_key_prefix():
|
2013-07-03 01:26:00 +02:00
|
|
|
if settings.TEST_SUITE:
|
|
|
|
# This sets the prefix mostly for the benefit of the JS tests.
|
|
|
|
# The Python tests overwrite KEY_PREFIX on each test.
|
|
|
|
return 'test_suite:' + str(os.getpid()) + ':'
|
|
|
|
|
2013-06-24 20:56:44 +02:00
|
|
|
filename = os.path.join(settings.DEPLOY_ROOT, "memcached_prefix")
|
2013-05-30 21:02:12 +02:00
|
|
|
try:
|
2015-11-01 17:10:29 +01:00
|
|
|
fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o444)
|
2013-05-30 21:02:12 +02:00
|
|
|
prefix = base64.b16encode(hashlib.sha256(str(random.getrandbits(256))).digest())[:32].lower() + ':'
|
|
|
|
# This does close the underlying file
|
|
|
|
with os.fdopen(fd, 'w') as f:
|
|
|
|
f.write(prefix + "\n")
|
|
|
|
except OSError:
|
|
|
|
# The file already exists
|
|
|
|
tries = 1
|
|
|
|
while tries < 10:
|
2015-10-14 22:31:08 +02:00
|
|
|
with open(filename, 'r') as f:
|
2013-05-30 21:02:12 +02:00
|
|
|
prefix = f.readline()[:-1]
|
|
|
|
if len(prefix) == 33:
|
|
|
|
break
|
|
|
|
tries += 1
|
|
|
|
prefix = ''
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
if not prefix:
|
2016-01-26 00:56:46 +01:00
|
|
|
print("Could not read memcache key prefix file")
|
|
|
|
sys.exit(1)
|
2013-05-30 21:02:12 +02:00
|
|
|
|
|
|
|
return prefix
|
|
|
|
|
|
|
|
KEY_PREFIX = get_or_create_key_prefix()
|
|
|
|
|
2013-07-02 19:10:50 +02:00
|
|
|
def bounce_key_prefix_for_testing(test_name):
|
|
|
|
global KEY_PREFIX
|
|
|
|
KEY_PREFIX = test_name + ':' + str(os.getpid()) + ':'
|
|
|
|
|
2013-06-19 19:44:03 +02:00
|
|
|
def get_cache_backend(cache_name):
|
|
|
|
if cache_name is None:
|
|
|
|
return djcache
|
|
|
|
return get_cache(cache_name)
|
|
|
|
|
2013-05-11 15:50:02 +02:00
|
|
|
def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=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-06-20 16:41:23 +02:00
|
|
|
key = keyfunc(*args, **kwargs)
|
2013-05-10 16:57:06 +02:00
|
|
|
|
2013-06-18 21:08:16 +02:00
|
|
|
val = cache_get(key, cache_name=cache_name)
|
2012-09-19 18:41:20 +02:00
|
|
|
|
2013-05-11 15:50:02 +02:00
|
|
|
extra = ""
|
|
|
|
if cache_name == 'database':
|
|
|
|
extra = ".dbcache"
|
|
|
|
|
|
|
|
if with_statsd_key is not None:
|
|
|
|
metric_key = with_statsd_key
|
2013-04-16 22:58:21 +02:00
|
|
|
else:
|
2013-05-11 15:50:02 +02:00
|
|
|
metric_key = statsd_key(key)
|
|
|
|
|
|
|
|
status = "hit" if val is not None else "miss"
|
|
|
|
statsd.incr("cache%s.%s.%s" % (extra, metric_key, status))
|
2013-04-16 22:58:21 +02:00
|
|
|
|
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-04-19 00:00:33 +02:00
|
|
|
|
2013-06-18 21:08:16 +02:00
|
|
|
cache_set(key, val, cache_name=cache_name, timeout=timeout)
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2012-09-19 18:41:20 +02:00
|
|
|
return val
|
|
|
|
|
|
|
|
return func_with_caching
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2013-05-30 20:01:36 +02:00
|
|
|
def cache_set(key, val, cache_name=None, timeout=None):
|
|
|
|
memcached_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
cache_backend = get_cache_backend(cache_name)
|
2013-06-18 21:08:01 +02:00
|
|
|
ret = cache_backend.set(KEY_PREFIX + key, (val,), timeout=timeout)
|
2013-05-30 20:01:36 +02:00
|
|
|
memcached_stats_finish()
|
|
|
|
return ret
|
|
|
|
|
2013-06-18 21:08:16 +02:00
|
|
|
def cache_get(key, cache_name=None):
|
|
|
|
memcached_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
cache_backend = get_cache_backend(cache_name)
|
2013-06-18 21:08:16 +02:00
|
|
|
ret = cache_backend.get(KEY_PREFIX + key)
|
|
|
|
memcached_stats_finish()
|
|
|
|
return ret
|
|
|
|
|
2013-04-22 16:29:57 +02:00
|
|
|
def cache_get_many(keys, cache_name=None):
|
2013-05-30 21:02:12 +02:00
|
|
|
keys = [KEY_PREFIX + key for key in keys]
|
2013-05-10 16:57:06 +02:00
|
|
|
memcached_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
ret = get_cache_backend(cache_name).get_many(keys)
|
2013-05-10 16:57:06 +02:00
|
|
|
memcached_stats_finish()
|
2013-06-19 19:31:55 +02:00
|
|
|
return dict([(key[len(KEY_PREFIX):], value) for key, value in ret.items()])
|
2013-04-22 16:29:57 +02:00
|
|
|
|
2013-05-13 18:11:53 +02:00
|
|
|
def cache_set_many(items, cache_name=None, timeout=None):
|
2013-05-30 21:02:12 +02:00
|
|
|
new_items = {}
|
|
|
|
for key in items:
|
|
|
|
new_items[KEY_PREFIX + key] = items[key]
|
|
|
|
items = new_items
|
2013-05-10 16:57:06 +02:00
|
|
|
memcached_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
ret = get_cache_backend(cache_name).set_many(items, timeout=timeout)
|
2013-05-10 16:57:06 +02:00
|
|
|
memcached_stats_finish()
|
|
|
|
return ret
|
2013-04-25 20:41:54 +02:00
|
|
|
|
2013-08-28 00:19:54 +02:00
|
|
|
def cache_delete(key, cache_name=None):
|
|
|
|
memcached_stats_start()
|
|
|
|
get_cache_backend(cache_name).delete(KEY_PREFIX + key)
|
|
|
|
memcached_stats_finish()
|
|
|
|
|
2013-08-22 16:45:38 +02:00
|
|
|
def cache_delete_many(items, cache_name=None):
|
|
|
|
memcached_stats_start()
|
|
|
|
get_cache_backend(cache_name).delete_many(
|
|
|
|
KEY_PREFIX + item for item in items)
|
|
|
|
memcached_stats_finish()
|
|
|
|
|
2013-06-27 21:55:42 +02:00
|
|
|
# Required Arguments are as follows:
|
|
|
|
# * object_ids: The list of object ids to look up
|
|
|
|
# * cache_key_function: object_id => cache key
|
|
|
|
# * query_function: [object_ids] => [objects from database]
|
|
|
|
# Optional keyword arguments:
|
|
|
|
# * setter: Function to call before storing items to cache (e.g. compression)
|
|
|
|
# * extractor: Function to call on items returned from cache
|
|
|
|
# (e.g. decompression). Should be the inverse of the setter
|
|
|
|
# function.
|
|
|
|
# * id_fetcher: Function mapping an object from database => object_id
|
|
|
|
# (in case we're using a key more complex than obj.id)
|
|
|
|
# * cache_transformer: Function mapping an object from database =>
|
|
|
|
# value for cache (in case the values that we're caching are some
|
|
|
|
# function of the objects, not the objects themselves)
|
|
|
|
def generic_bulk_cached_fetch(cache_key_function, query_function, object_ids,
|
|
|
|
extractor=lambda obj: obj,
|
|
|
|
setter=lambda obj: obj,
|
|
|
|
id_fetcher=lambda obj: obj.id,
|
|
|
|
cache_transformer=lambda obj: obj):
|
|
|
|
cache_keys = {}
|
|
|
|
for object_id in object_ids:
|
|
|
|
cache_keys[object_id] = cache_key_function(object_id)
|
|
|
|
cached_objects = cache_get_many([cache_keys[object_id]
|
|
|
|
for object_id in object_ids])
|
|
|
|
for (key, val) in cached_objects.items():
|
|
|
|
cached_objects[key] = extractor(cached_objects[key][0])
|
|
|
|
needed_ids = [object_id for object_id in object_ids if
|
|
|
|
cache_keys[object_id] not in cached_objects]
|
|
|
|
db_objects = query_function(needed_ids)
|
|
|
|
|
2016-03-31 03:21:05 +02:00
|
|
|
items_for_remote_cache = {}
|
2013-06-27 21:55:42 +02:00
|
|
|
for obj in db_objects:
|
|
|
|
key = cache_keys[id_fetcher(obj)]
|
|
|
|
item = cache_transformer(obj)
|
2016-03-31 03:21:05 +02:00
|
|
|
items_for_remote_cache[key] = (setter(item),)
|
2013-06-27 21:55:42 +02:00
|
|
|
cached_objects[key] = item
|
2016-03-31 03:21:05 +02:00
|
|
|
if len(items_for_remote_cache) > 0:
|
|
|
|
cache_set_many(items_for_remote_cache)
|
2013-06-27 21:55:42 +02:00
|
|
|
return dict((object_id, cached_objects[cache_keys[object_id]]) for object_id in object_ids
|
|
|
|
if cache_keys[object_id] in cached_objects)
|
|
|
|
|
2012-09-19 18:41:20 +02:00
|
|
|
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."""
|
|
|
|
|
2015-11-01 17:09:54 +01:00
|
|
|
func_uniqifier = '%s-%s' % (func.__code__.co_filename, func.__name__)
|
2012-09-19 18:41:20 +02:00
|
|
|
|
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))
|
2015-11-01 17:10:46 +01:00
|
|
|
return key.replace('-', '--').replace(' ', '-s')
|
2012-09-19 18:41:20 +02:00
|
|
|
|
|
|
|
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-08-22 17:44:52 +02:00
|
|
|
def display_recipient_cache_key(recipient_id):
|
|
|
|
return "display_recipient_dict:%d" % (recipient_id,)
|
|
|
|
|
2013-03-18 20:56:32 +01:00
|
|
|
def user_profile_by_email_cache_key(email):
|
2013-07-29 23:03:31 +02:00
|
|
|
# See the comment in zerver/lib/avatar.py:gravatar_hash for why we
|
2013-03-20 15:31:27 +01:00
|
|
|
# are proactively encoding email addresses even though they will
|
|
|
|
# with high likelihood be ASCII-only for the foreseeable future.
|
2014-01-08 00:07:53 +01:00
|
|
|
return 'user_profile_by_email:%s' % (make_safe_digest(email.strip()),)
|
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-09-13 22:35:27 +02:00
|
|
|
def cache_save_user_profile(user_profile):
|
2013-09-15 19:21:01 +02:00
|
|
|
cache_set(user_profile_by_id_cache_key(user_profile.id), user_profile, timeout=3600*24*7)
|
2013-09-13 22:35:27 +02:00
|
|
|
|
2013-10-23 23:16:39 +02:00
|
|
|
def active_user_dicts_in_realm_cache_key(realm):
|
|
|
|
return "active_user_dicts_in_realm:%s" % (realm.id,)
|
|
|
|
|
2014-02-26 00:12:14 +01:00
|
|
|
def active_bot_dicts_in_realm_cache_key(realm):
|
|
|
|
return "active_bot_dicts_in_realm:%s" % (realm.id,)
|
|
|
|
|
2014-01-15 22:48:27 +01:00
|
|
|
def get_stream_cache_key(stream_name, realm):
|
|
|
|
from zerver.models import Realm
|
|
|
|
if isinstance(realm, Realm):
|
|
|
|
realm_id = realm.id
|
|
|
|
else:
|
|
|
|
realm_id = realm
|
|
|
|
return "stream_by_realm_and_name:%s:%s" % (
|
|
|
|
realm_id, make_safe_digest(stream_name.strip().lower()))
|
|
|
|
|
2014-01-28 20:05:52 +01:00
|
|
|
def update_user_profile_caches(user_profiles):
|
2016-03-31 03:21:05 +02:00
|
|
|
items_for_remote_cache = {}
|
2014-01-28 20:05:52 +01:00
|
|
|
for user_profile in user_profiles:
|
2016-03-31 03:21:05 +02:00
|
|
|
items_for_remote_cache[user_profile_by_email_cache_key(user_profile.email)] = (user_profile,)
|
|
|
|
items_for_remote_cache[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
|
|
|
|
cache_set_many(items_for_remote_cache)
|
2013-04-05 00:13:03 +02:00
|
|
|
|
2014-01-28 17:17:06 +01:00
|
|
|
# Called by models.py to flush the user_profile cache whenever we save
|
|
|
|
# a user_profile object
|
|
|
|
def flush_user_profile(sender, **kwargs):
|
|
|
|
user_profile = kwargs['instance']
|
2014-01-28 20:05:52 +01:00
|
|
|
update_user_profile_caches([user_profile])
|
2014-01-28 17:17:06 +01:00
|
|
|
|
2013-10-23 23:16:39 +02:00
|
|
|
# Invalidate our active_users_in_realm info dict if any user has changed
|
|
|
|
# name or email
|
|
|
|
if kwargs['update_fields'] is None or \
|
2013-11-15 21:50:55 +01:00
|
|
|
len(set(['full_name', 'short_name', 'email', 'is_active']) & set(kwargs['update_fields'])) > 0:
|
2013-10-23 23:16:39 +02:00
|
|
|
cache_delete(active_user_dicts_in_realm_cache_key(user_profile.realm))
|
|
|
|
|
2014-02-26 00:12:14 +01:00
|
|
|
# Invalidate our active_bots_in_realm info dict if any bot has changed
|
|
|
|
bot_fields = {'full_name', 'api_key', 'avatar_source',
|
|
|
|
'default_all_public_streams', 'is_active',
|
|
|
|
'default_sending_stream', 'default_events_register_stream'}
|
|
|
|
if user_profile.is_bot and (kwargs['update_fields'] is None or bot_fields & set(kwargs['update_fields'])):
|
|
|
|
cache_delete(active_bot_dicts_in_realm_cache_key(user_profile.realm))
|
|
|
|
|
2013-09-06 20:50:25 +02:00
|
|
|
# Invalidate realm-wide alert words cache if any user in the realm has changed
|
|
|
|
# alert words
|
|
|
|
if kwargs['update_fields'] is None or "alert_words" in kwargs['update_fields']:
|
2013-10-23 23:16:39 +02:00
|
|
|
cache_delete(realm_alert_words_cache_key(user_profile.realm))
|
2013-09-06 20:50:25 +02:00
|
|
|
|
2014-01-28 18:18:19 +01:00
|
|
|
# Called by models.py to flush various caches whenever we save
|
|
|
|
# a Realm object. The main tricky thing here is that Realm info is
|
|
|
|
# generally cached indirectly through user_profile objects.
|
|
|
|
def flush_realm(sender, **kwargs):
|
|
|
|
realm = kwargs['instance']
|
2014-01-28 20:05:52 +01:00
|
|
|
users = realm.get_active_users()
|
|
|
|
update_user_profile_caches(users)
|
2014-01-28 18:03:06 +01:00
|
|
|
|
|
|
|
if realm.deactivated:
|
|
|
|
cache_delete(active_user_dicts_in_realm_cache_key(realm))
|
2014-02-26 00:12:14 +01:00
|
|
|
cache_delete(active_bot_dicts_in_realm_cache_key(realm))
|
2014-01-28 18:03:06 +01:00
|
|
|
cache_delete(realm_alert_words_cache_key(realm))
|
2014-01-28 17:29:00 +01:00
|
|
|
|
2013-09-06 20:50:25 +02:00
|
|
|
def realm_alert_words_cache_key(realm):
|
|
|
|
return "realm_alert_words:%s" % (realm.domain,)
|
2014-01-15 22:48:27 +01:00
|
|
|
|
|
|
|
# Called by models.py to flush the stream cache whenever we save a stream
|
|
|
|
# object.
|
2014-01-28 20:49:55 +01:00
|
|
|
def flush_stream(sender, **kwargs):
|
2014-02-26 00:12:14 +01:00
|
|
|
from zerver.models import UserProfile
|
2014-01-15 22:48:27 +01:00
|
|
|
stream = kwargs['instance']
|
2016-03-31 03:21:05 +02:00
|
|
|
items_for_remote_cache = {}
|
|
|
|
items_for_remote_cache[get_stream_cache_key(stream.name, stream.realm)] = (stream,)
|
|
|
|
cache_set_many(items_for_remote_cache)
|
2014-02-26 00:12:14 +01:00
|
|
|
|
|
|
|
if kwargs['update_fields'] is None or 'name' in kwargs['update_fields'] and \
|
|
|
|
UserProfile.objects.filter(
|
|
|
|
Q(default_sending_stream=stream) |
|
|
|
|
Q(default_events_register_stream=stream)
|
|
|
|
).exists():
|
|
|
|
cache_delete(active_bot_dicts_in_realm_cache_key(stream.realm))
|