Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
cache: Use DB for all bulk get-stream-by-name queries.
Steve Howell046e4c715b49
diff --git a/zerver/models.py b/zerver/models.pyindex 2f720e133c0..9543e06f5e9 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -7,7 +7,6 @@ from datetime import timedeltafrom email.headerregistry import Addressfrom typing import (TYPE_CHECKING,- AbstractSet,Any,Callable,Dict,@@ -15,7 +14,6 @@ from typing import (List,Optional,Pattern,- Sequence,Set,Tuple,TypedDict,@@ -63,7 +61,6 @@ from zerver.lib.cache import (bot_dict_fields,bot_dicts_in_realm_cache_key,bot_profile_cache_key,- bulk_cached_fetch,cache_delete,cache_set,cache_with_key,@@ -120,8 +117,6 @@ MAX_LANGUAGE_ID_LENGTH: int = 50SECONDS_PER_DAY = 86400-STREAM_NAMES = TypeVar("STREAM_NAMES", Sequence[str], AbstractSet[str])-if TYPE_CHECKING:# We use ModelBackend only for typing. Importing it otherwise causes circular dependency.from django.contrib.auth.backends import ModelBackend@@ -2884,8 +2879,8 @@ def get_stream_by_id_in_realm(stream_id: int, realm: Realm) -> Stream:return Stream.objects.select_related().get(id=stream_id, realm=realm)-def bulk_get_streams(realm: Realm, stream_names: STREAM_NAMES) -> Dict[str, Any]:- def fetch_streams_by_name(stream_names: List[str]) -> QuerySet[Stream]:+def bulk_get_streams(realm: Realm, stream_names: Set[str]) -> Dict[str, Any]:+ def fetch_streams_by_name(stream_names: Set[str]) -> QuerySet[Stream]:## This should be just#@@ -2897,24 +2892,12 @@ def bulk_get_streams(realm: Realm, stream_names: STREAM_NAMES) -> Dict[str, Any]where_clause = ("upper(zerver_stream.name::text) IN (SELECT upper(name) FROM unnest(%s) AS name)")- return (- get_active_streams(realm)- .select_related()- .extra(where=[where_clause], params=(list(stream_names),))- )-- def stream_name_to_cache_key(stream_name: str) -> str:- return get_stream_cache_key(stream_name, realm.id)+ return get_active_streams(realm).extra(where=[where_clause], params=(list(stream_names),))- def stream_to_lower_name(stream: Stream) -> str:- return stream.name.lower()-- return bulk_cached_fetch(- stream_name_to_cache_key,- fetch_streams_by_name,- [stream_name.lower() for stream_name in stream_names],- id_fetcher=stream_to_lower_name,- )+ if not stream_names:+ return {}+ streams = list(fetch_streams_by_name(stream_names))+ return {stream.name.lower(): stream for stream in streams}def get_huddle_recipient(user_profile_ids: Set[int]) -> Recipient:diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 24ea508168b..8b3b2c2ce2d 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -2418,7 +2418,7 @@ class StreamAdminTest(ZulipTestCase):If you're not an admin, you can't remove other people from streams except your own bots."""result = self.attempt_unsubscribe_of_principal(- query_count=7,+ query_count=8,target_users=[self.example_user("cordelia")],is_realm_admin=False,is_subbed=True,@@ -2433,7 +2433,7 @@ class StreamAdminTest(ZulipTestCase):those you aren't on."""result = self.attempt_unsubscribe_of_principal(- query_count=15,+ query_count=16,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=True,@@ -2460,8 +2460,8 @@ class StreamAdminTest(ZulipTestCase):for name in ["cordelia", "prospero", "iago", "hamlet", "outgoing_webhook_bot"]]result = self.attempt_unsubscribe_of_principal(- query_count=27,- cache_count=9,+ query_count=28,+ cache_count=8,target_users=target_users,is_realm_admin=True,is_subbed=True,@@ -2478,7 +2478,7 @@ class StreamAdminTest(ZulipTestCase):are on."""result = self.attempt_unsubscribe_of_principal(- query_count=16,+ query_count=17,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=True,@@ -2495,7 +2495,7 @@ class StreamAdminTest(ZulipTestCase):streams you aren't on."""result = self.attempt_unsubscribe_of_principal(- query_count=16,+ query_count=17,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=False,@@ -2509,7 +2509,7 @@ class StreamAdminTest(ZulipTestCase):def test_cant_remove_others_from_stream_legacy_emails(self) -> None:result = self.attempt_unsubscribe_of_principal(- query_count=7,+ query_count=8,is_realm_admin=False,is_subbed=True,invite_only=False,@@ -2521,7 +2521,7 @@ class StreamAdminTest(ZulipTestCase):def test_admin_remove_others_from_stream_legacy_emails(self) -> None:result = self.attempt_unsubscribe_of_principal(- query_count=15,+ query_count=16,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=True,@@ -2535,7 +2535,7 @@ class StreamAdminTest(ZulipTestCase):def test_admin_remove_multiple_users_from_stream_legacy_emails(self) -> None:result = self.attempt_unsubscribe_of_principal(- query_count=18,+ query_count=19,target_users=[self.example_user("cordelia"), self.example_user("prospero")],is_realm_admin=True,is_subbed=True,@@ -2553,7 +2553,7 @@ class StreamAdminTest(ZulipTestCase):fails gracefully."""result = self.attempt_unsubscribe_of_principal(- query_count=10,+ query_count=11,target_users=[self.example_user("cordelia")],is_realm_admin=True,is_subbed=False,@@ -2583,7 +2583,7 @@ class StreamAdminTest(ZulipTestCase):webhook_bot = self.example_user("webhook_bot")do_change_bot_owner(webhook_bot, bot_owner=other_user, acting_user=other_user)result = self.attempt_unsubscribe_of_principal(- query_count=8,+ query_count=9,target_users=[webhook_bot],is_realm_admin=False,is_subbed=True,@@ -4873,7 +4873,7 @@ class SubscriptionAPITest(ZulipTestCase):# The only known O(N) behavior here is that we call# principal_to_user_profile for each of our users.- self.assert_length(cache_tries, 4)+ self.assert_length(cache_tries, 3)def test_subscriptions_add_for_principal(self) -> None:"""