Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Cache topic_links() results per-request.
Sairam Bisoyi5c0715fd3074
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 82fafc97d06..0ae1aa89fe2 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -53,6 +53,7 @@ from zerver.lib.mention import ()from zerver.lib.mime_types import AUDIO_INLINE_MIME_TYPES, guess_typefrom zerver.lib.outgoing_http import OutgoingSession+from zerver.lib.per_request_cache import cache_for_current_requestfrom zerver.lib.subdomains import is_static_or_current_realm_urlfrom zerver.lib.tex import render_texfrom zerver.lib.thumbnail import (@@ -2458,6 +2459,7 @@ class TopicLinkMatch:# function on the URLs; they are expected to be HTML-escaped when# rendered by clients (just as links rendered into message bodies# are validated and escaped inside `url_to_a`).+@cache_for_current_requestdef topic_links(linkifiers_key: int, topic_name: str) -> list[dict[str, str]]:matches: list[TopicLinkMatch] = []linkifiers = linkifiers_for_realm(linkifiers_key)diff --git a/zerver/models/linkifiers.py b/zerver/models/linkifiers.pyindex 35efbe2d46c..1e9f2645b1b 100644--- a/zerver/models/linkifiers.py+++ b/zerver/models/linkifiers.py@@ -190,6 +190,7 @@ def flush_linkifiers(*, instance: RealmFilter, **kwargs: object) -> None:realm_id = instance.realm_idcache_delete(get_linkifiers_cache_key(realm_id))flush_per_request_cache("linkifiers_for_realm")+ flush_per_request_cache("topic_links")post_save.connect(flush_linkifiers, sender=RealmFilter)diff --git a/zerver/tests/test_message_move_stream.py b/zerver/tests/test_message_move_stream.pyindex 9e7a0cd4a39..41fe91b6b3c 100644--- a/zerver/tests/test_message_move_stream.py+++ b/zerver/tests/test_message_move_stream.py@@ -1563,7 +1563,7 @@ class MessageMoveStreamTest(ZulipTestCase):"iago", "test move stream", "new stream", "test")- with self.assert_database_query_count(60), self.assert_memcached_count(17):+ with self.assert_database_query_count(56), self.assert_memcached_count(17):result = self.client_patch(f"/json/messages/{msg_id}",{diff --git a/zerver/tests/test_message_move_topic.py b/zerver/tests/test_message_move_topic.pyindex c3f693b4ee2..8a86939fa35 100644--- a/zerver/tests/test_message_move_topic.py+++ b/zerver/tests/test_message_move_topic.py@@ -420,7 +420,7 @@ class MessageMoveTopicTest(ZulipTestCase):# state + 1/user with a UserTopic row for the events data)# beyond what is typical were there not UserTopic records to# update. Ideally, we'd eliminate the per-user component.- with self.assert_database_query_count(28):+ with self.assert_database_query_count(24):check_update_message(user_profile=hamlet,message_id=message_id,@@ -517,7 +517,7 @@ class MessageMoveTopicTest(ZulipTestCase):set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)- with self.assert_database_query_count(30):+ with self.assert_database_query_count(25):check_update_message(user_profile=desdemona,message_id=message_id,@@ -547,7 +547,7 @@ class MessageMoveTopicTest(ZulipTestCase):]set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)- with self.assert_database_query_count(36):+ with self.assert_database_query_count(31):check_update_message(user_profile=desdemona,message_id=message_id,@@ -580,7 +580,7 @@ class MessageMoveTopicTest(ZulipTestCase):set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)- with self.assert_database_query_count(32):+ with self.assert_database_query_count(28):check_update_message(user_profile=desdemona,message_id=message_id,@@ -603,7 +603,7 @@ class MessageMoveTopicTest(ZulipTestCase):second_message_id = self.send_stream_message(hamlet, stream_name, topic_name="changed topic name", content="Second message")- with self.assert_database_query_count(26):+ with self.assert_database_query_count(22):check_update_message(user_profile=desdemona,message_id=second_message_id,@@ -682,7 +682,7 @@ class MessageMoveTopicTest(ZulipTestCase):users_to_be_notified_via_muted_topics_event.append(user_topic.user_profile_id)change_all_topic_name = "Topic 1 edited"- with self.assert_database_query_count(33):+ with self.assert_database_query_count(29):check_update_message(user_profile=hamlet,message_id=message_id,