mirror of
https://github.com/zulip/zulip.git
synced 2025-11-18 12:54:58 +00:00
Use text_type as type of cache keys and update users.
This changes the type annotations for the cache keys in Zulip to be consistently text_type, and updates the annotations for values that are used as cache keys across the codebase.
This commit is contained in:
committed by
Tim Abbott
parent
d3b80d94a2
commit
53084fe03c
@@ -87,7 +87,7 @@ def bounce_key_prefix_for_testing(test_name):
|
||||
KEY_PREFIX = test_name + u':' + text_type(os.getpid()) + u':'
|
||||
|
||||
def get_cache_backend(cache_name):
|
||||
# type: (str) -> get_cache
|
||||
# type: (Optional[str]) -> get_cache
|
||||
if cache_name is None:
|
||||
return djcache
|
||||
return get_cache(cache_name)
|
||||
@@ -138,14 +138,14 @@ def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None)
|
||||
return decorator
|
||||
|
||||
def cache_set(key, val, cache_name=None, timeout=None):
|
||||
# type: (str, Any, Optional[str], Optional[int]) -> None
|
||||
# type: (text_type, Any, Optional[str], Optional[int]) -> None
|
||||
remote_cache_stats_start()
|
||||
cache_backend = get_cache_backend(cache_name)
|
||||
cache_backend.set(KEY_PREFIX + key, (val,), timeout=timeout)
|
||||
remote_cache_stats_finish()
|
||||
|
||||
def cache_get(key, cache_name=None):
|
||||
# type: (str, Optional[str]) -> Any
|
||||
# type: (text_type, Optional[str]) -> Any
|
||||
remote_cache_stats_start()
|
||||
cache_backend = get_cache_backend(cache_name)
|
||||
ret = cache_backend.get(KEY_PREFIX + key)
|
||||
@@ -153,31 +153,31 @@ def cache_get(key, cache_name=None):
|
||||
return ret
|
||||
|
||||
def cache_get_many(keys, cache_name=None):
|
||||
# type: (List[str], Optional[str]) -> Dict[str, Any]
|
||||
keys = [KEY_PREFIX + key for key in keys] # type: ignore # temporary
|
||||
# type: (List[text_type], Optional[str]) -> Dict[text_type, Any]
|
||||
keys = [KEY_PREFIX + key for key in keys]
|
||||
remote_cache_stats_start()
|
||||
ret = get_cache_backend(cache_name).get_many(keys)
|
||||
remote_cache_stats_finish()
|
||||
return dict([(key[len(KEY_PREFIX):], value) for key, value in ret.items()])
|
||||
|
||||
def cache_set_many(items, cache_name=None, timeout=None):
|
||||
# type: (Dict[str, Any], Optional[str], Optional[int]) -> None
|
||||
# type: (Dict[text_type, Any], Optional[str], Optional[int]) -> None
|
||||
new_items = {}
|
||||
for key in items:
|
||||
new_items[KEY_PREFIX + key] = items[key]
|
||||
items = new_items # type: ignore # temporary
|
||||
items = new_items
|
||||
remote_cache_stats_start()
|
||||
get_cache_backend(cache_name).set_many(items, timeout=timeout)
|
||||
remote_cache_stats_finish()
|
||||
|
||||
def cache_delete(key, cache_name=None):
|
||||
# type: (str, Optional[str]) -> None
|
||||
# type: (text_type, Optional[str]) -> None
|
||||
remote_cache_stats_start()
|
||||
get_cache_backend(cache_name).delete(KEY_PREFIX + key)
|
||||
remote_cache_stats_finish()
|
||||
|
||||
def cache_delete_many(items, cache_name=None):
|
||||
# type: (Iterable[str], Optional[str]) -> None
|
||||
# type: (Iterable[text_type], Optional[str]) -> None
|
||||
remote_cache_stats_start()
|
||||
get_cache_backend(cache_name).delete_many(
|
||||
KEY_PREFIX + item for item in items)
|
||||
@@ -202,8 +202,8 @@ def generic_bulk_cached_fetch(cache_key_function, query_function, object_ids,
|
||||
setter=lambda obj: obj,
|
||||
id_fetcher=lambda obj: obj.id,
|
||||
cache_transformer=lambda obj: obj):
|
||||
# type: (Callable[[Any], str], Callable[[List[Any]], List[Any]], List[Any], Callable[[Any], Any], Callable[[Any], Any], Callable[[Any], Any], Callable[[Any], Any]) -> Dict[Any, Any]
|
||||
cache_keys = {} # type: Dict[int, str]
|
||||
# type: (Callable[[Any], text_type], Callable[[List[Any]], Iterable[Any]], Iterable[Any], Callable[[Any], Any], Callable[[Any], Any], Callable[[Any], Any], Callable[[Any], Any]) -> Dict[Any, Any]
|
||||
cache_keys = {} # type: Dict[Any, text_type]
|
||||
for object_id in object_ids:
|
||||
cache_keys[object_id] = cache_key_function(object_id)
|
||||
cached_objects = cache_get_many([cache_keys[object_id]
|
||||
@@ -214,7 +214,7 @@ def generic_bulk_cached_fetch(cache_key_function, query_function, object_ids,
|
||||
cache_keys[object_id] not in cached_objects]
|
||||
db_objects = query_function(needed_ids)
|
||||
|
||||
items_for_remote_cache = {} # type: Dict[str, Any]
|
||||
items_for_remote_cache = {} # type: Dict[text_type, Any]
|
||||
for obj in db_objects:
|
||||
key = cache_keys[id_fetcher(obj)]
|
||||
item = cache_transformer(obj)
|
||||
@@ -244,23 +244,23 @@ def cache(func):
|
||||
return cache_with_key(keyfunc)(func)
|
||||
|
||||
def message_cache_key(message_id):
|
||||
# type: (int) -> str
|
||||
return "message:%d" % (message_id,)
|
||||
# type: (int) -> text_type
|
||||
return u"message:%d" % (message_id,)
|
||||
|
||||
def display_recipient_cache_key(recipient_id):
|
||||
# type: (int) -> str
|
||||
return "display_recipient_dict:%d" % (recipient_id,)
|
||||
# type: (int) -> text_type
|
||||
return u"display_recipient_dict:%d" % (recipient_id,)
|
||||
|
||||
def user_profile_by_email_cache_key(email):
|
||||
# type: (str) -> str
|
||||
# type: (text_type) -> text_type
|
||||
# See the comment in zerver/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.strip()),)
|
||||
return u'user_profile_by_email:%s' % (make_safe_digest(email.strip()),)
|
||||
|
||||
def user_profile_by_id_cache_key(user_profile_id):
|
||||
# type: (int) -> str
|
||||
return "user_profile_by_id:%s" % (user_profile_id,)
|
||||
# type: (int) -> text_type
|
||||
return u"user_profile_by_id:%s" % (user_profile_id,)
|
||||
|
||||
# TODO: Refactor these cache helpers into another file that can import
|
||||
# models.py so that we can replace many of these type: Anys
|
||||
@@ -271,8 +271,8 @@ def cache_save_user_profile(user_profile):
|
||||
|
||||
active_user_dict_fields = ['id', 'full_name', 'short_name', 'email', 'is_realm_admin', 'is_bot'] # type: List[str]
|
||||
def active_user_dicts_in_realm_cache_key(realm):
|
||||
# type: (Any) -> str
|
||||
return "active_user_dicts_in_realm:%s" % (realm.id,)
|
||||
# type: (Any) -> text_type
|
||||
return u"active_user_dicts_in_realm:%s" % (realm.id,)
|
||||
|
||||
active_bot_dict_fields = ['id', 'full_name', 'short_name',
|
||||
'email', 'default_sending_stream__name',
|
||||
@@ -280,17 +280,17 @@ active_bot_dict_fields = ['id', 'full_name', 'short_name',
|
||||
'default_all_public_streams', 'api_key',
|
||||
'bot_owner__email', 'avatar_source'] # type: List[str]
|
||||
def active_bot_dicts_in_realm_cache_key(realm):
|
||||
# type: (Any) -> str
|
||||
return "active_bot_dicts_in_realm:%s" % (realm.id,)
|
||||
# type: (Any) -> text_type
|
||||
return u"active_bot_dicts_in_realm:%s" % (realm.id,)
|
||||
|
||||
def get_stream_cache_key(stream_name, realm):
|
||||
# type: (six.text_type, Any) -> str
|
||||
# type: (text_type, Any) -> text_type
|
||||
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" % (
|
||||
return u"stream_by_realm_and_name:%s:%s" % (
|
||||
realm_id, make_safe_digest(stream_name.strip().lower()))
|
||||
|
||||
def update_user_profile_caches(user_profiles):
|
||||
@@ -341,8 +341,8 @@ def flush_realm(sender, **kwargs):
|
||||
cache_delete(realm_alert_words_cache_key(realm))
|
||||
|
||||
def realm_alert_words_cache_key(realm):
|
||||
# type: (Any) -> str
|
||||
return "realm_alert_words:%s" % (realm.domain,)
|
||||
# type: (Any) -> text_type
|
||||
return u"realm_alert_words:%s" % (realm.domain,)
|
||||
|
||||
# Called by models.py to flush the stream cache whenever we save a stream
|
||||
# object.
|
||||
|
||||
Reference in New Issue
Block a user