2013-03-13 19:15:29 +01:00
|
|
|
# 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
|
2013-03-13 18:52:54 +01:00
|
|
|
from zephyr.models import Message, UserProfile
|
|
|
|
from zephyr.lib.cache import cache_with_key, djcache, message_cache_key, \
|
2013-03-18 20:56:32 +01:00
|
|
|
user_profile_by_email_cache_key, user_profile_by_user_cache_key, \
|
2013-03-18 17:10:45 +01:00
|
|
|
user_by_id_cache_key, user_profile_by_id_cache_key
|
2013-01-09 20:35:19 +01:00
|
|
|
|
2013-01-14 17:51:18 +01:00
|
|
|
MESSAGE_CACHE_SIZE = 25000
|
|
|
|
|
2013-01-09 20:35:19 +01:00
|
|
|
def cache_save_message(message):
|
|
|
|
djcache.set(message_cache_key(message.id), (message,), timeout=3600*24)
|
|
|
|
|
|
|
|
@cache_with_key(message_cache_key)
|
|
|
|
def cache_get_message(message_id):
|
2013-03-15 19:13:18 +01:00
|
|
|
return Message.objects.select_related().get(id=message_id)
|
2013-01-09 20:35:19 +01:00
|
|
|
|
|
|
|
# Called on Tornado startup to ensure our message cache isn't empty
|
2013-01-14 17:51:18 +01:00
|
|
|
def populate_message_cache():
|
2013-03-13 19:07:29 +01:00
|
|
|
items_for_memcached = {}
|
2013-03-15 19:14:34 +01:00
|
|
|
BATCH_SIZE = 1000
|
|
|
|
count = 0
|
2013-03-15 19:13:18 +01:00
|
|
|
for m in Message.objects.select_related().all().order_by("-id")[0:MESSAGE_CACHE_SIZE]:
|
2013-03-13 19:07:29 +01:00
|
|
|
items_for_memcached[message_cache_key(m.id)] = (m,)
|
2013-03-15 19:14:34 +01:00
|
|
|
count += 1
|
|
|
|
if (count % BATCH_SIZE == 0):
|
|
|
|
djcache.set_many(items_for_memcached, timeout=3600*24)
|
|
|
|
items_for_memcached = {}
|
2013-01-09 20:35:19 +01:00
|
|
|
|
2013-03-13 19:07:29 +01:00
|
|
|
djcache.set_many(items_for_memcached, timeout=3600*24)
|
2013-03-13 18:52:54 +01:00
|
|
|
|
|
|
|
# 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():
|
2013-03-18 20:56:32 +01:00
|
|
|
items_for_memcached[user_profile_by_email_cache_key(user_profile.user.email)] = (user_profile,)
|
|
|
|
items_for_memcached[user_profile_by_user_cache_key(user_profile.user.id)] = (user_profile,)
|
2013-03-13 18:52:54 +01:00
|
|
|
items_for_memcached[user_by_id_cache_key(user_profile.user.id)] = (user_profile.user,)
|
2013-03-18 17:10:45 +01:00
|
|
|
items_for_memcached[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
|
2013-03-13 18:52:54 +01:00
|
|
|
|
|
|
|
djcache.set_many(items_for_memcached, timeout=3600*24*7)
|
|
|
|
|
|
|
|
def fill_memcached_caches():
|
|
|
|
populate_user_cache()
|
|
|
|
populate_message_cache()
|