Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
alert_words: Refactor the code to flush alert_words cache.
Ujjawal Modia361c23aac7d
diff --git a/zerver/lib/alert_words.py b/zerver/lib/alert_words.pyindex f9561e3d233..3466789681d 100644--- a/zerver/lib/alert_words.py+++ b/zerver/lib/alert_words.py@@ -11,7 +11,7 @@ from zerver.lib.cache import (from zerver.models import AlertWord, Realm, UserProfile, flush_realm_alert_words-@cache_with_key(realm_alert_words_cache_key, timeout=3600 * 24)+@cache_with_key(lambda realm: realm_alert_words_cache_key(realm.id), timeout=3600 * 24)def alert_words_in_realm(realm: Realm) -> Dict[int, List[str]]:user_ids_and_words = AlertWord.objects.filter(realm=realm, user_profile__is_active=True).values("user_profile_id", "word"@@ -23,7 +23,7 @@ def alert_words_in_realm(realm: Realm) -> Dict[int, List[str]]:return user_ids_with_words-@cache_with_key(realm_alert_words_automaton_cache_key, timeout=3600 * 24)+@cache_with_key(lambda realm: realm_alert_words_automaton_cache_key(realm.id), timeout=3600 * 24)def get_alert_word_automaton(realm: Realm) -> ahocorasick.Automaton:user_id_with_words = alert_words_in_realm(realm)alert_word_automaton = ahocorasick.Automaton()@@ -66,7 +66,7 @@ def add_user_alert_words(user_profile: UserProfile, new_words: Iterable[str]) ->for word in word_dict.values())# Django bulk_create operations don't flush caches, so we need to do this ourselves.- flush_realm_alert_words(user_profile.realm)+ flush_realm_alert_words(user_profile.realm_id)return user_alert_words(user_profile)diff --git a/zerver/lib/cache.py b/zerver/lib/cache.pyindex d2916ad02d4..1b44291706a 100644--- a/zerver/lib/cache.py+++ b/zerver/lib/cache.py@@ -635,8 +635,8 @@ def flush_realm(cache_delete(realm_user_dicts_cache_key(realm.id))cache_delete(active_user_ids_cache_key(realm.id))cache_delete(bot_dicts_in_realm_cache_key(realm.id))- cache_delete(realm_alert_words_cache_key(realm))- cache_delete(realm_alert_words_automaton_cache_key(realm))+ cache_delete(realm_alert_words_cache_key(realm.id))+ cache_delete(realm_alert_words_automaton_cache_key(realm.id))cache_delete(active_non_guest_user_ids_cache_key(realm.id))cache_delete(realm_rendered_description_cache_key(realm))cache_delete(realm_text_description_cache_key(realm))@@ -645,12 +645,12 @@ def flush_realm(cache_delete(realm_text_description_cache_key(realm))-def realm_alert_words_cache_key(realm: "Realm") -> str:- return f"realm_alert_words:{realm.string_id}"+def realm_alert_words_cache_key(realm_id: int) -> str:+ return f"realm_alert_words:{realm_id}"-def realm_alert_words_automaton_cache_key(realm: "Realm") -> str:- return f"realm_alert_words_automaton:{realm.string_id}"+def realm_alert_words_automaton_cache_key(realm_id: int) -> str:+ return f"realm_alert_words_automaton:{realm_id}"def realm_rendered_description_cache_key(realm: "Realm") -> str:diff --git a/zerver/models.py b/zerver/models.pyindex bd37e4967ae..64388397b3f 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -4949,14 +4949,14 @@ class AlertWord(models.Model):unique_together = ("user_profile", "word")-def flush_realm_alert_words(realm: Realm) -> None:- cache_delete(realm_alert_words_cache_key(realm))- cache_delete(realm_alert_words_automaton_cache_key(realm))+def flush_realm_alert_words(realm_id: int) -> None:+ cache_delete(realm_alert_words_cache_key(realm_id))+ cache_delete(realm_alert_words_automaton_cache_key(realm_id))def flush_alert_word(*, instance: AlertWord, **kwargs: object) -> None:- realm = instance.realm- flush_realm_alert_words(realm)+ realm_id = instance.realm_id+ flush_realm_alert_words(realm_id)post_save.connect(flush_alert_word, sender=AlertWord)