cache_helpers: Fill in memcached message cache in batches of 1000.

This cache filling code takes about 5 seconds to run, which means it
will finish before Tornado starts reading from this cache, but if that
were to change, it would be much better to have made at least some
progress.

(imported from commit 60a3420cdb9ddf331d83573a3fdb6be1a5ee5a4f)
This commit is contained in:
Tim Abbott
2013-03-15 14:14:34 -04:00
parent e78ef47487
commit ec07184721

View File

@@ -18,8 +18,14 @@ def cache_get_message(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 = {}
BATCH_SIZE = 1000
count = 0
for m in Message.objects.select_related().all().order_by("-id")[0:MESSAGE_CACHE_SIZE]: for m in Message.objects.select_related().all().order_by("-id")[0:MESSAGE_CACHE_SIZE]:
items_for_memcached[message_cache_key(m.id)] = (m,) items_for_memcached[message_cache_key(m.id)] = (m,)
count += 1
if (count % BATCH_SIZE == 0):
djcache.set_many(items_for_memcached, timeout=3600*24)
items_for_memcached = {}
djcache.set_many(items_for_memcached, timeout=3600*24) djcache.set_many(items_for_memcached, timeout=3600*24)