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:
Eklavya Sharma
2016-06-10 03:42:34 +05:30
committed by Tim Abbott
parent d3b80d94a2
commit 53084fe03c
5 changed files with 49 additions and 47 deletions

View File

@@ -1,4 +1,6 @@
from __future__ import absolute_import
from six import text_type
from typing import Any, Dict, Callable, Tuple
# This file needs to be different from cache.py because cache.py
@@ -37,33 +39,33 @@ def message_fetch_objects():
id__gt=max_id - MESSAGE_CACHE_SIZE)
def message_cache_items(items_for_remote_cache, message):
# type: (Dict[str, Tuple[Message]], Message) -> None
# type: (Dict[text_type, Tuple[Message]], Message) -> None
items_for_remote_cache[message_cache_key(message.id)] = (message,)
def user_cache_items(items_for_remote_cache, user_profile):
# type: (Dict[str, Tuple[UserProfile]], UserProfile) -> None
# type: (Dict[text_type, Tuple[UserProfile]], UserProfile) -> None
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,)
def stream_cache_items(items_for_remote_cache, stream):
# type: (Dict[str, Tuple[Stream]], Stream) -> None
# type: (Dict[text_type, Tuple[Stream]], Stream) -> None
items_for_remote_cache[get_stream_cache_key(stream.name, stream.realm_id)] = (stream,)
def client_cache_items(items_for_remote_cache, client):
# type: (Dict[str, Tuple[Client]], Client) -> None
# type: (Dict[text_type, Tuple[Client]], Client) -> None
items_for_remote_cache[get_client_cache_key(client.name)] = (client,)
def huddle_cache_items(items_for_remote_cache, huddle):
# type: (Dict[str, Tuple[Huddle]], Huddle) -> None
# type: (Dict[text_type, Tuple[Huddle]], Huddle) -> None
items_for_remote_cache[huddle_hash_cache_key(huddle.huddle_hash)] = (huddle,)
def recipient_cache_items(items_for_remote_cache, recipient):
# type: (Dict[str, Tuple[Recipient]], Recipient) -> None
# type: (Dict[text_type, Tuple[Recipient]], Recipient) -> None
items_for_remote_cache[get_recipient_cache_key(recipient.type, recipient.type_id)] = (recipient,)
session_engine = import_module(settings.SESSION_ENGINE)
def session_cache_items(items_for_remote_cache, session):
# type: (Dict[str, str], Session) -> None
# type: (Dict[text_type, text_type], Session) -> None
store = session_engine.SessionStore(session_key=session.session_key)
items_for_remote_cache[store.cache_key] = store.decode(session.session_data)
@@ -81,13 +83,13 @@ cache_fillers = {
'message': (message_fetch_objects, message_cache_items, 3600 * 24, 1000),
'huddle': (lambda: Huddle.objects.select_related().all(), huddle_cache_items, 3600*24*7, 10000),
'session': (lambda: Session.objects.all(), session_cache_items, 3600*24*7, 10000),
} # type: Dict[str, Tuple[Callable[[], List[Any]], Callable[[Dict[str, Any], Any], None], int, int]]
} # type: Dict[str, Tuple[Callable[[], List[Any]], Callable[[Dict[text_type, Any], Any], None], int, int]]
def fill_remote_cache(cache):
# type: (str) -> None
remote_cache_time_start = get_remote_cache_time()
remote_cache_requests_start = get_remote_cache_requests()
items_for_remote_cache = {} # type: Dict[str, Any]
items_for_remote_cache = {} # type: Dict[text_type, Any]
(objects, items_filler, timeout, batch_size) = cache_fillers[cache]
count = 0
for obj in objects():