cache: Clear realm descriptions when description is changed.

Clear both the rendered and the text descriptions for a realm, when the
description of the realm is changed.
This commit is contained in:
Puneeth Chaganti 2019-05-16 18:18:42 +05:30 committed by Tim Abbott
parent f084075c2a
commit 5f50c4d799
2 changed files with 39 additions and 18 deletions

View File

@ -375,13 +375,7 @@ def delete_display_recipient_cache(user_profile: 'UserProfile') -> None:
keys = [display_recipient_cache_key(rid) for rid in recipient_ids]
cache_delete_many(keys)
# Called by models.py to flush the user_profile cache whenever we save
# a user_profile object
def flush_user_profile(sender: Any, **kwargs: Any) -> None:
user_profile = kwargs['instance']
delete_user_profile_caches([user_profile])
def changed(fields: List[str]) -> bool:
def changed(kwargs: Any, fields: List[str]) -> bool:
if kwargs.get('update_fields') is None:
# adds/deletes should invalidate the cache
return True
@ -393,29 +387,35 @@ def flush_user_profile(sender: Any, **kwargs: Any) -> None:
return False
# Called by models.py to flush the user_profile cache whenever we save
# a user_profile object
def flush_user_profile(sender: Any, **kwargs: Any) -> None:
user_profile = kwargs['instance']
delete_user_profile_caches([user_profile])
# Invalidate our active_users_in_realm info dict if any user has changed
# the fields in the dict or become (in)active
if changed(realm_user_dict_fields):
if changed(kwargs, realm_user_dict_fields):
cache_delete(realm_user_dicts_cache_key(user_profile.realm_id))
if changed(['is_active']):
if changed(kwargs, ['is_active']):
cache_delete(active_user_ids_cache_key(user_profile.realm_id))
cache_delete(active_non_guest_user_ids_cache_key(user_profile.realm_id))
if changed(['is_guest']):
if changed(kwargs, ['is_guest']):
cache_delete(active_non_guest_user_ids_cache_key(user_profile.realm_id))
if changed(['email', 'full_name', 'short_name', 'id', 'is_mirror_dummy']):
if changed(kwargs, ['email', 'full_name', 'short_name', 'id', 'is_mirror_dummy']):
delete_display_recipient_cache(user_profile)
# Invalidate our bots_in_realm info dict if any bot has
# changed the fields in the dict or become (in)active
if user_profile.is_bot and changed(bot_dict_fields):
if user_profile.is_bot and changed(kwargs, bot_dict_fields):
cache_delete(bot_dicts_in_realm_cache_key(user_profile.realm))
# Invalidate realm-wide alert words cache if any user in the realm has changed
# alert words
if changed(['alert_words']):
if changed(kwargs, ['alert_words']):
cache_delete(realm_alert_words_cache_key(user_profile.realm))
cache_delete(realm_alert_words_automaton_cache_key(user_profile.realm))
@ -438,6 +438,10 @@ def flush_realm(sender: Any, **kwargs: Any) -> None:
cache_delete(realm_rendered_description_cache_key(realm))
cache_delete(realm_text_description_cache_key(realm))
if changed(kwargs, ['description']):
cache_delete(realm_rendered_description_cache_key(realm))
cache_delete(realm_text_description_cache_key(realm))
def realm_alert_words_cache_key(realm: 'Realm') -> str:
return "realm_alert_words:%s" % (realm.string_id,)

View File

@ -21,6 +21,7 @@ from zerver.lib.actions import (
)
from confirmation.models import create_confirmation_link, Confirmation
from zerver.lib.realm_description import get_realm_rendered_description, get_realm_text_description
from zerver.lib.send_email import send_future_email
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import tornado_redirected_to_list
@ -185,6 +186,22 @@ class RealmTest(ZulipTestCase):
do_deactivate_realm(user.realm)
self.assertEqual(ScheduledEmail.objects.count(), 0)
def test_do_change_realm_description_clears_cached_descriptions(self) -> None:
realm = get_realm('zulip')
rendered_description = get_realm_rendered_description(realm)
text_description = get_realm_text_description(realm)
realm.description = 'New Description'
realm.save(update_fields=['description'])
new_rendered_description = get_realm_rendered_description(realm)
self.assertNotEqual(rendered_description, new_rendered_description)
self.assertIn(realm.description, new_rendered_description)
new_text_description = get_realm_text_description(realm)
self.assertNotEqual(text_description, new_text_description)
self.assertEqual(realm.description, new_text_description)
def test_do_deactivate_realm_on_deactivated_realm(self) -> None:
"""Ensure early exit is working in realm deactivation"""
realm = get_realm('zulip')