diff --git a/zerver/lib/alert_words.py b/zerver/lib/alert_words.py index f9561e3d23..3466789681 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.py index d2916ad02d..1b44291706 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.py index bd37e4967a..64388397b3 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)