diff --git a/zerver/lib/alert_words.py b/zerver/lib/alert_words.py index f7a080d052..35a0081cb8 100644 --- a/zerver/lib/alert_words.py +++ b/zerver/lib/alert_words.py @@ -2,10 +2,13 @@ import re import zerver.models +from zerver.lib.cache import cache_with_key, realm_alert_words_cache_key + import itertools import logging import ujson +@cache_with_key(realm_alert_words_cache_key, timeout=3600*24) def alert_words_in_realm(realm): users = zerver.models.UserProfile.objects.filter(realm=realm, is_active=True) all_user_words = dict((user, user_alert_words(user)) for user in users) diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 68376ac5da..77aecfbc67 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -242,6 +242,11 @@ def update_user_profile_cache(sender, **kwargs): items_for_memcached[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,) cache_set_many(items_for_memcached) + # Invalidate realm-wide alert words cache if any user in the realm has changed + # alert words + if kwargs['update_fields'] is None or "alert_words" in kwargs['update_fields']: + djcache.delete(KEY_PREFIX + realm_alert_words_cache_key(user_profile.realm)) + def status_dict_cache_key(user_profile): return "status_dict:%d" % (user_profile.realm_id,) @@ -251,3 +256,6 @@ def update_user_presence_cache(sender, **kwargs): # If the status of the user changed, flush the user's realm's # entry in the UserPresence cache to avoid giving out stale state djcache.delete(KEY_PREFIX + status_dict_cache_key(user_profile)) + +def realm_alert_words_cache_key(realm): + return "realm_alert_words:%s" % (realm.domain,)