From 0966bf1a489c1ec173dd14d531a73eae4a1560b6 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 17 Sep 2017 13:26:43 -0700 Subject: [PATCH] Simplify get_stream_cache_key(). Before this commit, we could pass in either a Realm object or a realm_id to get_stream_cache_key(). Now we consistently pass it a realm_id. --- zerver/lib/actions.py | 6 +++--- zerver/lib/cache.py | 11 +++-------- zerver/models.py | 10 +++++----- zerver/tests/test_messages.py | 2 +- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 9bd39ca7ab..412115b58e 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -618,7 +618,7 @@ def do_deactivate_stream(stream, log=True): do_remove_default_stream(stream) # Remove the old stream information from remote cache. - old_cache_key = get_stream_cache_key(old_name, stream.realm) + old_cache_key = get_stream_cache_key(old_name, stream.realm_id) cache_delete(old_cache_key) stream_dict = stream.to_dict() @@ -2495,8 +2495,8 @@ def do_rename_stream(stream, new_name, log=True): # Update the display recipient and stream, which are easy single # items to set. - old_cache_key = get_stream_cache_key(old_name, stream.realm) - new_cache_key = get_stream_cache_key(stream.name, stream.realm) + old_cache_key = get_stream_cache_key(old_name, stream.realm_id) + new_cache_key = get_stream_cache_key(stream.name, stream.realm_id) if old_cache_key != new_cache_key: cache_delete(old_cache_key) cache_set(new_cache_key, stream) diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 90ef6ebe84..857af2e5aa 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -349,13 +349,8 @@ def bot_dicts_in_realm_cache_key(realm): # type: (Realm) -> Text return u"bot_dicts_in_realm:%s" % (realm.id,) -def get_stream_cache_key(stream_name, realm): - # type: (Text, Union[Realm, int]) -> Text - from zerver.models import Realm - if isinstance(realm, Realm): - realm_id = realm.id - else: - realm_id = realm +def get_stream_cache_key(stream_name, realm_id): + # type: (Text, int) -> Text return u"stream_by_realm_and_name:%s:%s" % ( realm_id, make_safe_digest(stream_name.strip().lower())) @@ -432,7 +427,7 @@ def flush_stream(sender, **kwargs): from zerver.models import UserProfile stream = kwargs['instance'] items_for_remote_cache = {} - items_for_remote_cache[get_stream_cache_key(stream.name, stream.realm)] = (stream,) + items_for_remote_cache[get_stream_cache_key(stream.name, stream.realm_id)] = (stream,) cache_set_many(items_for_remote_cache) if kwargs.get('update_fields') is None or 'name' in kwargs['update_fields'] and \ diff --git a/zerver/models.py b/zerver/models.py index c98598783f..fbc5b9750b 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1025,10 +1025,10 @@ def get_client_remote_cache(name): # get_stream_backend takes either a realm id or a realm @cache_with_key(get_stream_cache_key, timeout=3600*24*7) -def get_stream_backend(stream_name, realm): - # type: (Text, Realm) -> Stream +def get_stream_backend(stream_name, realm_id): + # type: (Text, int) -> Stream return Stream.objects.select_related("realm").get( - name__iexact=stream_name.strip(), realm_id=realm.id) + name__iexact=stream_name.strip(), realm_id=realm_id) def get_active_streams(realm): # type: (Optional[Realm]) -> QuerySet @@ -1039,7 +1039,7 @@ def get_active_streams(realm): def get_stream(stream_name, realm): # type: (Text, Realm) -> Stream - return get_stream_backend(stream_name, realm) + return get_stream_backend(stream_name, realm.id) def bulk_get_streams(realm, stream_names): # type: (Realm, STREAM_NAMES) -> Dict[Text, Any] @@ -1062,7 +1062,7 @@ def bulk_get_streams(realm, stream_names): where=[where_clause], params=stream_names) - return generic_bulk_cached_fetch(lambda stream_name: get_stream_cache_key(stream_name, realm), + return generic_bulk_cached_fetch(lambda stream_name: get_stream_cache_key(stream_name, realm.id), fetch_streams_by_name, [stream_name.lower() for stream_name in stream_names], id_fetcher=lambda stream: stream.name.lower()) diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 5c13f4d36d..7077622a66 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -573,7 +573,7 @@ class StreamMessagesTest(ZulipTestCase): # persistent, so our test can also fail if cache is invalidated # during the course of the unit test. flush_per_request_caches() - cache_delete(get_stream_cache_key(stream, realm)) + cache_delete(get_stream_cache_key(stream, realm.id)) with queries_captured() as queries: send_message()