fill_memcached_caches: Also fill the caches of User/UserProfile objects.

Since we flush memcached when we do a server restart, the flurry of
get_updates requests that fly in afterwards are all cache misses for
getting the User/UserProfile objects, so Tornado ends up spending
around 70ms per get_updates request rather than the usual 1-2ms.

So this should substantially improve our Tornado performance around
server restarts.

(imported from commit 07b8126bdfd4ff14e4c3362f9eda1fe5fd571c5b)
This commit is contained in:
Tim Abbott 2013-03-13 13:52:54 -04:00
parent cf58ee833a
commit 76837ebe2c
2 changed files with 20 additions and 4 deletions

View File

@ -1,8 +1,10 @@
# This file needs to be different from cache.py because cache.py
# cannot import anything from zephyr.models or we'd have an import
# loop
from zephyr.models import Message
from zephyr.lib.cache import cache_with_key, djcache, message_cache_key
from zephyr.models import Message, UserProfile
from zephyr.lib.cache import cache_with_key, djcache, message_cache_key, \
userprofile_by_email_cache_key, userprofile_by_user_cache_key, \
user_by_id_cache_key
MESSAGE_CACHE_SIZE = 25000
@ -21,3 +23,17 @@ def populate_message_cache():
items_for_memcached[message_cache_key(m.id)] = (m,)
djcache.set_many(items_for_memcached, timeout=3600*24)
# Fill our various caches of User/UserProfile objects used by Tornado
def populate_user_cache():
items_for_memcached = {}
for user_profile in UserProfile.objects.select_related().all():
items_for_memcached[userprofile_by_email_cache_key(user_profile.user.email)] = (user_profile,)
items_for_memcached[userprofile_by_user_cache_key(user_profile.user.id)] = (user_profile,)
items_for_memcached[user_by_id_cache_key(user_profile.user.id)] = (user_profile.user,)
djcache.set_many(items_for_memcached, timeout=3600*24*7)
def fill_memcached_caches():
populate_user_cache()
populate_message_cache()

View File

@ -1,10 +1,10 @@
from optparse import make_option
from django.core.management.base import BaseCommand
from zephyr.lib.cache_helpers import populate_message_cache
from zephyr.lib.cache_helpers import fill_memcached_caches
class Command(BaseCommand):
option_list = BaseCommand.option_list
help = "Populate the memcached cache of messages."
def handle(self, *args, **options):
populate_message_cache()
fill_memcached_caches()