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.
This commit is contained in:
Steve Howell 2017-09-17 13:26:43 -07:00 committed by Tim Abbott
parent 0e24e6bdfa
commit 0966bf1a48
4 changed files with 12 additions and 17 deletions

View File

@ -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)

View File

@ -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 \

View File

@ -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())

View File

@ -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()