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
|
2016-10-10 15:09:41 +02:00
|
|
|
from django.core.cache import caches
|
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
|
2016-06-11 12:18:44 +02:00
|
|
|
from django.core.cache.backends.base import BaseCache
|
2012-09-19 18:41:20 +02:00
|
|
|
|
2017-10-28 18:57:57 +02:00
|
|
|
from typing import cast, Any, Callable, Dict, Iterable, List, Optional, Union, Set, TypeVar, Text, Tuple
|
2016-04-21 05:56:40 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.utils import statsd, statsd_key, make_safe_digest
|
2016-07-18 23:25:01 +02:00
|
|
|
import subprocess
|
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 hashlib
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2016-06-10 18:06:39 +02:00
|
|
|
if False:
|
2017-11-27 05:27:04 +01:00
|
|
|
from zerver.models import UserProfile, Realm, Message
|
2016-06-10 18:06:39 +02:00
|
|
|
# These modules have to be imported for type annotations but
|
|
|
|
# they cannot be imported at runtime due to cyclic dependency.
|
|
|
|
|
2017-10-24 04:52:04 +02:00
|
|
|
ReturnT = TypeVar('ReturnT') # Useful for matching return types via Callable[..., ReturnT]
|
2016-07-22 15:10:19 +02:00
|
|
|
|
2016-10-27 12:06:44 +02:00
|
|
|
class NotFoundInCache(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-03-31 03:23:21 +02:00
|
|
|
remote_cache_time_start = 0.0
|
2016-03-31 03:25:46 +02:00
|
|
|
remote_cache_total_time = 0.0
|
|
|
|
remote_cache_total_requests = 0
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_remote_cache_time() -> float:
|
2016-03-31 03:25:46 +02:00
|
|
|
return remote_cache_total_time
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_remote_cache_requests() -> int:
|
2016-03-31 03:25:46 +02:00
|
|
|
return remote_cache_total_requests
|
2013-04-19 00:00:33 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def remote_cache_stats_start() -> None:
|
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
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def remote_cache_stats_finish() -> None:
|
2016-03-31 03:25:46 +02:00
|
|
|
global remote_cache_total_time
|
|
|
|
global remote_cache_total_requests
|
2016-03-31 03:23:21 +02:00
|
|
|
global remote_cache_time_start
|
2016-03-31 03:25:46 +02:00
|
|
|
remote_cache_total_requests += 1
|
|
|
|
remote_cache_total_time += (time.time() - remote_cache_time_start)
|
2013-04-16 22:58:21 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_or_create_key_prefix() -> Text:
|
2016-12-15 07:02:42 +01:00
|
|
|
if settings.CASPER_TESTS:
|
2016-11-21 09:18:50 +01:00
|
|
|
# This sets the prefix for the benefit of the Casper tests.
|
|
|
|
#
|
|
|
|
# Having a fixed key is OK since we don't support running
|
|
|
|
# multiple copies of the casper tests at the same time anyway.
|
2017-11-03 03:12:25 +01:00
|
|
|
return 'casper_tests:'
|
2016-12-15 07:02:42 +01:00
|
|
|
elif settings.TEST_SUITE:
|
|
|
|
# The Python tests overwrite KEY_PREFIX on each test, but use
|
|
|
|
# this codepath as well, just to save running the more complex
|
|
|
|
# code below for reading the normal key prefix.
|
2017-11-03 03:12:25 +01:00
|
|
|
return 'django_tests_unused:'
|
2016-12-15 07:02:42 +01:00
|
|
|
|
2016-07-14 05:21:59 +02:00
|
|
|
# directory `var` should exist in production
|
2016-07-18 23:25:01 +02:00
|
|
|
subprocess.check_call(["mkdir", "-p", os.path.join(settings.DEPLOY_ROOT, "var")])
|
2013-07-03 01:26:00 +02:00
|
|
|
|
2016-07-14 05:21:59 +02:00
|
|
|
filename = os.path.join(settings.DEPLOY_ROOT, "var", "remote_cache_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)
|
2016-12-27 07:09:35 +01:00
|
|
|
random_hash = hashlib.sha256(Text(random.getrandbits(256)).encode('utf-8')).digest()
|
2016-06-09 22:40:04 +02:00
|
|
|
prefix = base64.b16encode(random_hash)[:32].decode('utf-8').lower() + ':'
|
2013-05-30 21:02:12 +02:00
|
|
|
# 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-03-31 03:32:06 +02:00
|
|
|
print("Could not read remote cache key prefix file")
|
2016-01-26 00:56:46 +01:00
|
|
|
sys.exit(1)
|
2013-05-30 21:02:12 +02:00
|
|
|
|
|
|
|
return prefix
|
|
|
|
|
2017-05-07 17:02:58 +02:00
|
|
|
KEY_PREFIX = get_or_create_key_prefix() # type: Text
|
2013-05-30 21:02:12 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def bounce_key_prefix_for_testing(test_name: Text) -> None:
|
2013-07-02 19:10:50 +02:00
|
|
|
global KEY_PREFIX
|
2017-11-03 03:12:25 +01:00
|
|
|
KEY_PREFIX = test_name + ':' + Text(os.getpid()) + ':'
|
2017-05-05 15:19:11 +02:00
|
|
|
# We are taking the hash of the KEY_PREFIX to decrease the size of the key.
|
|
|
|
# Memcached keys should have a length of less than 256.
|
|
|
|
KEY_PREFIX = hashlib.sha1(KEY_PREFIX.encode('utf-8')).hexdigest()
|
2013-07-02 19:10:50 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_cache_backend(cache_name: Optional[str]) -> BaseCache:
|
2013-06-19 19:44:03 +02:00
|
|
|
if cache_name is None:
|
|
|
|
return djcache
|
2016-10-10 15:09:41 +02:00
|
|
|
return caches[cache_name]
|
2013-06-19 19:44:03 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_cache_with_key(keyfunc: Any, cache_name: Optional[str]=None) -> Any:
|
2016-10-27 12:06:44 +02:00
|
|
|
"""
|
|
|
|
The main goal of this function getting value from the cache like in the "cache_with_key".
|
|
|
|
A cache value can contain any data including the "None", so
|
|
|
|
here used exception for case if value isn't found in the cache.
|
|
|
|
"""
|
2017-11-05 11:15:10 +01:00
|
|
|
def decorator(func: Callable[..., Any]) -> (Callable[..., Any]):
|
2016-10-27 12:06:44 +02:00
|
|
|
@wraps(func)
|
2017-11-05 11:15:10 +01:00
|
|
|
def func_with_caching(*args: Any, **kwargs: Any) -> Callable[..., Any]:
|
2016-10-27 12:06:44 +02:00
|
|
|
key = keyfunc(*args, **kwargs)
|
|
|
|
val = cache_get(key, cache_name=cache_name)
|
|
|
|
if val is not None:
|
|
|
|
return val[0]
|
|
|
|
raise NotFoundInCache()
|
|
|
|
|
|
|
|
return func_with_caching
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2013-05-11 15:50:02 +02:00
|
|
|
def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None):
|
2017-10-24 04:52:04 +02:00
|
|
|
# type: (Callable[..., Text], Optional[str], Optional[int], Optional[str]) -> Callable[[Callable[..., ReturnT]], Callable[..., ReturnT]]
|
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."""
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def decorator(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
|
2012-11-02 00:23:26 +01:00
|
|
|
@wraps(func)
|
2017-11-05 11:15:10 +01:00
|
|
|
def func_with_caching(*args: Any, **kwargs: Any) -> ReturnT:
|
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
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_set(key: Text, val: Any, cache_name: Optional[str]=None, timeout: Optional[int]=None) -> None:
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
cache_backend = get_cache_backend(cache_name)
|
2016-06-09 23:35:53 +02:00
|
|
|
cache_backend.set(KEY_PREFIX + key, (val,), timeout=timeout)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_finish()
|
2013-05-30 20:01:36 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_get(key: Text, cache_name: Optional[str]=None) -> Any:
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_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)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_finish()
|
2013-06-18 21:08:16 +02:00
|
|
|
return ret
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_get_many(keys: List[Text], cache_name: Optional[str]=None) -> Dict[Text, Any]:
|
2016-06-10 00:12:34 +02:00
|
|
|
keys = [KEY_PREFIX + key for key in keys]
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_start()
|
2013-06-19 19:44:03 +02:00
|
|
|
ret = get_cache_backend(cache_name).get_many(keys)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_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
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_set_many(items: Dict[Text, Any], cache_name: Optional[str]=None,
|
|
|
|
timeout: Optional[int]=None) -> None:
|
2013-05-30 21:02:12 +02:00
|
|
|
new_items = {}
|
|
|
|
for key in items:
|
|
|
|
new_items[KEY_PREFIX + key] = items[key]
|
2016-06-10 00:12:34 +02:00
|
|
|
items = new_items
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_start()
|
2016-06-09 23:35:53 +02:00
|
|
|
get_cache_backend(cache_name).set_many(items, timeout=timeout)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_finish()
|
2013-04-25 20:41:54 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_delete(key: Text, cache_name: Optional[str]=None) -> None:
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_start()
|
2013-08-28 00:19:54 +02:00
|
|
|
get_cache_backend(cache_name).delete(KEY_PREFIX + key)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_finish()
|
2013-08-28 00:19:54 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache_delete_many(items: Iterable[Text], cache_name: Optional[str]=None) -> None:
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_start()
|
2013-08-22 16:45:38 +02:00
|
|
|
get_cache_backend(cache_name).delete_many(
|
|
|
|
KEY_PREFIX + item for item in items)
|
2016-03-31 03:25:05 +02:00
|
|
|
remote_cache_stats_finish()
|
2013-08-22 16:45:38 +02:00
|
|
|
|
2017-10-28 18:57:57 +02:00
|
|
|
# Generic_bulk_cached fetch and its helpers
|
|
|
|
ObjKT = TypeVar('ObjKT')
|
|
|
|
ItemT = TypeVar('ItemT')
|
|
|
|
CompressedItemT = TypeVar('CompressedItemT')
|
|
|
|
|
|
|
|
def default_extractor(obj: CompressedItemT) -> ItemT:
|
|
|
|
return obj # type: ignore # Need a type assert that ItemT=CompressedItemT
|
|
|
|
|
|
|
|
def default_setter(obj: ItemT) -> CompressedItemT:
|
|
|
|
return obj # type: ignore # Need a type assert that ItemT=CompressedItemT
|
|
|
|
|
|
|
|
def default_id_fetcher(obj: ItemT) -> ObjKT:
|
|
|
|
return obj.id # type: ignore # Need ItemT/CompressedItemT typevars to be a Django protocol
|
|
|
|
|
|
|
|
def default_cache_transformer(obj: ItemT) -> ItemT:
|
|
|
|
return obj
|
2017-10-28 19:00:43 +02:00
|
|
|
|
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)
|
2017-10-28 18:57:57 +02:00
|
|
|
def generic_bulk_cached_fetch(
|
|
|
|
cache_key_function: Callable[[ObjKT], Text],
|
|
|
|
query_function: Callable[[List[ObjKT]], Iterable[Any]],
|
|
|
|
object_ids: Iterable[ObjKT],
|
|
|
|
extractor: Callable[[CompressedItemT], ItemT] = default_extractor,
|
|
|
|
setter: Callable[[ItemT], CompressedItemT] = default_setter,
|
|
|
|
id_fetcher: Callable[[ItemT], ObjKT] = default_id_fetcher,
|
|
|
|
cache_transformer: Callable[[ItemT], ItemT] = default_cache_transformer
|
|
|
|
) -> Dict[ObjKT, ItemT]:
|
2017-05-07 17:02:58 +02:00
|
|
|
cache_keys = {} # type: Dict[ObjKT, Text]
|
2013-06-27 21:55:42 +02:00
|
|
|
for object_id in object_ids:
|
|
|
|
cache_keys[object_id] = cache_key_function(object_id)
|
2017-10-28 19:01:44 +02:00
|
|
|
cached_objects_compressed = cache_get_many([cache_keys[object_id]
|
2017-10-28 18:57:57 +02:00
|
|
|
for object_id in object_ids]) # type: Dict[Text, Tuple[CompressedItemT]]
|
2017-10-28 19:01:44 +02:00
|
|
|
cached_objects = {} # type: Dict[Text, ItemT]
|
|
|
|
for (key, val) in cached_objects_compressed.items():
|
|
|
|
cached_objects[key] = extractor(cached_objects_compressed[key][0])
|
2013-06-27 21:55:42 +02:00
|
|
|
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)
|
|
|
|
|
2017-10-28 18:57:57 +02:00
|
|
|
items_for_remote_cache = {} # type: Dict[Text, Tuple[CompressedItemT]]
|
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)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def cache(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
|
2012-09-19 18:41:20 +02:00
|
|
|
"""Decorator which applies Django caching to a function.
|
|
|
|
|
|
|
|
Uses a key based on the function's name, filename, and
|
|
|
|
the repr() of its arguments."""
|
|
|
|
|
2017-08-25 20:01:20 +02: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)
|
2017-11-05 11:15:10 +01:00
|
|
|
def keyfunc(*args: Any, **kwargs: Any) -> str:
|
2012-09-19 18:41:20 +02:00
|
|
|
# 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
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def display_recipient_cache_key(recipient_id: int) -> Text:
|
2016-06-10 00:12:34 +02:00
|
|
|
return u"display_recipient_dict:%d" % (recipient_id,)
|
2013-08-22 17:44:52 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_profile_by_email_cache_key(email: Text) -> Text:
|
2016-09-28 00:12:18 +02:00
|
|
|
# See the comment in zerver/lib/avatar_hash.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.
|
2017-11-03 03:12:25 +01:00
|
|
|
return 'user_profile_by_email:%s' % (make_safe_digest(email.strip()),)
|
2013-03-13 18:49:29 +01:00
|
|
|
|
2017-11-27 23:33:13 +01:00
|
|
|
def user_profile_cache_key_id(email, realm_id):
|
|
|
|
# type: (Text, int) -> Text
|
|
|
|
return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm_id,)
|
|
|
|
|
2017-05-22 19:45:54 +02:00
|
|
|
def user_profile_cache_key(email, realm):
|
|
|
|
# type: (Text, Realm) -> Text
|
2017-11-27 23:33:13 +01:00
|
|
|
return user_profile_cache_key_id(email, realm.id)
|
2017-05-22 19:45:54 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def bot_profile_cache_key(email: Text) -> Text:
|
2017-05-22 23:37:15 +02:00
|
|
|
return u"bot_profile:%s" % (make_safe_digest(email.strip()))
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_profile_by_id_cache_key(user_profile_id: int) -> Text:
|
2016-06-10 00:12:34 +02:00
|
|
|
return u"user_profile_by_id:%s" % (user_profile_id,)
|
2013-03-18 17:10:45 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_profile_by_api_key_cache_key(api_key: Text) -> Text:
|
2017-08-25 07:43:38 +02:00
|
|
|
return u"user_profile_by_api_key:%s" % (api_key,)
|
|
|
|
|
2016-04-21 05:56:40 +02:00
|
|
|
# TODO: Refactor these cache helpers into another file that can import
|
2016-12-18 23:11:24 +01:00
|
|
|
# models.py so that python v3 style type annotations can also work.
|
2016-04-21 05:56:40 +02:00
|
|
|
|
2017-10-21 18:20:49 +02:00
|
|
|
realm_user_dict_fields = [
|
2017-02-21 17:55:32 +01:00
|
|
|
'id', 'full_name', 'short_name', 'email',
|
2017-10-21 18:20:49 +02:00
|
|
|
'avatar_source', 'avatar_version', 'is_active',
|
2017-05-10 06:54:49 +02:00
|
|
|
'is_realm_admin', 'is_bot', 'realm_id', 'timezone'] # type: List[str]
|
2017-02-21 17:55:32 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def realm_user_dicts_cache_key(realm_id: int) -> Text:
|
2017-10-21 18:20:49 +02:00
|
|
|
return u"realm_user_dicts:%s" % (realm_id,)
|
2013-10-23 23:16:39 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def active_user_ids_cache_key(realm_id: int) -> Text:
|
2017-09-16 21:44:03 +02:00
|
|
|
return u"active_user_ids:%s" % (realm_id,)
|
|
|
|
|
2017-06-12 19:50:03 +02:00
|
|
|
bot_dict_fields = ['id', 'full_name', 'short_name', 'bot_type', 'email',
|
2017-02-06 20:45:26 +01:00
|
|
|
'is_active', 'default_sending_stream__name',
|
2017-05-10 07:09:28 +02:00
|
|
|
'realm_id',
|
2017-02-06 20:45:26 +01:00
|
|
|
'default_events_register_stream__name',
|
|
|
|
'default_all_public_streams', 'api_key',
|
|
|
|
'bot_owner__email', 'avatar_source',
|
2017-05-07 17:02:58 +02:00
|
|
|
'avatar_version'] # type: List[str]
|
2017-02-06 20:45:26 +01:00
|
|
|
|
|
|
|
def bot_dicts_in_realm_cache_key(realm):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Realm) -> Text
|
2017-02-06 20:45:26 +01:00
|
|
|
return u"bot_dicts_in_realm:%s" % (realm.id,)
|
2014-02-26 00:12:14 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_stream_cache_key(stream_name: Text, realm_id: int) -> Text:
|
2016-06-10 00:12:34 +02:00
|
|
|
return u"stream_by_realm_and_name:%s:%s" % (
|
2014-01-15 22:48:27 +01:00
|
|
|
realm_id, make_safe_digest(stream_name.strip().lower()))
|
|
|
|
|
2016-07-27 10:05:37 +02:00
|
|
|
def delete_user_profile_caches(user_profiles):
|
2016-06-10 18:06:39 +02:00
|
|
|
# type: (Iterable[UserProfile]) -> None
|
2016-07-27 10:05:37 +02:00
|
|
|
keys = []
|
2014-01-28 20:05:52 +01:00
|
|
|
for user_profile in user_profiles:
|
2016-07-27 10:05:37 +02:00
|
|
|
keys.append(user_profile_by_email_cache_key(user_profile.email))
|
|
|
|
keys.append(user_profile_by_id_cache_key(user_profile.id))
|
2017-08-25 07:43:38 +02:00
|
|
|
keys.append(user_profile_by_api_key_cache_key(user_profile.api_key))
|
2017-05-22 19:45:54 +02:00
|
|
|
keys.append(user_profile_cache_key(user_profile.email, user_profile.realm))
|
2016-07-27 10:05:37 +02:00
|
|
|
|
|
|
|
cache_delete_many(keys)
|
2013-04-05 00:13:03 +02:00
|
|
|
|
2017-01-23 12:11:04 +01:00
|
|
|
def delete_display_recipient_cache(user_profile):
|
|
|
|
# type: (UserProfile) -> None
|
|
|
|
from zerver.models import Subscription # We need to import here to avoid cyclic dependency.
|
|
|
|
recipient_ids = Subscription.objects.filter(user_profile=user_profile)
|
|
|
|
recipient_ids = recipient_ids.values_list('recipient_id', flat=True)
|
|
|
|
keys = [display_recipient_cache_key(rid) for rid in recipient_ids]
|
|
|
|
cache_delete_many(keys)
|
|
|
|
|
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
|
2017-11-05 11:15:10 +01:00
|
|
|
def flush_user_profile(sender: Any, **kwargs: Any) -> None:
|
2014-01-28 17:17:06 +01:00
|
|
|
user_profile = kwargs['instance']
|
2016-07-27 10:05:37 +02:00
|
|
|
delete_user_profile_caches([user_profile])
|
2014-01-28 17:17:06 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def changed(fields: List[str]) -> bool:
|
2017-10-22 03:14:20 +02:00
|
|
|
if kwargs.get('update_fields') is None:
|
|
|
|
# adds/deletes should invalidate the cache
|
|
|
|
return True
|
|
|
|
|
|
|
|
update_fields = set(kwargs['update_fields'])
|
|
|
|
for f in fields:
|
|
|
|
if f in update_fields:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2013-10-23 23:16:39 +02:00
|
|
|
# Invalidate our active_users_in_realm info dict if any user has changed
|
2016-04-27 23:44:26 +02:00
|
|
|
# the fields in the dict or become (in)active
|
2017-10-22 03:14:20 +02:00
|
|
|
if changed(realm_user_dict_fields):
|
2017-10-21 18:20:49 +02:00
|
|
|
cache_delete(realm_user_dicts_cache_key(user_profile.realm_id))
|
2013-10-23 23:16:39 +02:00
|
|
|
|
2017-10-22 03:14:20 +02:00
|
|
|
if changed(['is_active']):
|
2017-09-16 21:44:03 +02:00
|
|
|
cache_delete(active_user_ids_cache_key(user_profile.realm_id))
|
|
|
|
|
2017-10-22 03:14:44 +02:00
|
|
|
if changed(['email', 'full_name', 'short_name', 'id', 'is_mirror_dummy']):
|
2017-01-23 12:11:04 +01:00
|
|
|
delete_display_recipient_cache(user_profile)
|
|
|
|
|
2017-02-06 20:45:26 +01:00
|
|
|
# Invalidate our bots_in_realm info dict if any bot has
|
2016-04-27 23:57:38 +02:00
|
|
|
# changed the fields in the dict or become (in)active
|
2017-10-22 03:14:20 +02:00
|
|
|
if user_profile.is_bot and changed(bot_dict_fields):
|
2017-02-06 20:45:26 +01:00
|
|
|
cache_delete(bot_dicts_in_realm_cache_key(user_profile.realm))
|
2014-02-26 00:12:14 +01:00
|
|
|
|
2013-09-06 20:50:25 +02:00
|
|
|
# Invalidate realm-wide alert words cache if any user in the realm has changed
|
|
|
|
# alert words
|
2017-10-22 03:14:20 +02:00
|
|
|
if changed(['alert_words']):
|
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.
|
2017-11-05 11:15:10 +01:00
|
|
|
def flush_realm(sender: Any, **kwargs: Any) -> None:
|
2014-01-28 18:18:19 +01:00
|
|
|
realm = kwargs['instance']
|
2014-01-28 20:05:52 +01:00
|
|
|
users = realm.get_active_users()
|
2016-07-27 10:05:37 +02:00
|
|
|
delete_user_profile_caches(users)
|
2014-01-28 18:03:06 +01:00
|
|
|
|
|
|
|
if realm.deactivated:
|
2017-10-21 18:20:49 +02:00
|
|
|
cache_delete(realm_user_dicts_cache_key(realm.id))
|
2017-09-16 21:44:03 +02:00
|
|
|
cache_delete(active_user_ids_cache_key(realm.id))
|
2017-02-06 20:45:26 +01:00
|
|
|
cache_delete(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):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Realm) -> Text
|
2017-03-13 19:00:41 +01:00
|
|
|
return u"realm_alert_words:%s" % (realm.string_id,)
|
2014-01-15 22:48:27 +01:00
|
|
|
|
|
|
|
# Called by models.py to flush the stream cache whenever we save a stream
|
|
|
|
# object.
|
2017-11-05 11:15:10 +01:00
|
|
|
def flush_stream(sender: Any, **kwargs: Any) -> None:
|
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 = {}
|
2017-09-17 22:26:43 +02:00
|
|
|
items_for_remote_cache[get_stream_cache_key(stream.name, stream.realm_id)] = (stream,)
|
2016-03-31 03:21:05 +02:00
|
|
|
cache_set_many(items_for_remote_cache)
|
2014-02-26 00:12:14 +01:00
|
|
|
|
2016-04-15 00:32:47 +02:00
|
|
|
if kwargs.get('update_fields') is None or 'name' in kwargs['update_fields'] and \
|
2014-02-26 00:12:14 +01:00
|
|
|
UserProfile.objects.filter(
|
|
|
|
Q(default_sending_stream=stream) |
|
2017-01-24 06:02:39 +01:00
|
|
|
Q(default_events_register_stream=stream)).exists():
|
2017-02-06 20:45:26 +01:00
|
|
|
cache_delete(bot_dicts_in_realm_cache_key(stream.realm))
|
2016-10-04 15:40:02 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def to_dict_cache_key_id(message_id: int) -> Text:
|
2017-11-03 03:12:25 +01:00
|
|
|
return 'message_dict:%d' % (message_id,)
|
2016-10-04 15:40:02 +02:00
|
|
|
|
2017-10-20 20:29:49 +02:00
|
|
|
def to_dict_cache_key(message):
|
|
|
|
# type: (Message) -> Text
|
|
|
|
return to_dict_cache_key_id(message.id)
|
2016-10-04 15:40:02 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def flush_message(sender: Any, **kwargs: Any) -> None:
|
2016-10-04 15:40:02 +02:00
|
|
|
message = kwargs['instance']
|
2017-10-20 20:29:49 +02:00
|
|
|
cache_delete(to_dict_cache_key_id(message.id))
|