message_cache: Query related models to avoid more queries later.

(imported from commit c1442165c68e9eef1cb5d310624c1021c1a57350)
This commit is contained in:
Tim Abbott 2013-03-15 14:13:18 -04:00
parent d88591c19c
commit e78ef47487
1 changed files with 2 additions and 3 deletions

View File

@ -13,13 +13,12 @@ def cache_save_message(message):
@cache_with_key(message_cache_key) @cache_with_key(message_cache_key)
def cache_get_message(message_id): def cache_get_message(message_id):
return Message.objects.select_related("sending_client", "sender").get(id=message_id) return Message.objects.select_related().get(id=message_id)
# Called on Tornado startup to ensure our message cache isn't empty # Called on Tornado startup to ensure our message cache isn't empty
def populate_message_cache(): def populate_message_cache():
items_for_memcached = {} items_for_memcached = {}
for m in Message.objects.select_related("sending_client", "sender").all().order_by( for m in Message.objects.select_related().all().order_by("-id")[0:MESSAGE_CACHE_SIZE]:
"-id")[0:MESSAGE_CACHE_SIZE]:
items_for_memcached[message_cache_key(m.id)] = (m,) items_for_memcached[message_cache_key(m.id)] = (m,)
djcache.set_many(items_for_memcached, timeout=3600*24) djcache.set_many(items_for_memcached, timeout=3600*24)