From 76837ebe2cc3e2c5c8cc7c01e228950ab8ad65db Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 13 Mar 2013 13:52:54 -0400 Subject: [PATCH] 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) --- zephyr/lib/cache_helpers.py | 20 +++++++++++++++++-- .../commands/fill_memcached_caches.py | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/zephyr/lib/cache_helpers.py b/zephyr/lib/cache_helpers.py index cfb2babde1..28e5aac7d0 100644 --- a/zephyr/lib/cache_helpers.py +++ b/zephyr/lib/cache_helpers.py @@ -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() diff --git a/zephyr/management/commands/fill_memcached_caches.py b/zephyr/management/commands/fill_memcached_caches.py index da066f1c19..09f177e456 100644 --- a/zephyr/management/commands/fill_memcached_caches.py +++ b/zephyr/management/commands/fill_memcached_caches.py @@ -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()