cache: Fix fragile active_bot_dicts_in_realm caching model.

The issue here is similar to that in the previous commit.
This commit is contained in:
Tim Abbott 2016-04-27 14:57:38 -07:00
parent fbc7e977ac
commit 2a2cbd60c3
2 changed files with 14 additions and 12 deletions

View File

@ -273,6 +273,11 @@ def active_user_dicts_in_realm_cache_key(realm):
# type: (Any) -> str
return "active_user_dicts_in_realm:%s" % (realm.id,)
active_bot_dict_fields = ['id', 'full_name', 'short_name',
'email', 'default_sending_stream__name',
'default_events_register_stream__name',
'default_all_public_streams', 'api_key',
'bot_owner__email', 'avatar_source']
def active_bot_dicts_in_realm_cache_key(realm):
# type: (Any) -> str
return "active_bot_dicts_in_realm:%s" % (realm.id,)
@ -308,11 +313,11 @@ def flush_user_profile(sender, **kwargs):
len(set(active_user_dict_fields + ['is_active']) & set(kwargs['update_fields'])) > 0:
cache_delete(active_user_dicts_in_realm_cache_key(user_profile.realm))
# Invalidate our active_bots_in_realm info dict if any bot has changed
bot_fields = {'full_name', 'api_key', 'avatar_source',
'default_all_public_streams', 'is_active',
'default_sending_stream', 'default_events_register_stream'}
if user_profile.is_bot and (kwargs['update_fields'] is None or bot_fields & set(kwargs['update_fields'])):
# Invalidate our active_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 (kwargs['update_fields'] is None or
(set(active_bot_dict_fields + ['is_active']) &
set(kwargs['update_fields']))):
cache_delete(active_bot_dicts_in_realm_cache_key(user_profile.realm))
# Invalidate realm-wide alert words cache if any user in the realm has changed

View File

@ -11,7 +11,8 @@ from zerver.lib.cache import cache_with_key, flush_user_profile, flush_realm, \
generic_bulk_cached_fetch, cache_set, flush_stream, \
display_recipient_cache_key, cache_delete, \
get_stream_cache_key, active_user_dicts_in_realm_cache_key, \
active_bot_dicts_in_realm_cache_key, active_user_dict_fields
active_bot_dicts_in_realm_cache_key, active_user_dict_fields, \
active_bot_dict_fields
from zerver.lib.utils import make_safe_digest, generate_random_token
from django.db import transaction
from zerver.lib.avatar import gravatar_hash, get_avatar_url
@ -1105,12 +1106,8 @@ def get_active_user_dicts_in_realm(realm):
@cache_with_key(active_bot_dicts_in_realm_cache_key, timeout=3600*24*7)
def get_active_bot_dicts_in_realm(realm):
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=True) \
.values('id', 'full_name', 'short_name',
'email', 'default_sending_stream__name',
'default_events_register_stream__name',
'default_all_public_streams', 'api_key',
'bot_owner__email', 'avatar_source')
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=True) \
.values(*active_bot_dict_fields)
def get_prereg_user_by_email(email):
# A user can be invited many times, so only return the result of the latest