Don't flush the UserPresence cache for a realm unless status changed.

Previously we had an issue that every other update_active_status
request for a particular realm would result in doing the expensive
query to compute the list of active users in that realm.  It turned
out this was because on every update_active_status request, we'd queue
an event that would have the effect of clearing the cache, even if
nobody's tatus changed.  This fixes that issue, by only clearing the
cache for a realm if someone's status actually changed (or the 60s
timeout expires).

(imported from commit d5b829fe255a31c8cecb58458738f1e72a2cf6de)
This commit is contained in:
Tim Abbott 2013-06-24 13:10:25 -04:00
parent c87d182102
commit 04f1b1da66
2 changed files with 14 additions and 3 deletions

View File

@ -808,9 +808,17 @@ def do_update_user_presence(user_profile, client, log_time, status):
became_online = (status == UserPresence.ACTIVE) and (stale_status or was_idle)
if not created:
# The following block attempts to only update the "status"
# field in the event that it actually changed. This is
# important to avoid flushing the UserPresence cache when the
# data it would return to a client hasn't actually changed
# (see the UserPresence post_save hook for details).
presence.timestamp = log_time
presence.status = status
presence.save(update_fields=["timestamp", "status"])
update_fields = ["timestamp"]
if presence.status != status:
presence.status = status
update_fields.append("status")
presence.save(update_fields=update_fields)
if not user_profile.realm.domain == "mit.edu" and (created or became_online):
# Push event to all users in the realm so they see the new user

View File

@ -183,4 +183,7 @@ def status_dict_cache_key(user_profile):
def update_user_presence_cache(sender, **kwargs):
user_profile = kwargs['instance'].user_profile
djcache.delete(KEY_PREFIX + status_dict_cache_key(user_profile))
if kwargs['update_fields'] is None or "status" in kwargs['update_fields']:
# 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))