mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 00:46:03 +00:00
tornado: Cache messages internally in tornado process.
This allows us to handle the return_messages_immediately part of get_updates requests without having to talk to the database. (imported from commit ed0b7742d359efb21a0a4960f4fc25f4337e9ad4)
This commit is contained in:
25
zephyr/lib/message_cache.py
Normal file
25
zephyr/lib/message_cache.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from zephyr.models import Message
|
||||
from zephyr.lib.cache import cache_with_key, djcache
|
||||
|
||||
def message_cache_key(message_id):
|
||||
return "message:%d" % (message_id,)
|
||||
|
||||
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):
|
||||
return Message.objects.select_related("client", "sender").get(id=message_id)
|
||||
|
||||
# Called on Tornado startup to ensure our message cache isn't empty
|
||||
def populate_message_cache(count):
|
||||
max_message_id = 0
|
||||
min_message_id = 0
|
||||
messages_for_memcached = {}
|
||||
for m in Message.objects.select_related("sender", "client").all().order_by("-id")[0:count]:
|
||||
max_message_id = max(m.id, max_message_id)
|
||||
min_message_id = min(m.id, min_message_id)
|
||||
messages_for_memcached[message_cache_key(m.id)] = (m,)
|
||||
|
||||
djcache.set_many(messages_for_memcached, timeout=3600*24)
|
||||
return max_message_id, min_message_id
|
||||
Reference in New Issue
Block a user