Set timeouts for our memcached caches.

The policy this implements is:
* 1 week for most persistent data (Clients, etc.)
* 1 day for messages

(imported from commit d57bb2c6b9626ffa2155c6d0ef9b60827d1f2381)
This commit is contained in:
Tim Abbott
2013-03-26 14:09:45 -04:00
parent e2d010ef2d
commit e8aa77c9b4
5 changed files with 15 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ from openid.consumer.consumer import SUCCESS
from zephyr.lib.cache import cache_with_key
from zephyr.lib.cache import user_by_id_cache_key
@cache_with_key(user_by_id_cache_key)
@cache_with_key(user_by_id_cache_key, timeout=3600*24*7)
def get_user_by_id(user_id):
try:
return User.objects.select_related().get(id=user_id)

View File

@@ -48,11 +48,11 @@ def update_user_activity(request, user_profile, client):
# I like the all-lowercase name better
require_post = require_POST
@cache_with_key(user_profile_by_user_cache_key)
@cache_with_key(user_profile_by_user_cache_key, timeout=3600*24*7)
def get_user_profile_by_user_id(user_id):
return UserProfile.objects.select_related().get(user_id=user_id)
@cache_with_key(user_profile_by_email_cache_key)
@cache_with_key(user_profile_by_email_cache_key, timeout=3600*24*7)
def get_user_profile_by_email(email):
return UserProfile.objects.select_related().get(user__email__iexact=email)

View File

@@ -110,7 +110,8 @@ def compute_mit_user_fullname(email):
traceback.print_exc()
return email.lower()
@cache_with_key(lambda realm, email: user_profile_by_email_cache_key(email))
@cache_with_key(lambda realm, email: user_profile_by_email_cache_key(email),
timeout=3600*24*7)
@transaction.commit_on_success
def create_mit_user_if_needed(realm, email):
try:

View File

@@ -15,7 +15,7 @@ MESSAGE_CACHE_SIZE = 25000
def cache_save_message(message):
djcache.set(message_cache_key(message.id), (message,), timeout=3600*24)
@cache_with_key(message_cache_key)
@cache_with_key(message_cache_key, timeout=3600*24)
def cache_get_message(message_id):
return Message.objects.select_related().get(id=message_id)

View File

@@ -20,7 +20,8 @@ from bitfield import BitField
MAX_SUBJECT_LENGTH = 60
MAX_MESSAGE_LENGTH = 10000
@cache_with_key(lambda self: 'display_recipient_dict:%d' % (self.id,))
@cache_with_key(lambda self: 'display_recipient_dict:%d' % (self.id,),
timeout=3600*24*7)
def get_display_recipient(recipient):
"""
recipient: an instance of Recipient.
@@ -161,7 +162,7 @@ class Client(models.Model):
def get_client_cache_key(name):
return 'get_client:%s' % (make_safe_digest(name),)
@cache_with_key(get_client_cache_key)
@cache_with_key(get_client_cache_key, timeout=3600*24*7)
@transaction.commit_on_success
def get_client(name):
try:
@@ -188,7 +189,7 @@ def get_stream_cache_key(stream_name, realm):
realm_id, make_safe_digest(stream_name.strip().lower()))
# get_stream_backend takes either a realm id or a realm
@cache_with_key(get_stream_cache_key)
@cache_with_key(get_stream_cache_key, timeout=3600*24*7)
def get_stream_backend(stream_name, realm):
if isinstance(realm, Realm):
realm_id = realm.id
@@ -207,7 +208,7 @@ def get_stream(stream_name, realm):
def get_recipient_cache_key(type, type_id):
return "get_recipient:%s:%s" % (type, type_id,)
@cache_with_key(get_recipient_cache_key)
@cache_with_key(get_recipient_cache_key, timeout=3600*24*7)
def get_recipient(type, type_id):
return Recipient.objects.get(type_id=type_id, type=type)
@@ -231,7 +232,8 @@ class Message(models.Model):
def __str__(self):
return self.__repr__()
@cache_with_key(lambda self, apply_markdown, rendered_content=None: 'message_dict:%d:%d' % (self.id, apply_markdown))
@cache_with_key(lambda self, apply_markdown, rendered_content=None: 'message_dict:%d:%d' % (self.id, apply_markdown),
timeout=3600*24)
def to_dict(self, apply_markdown, rendered_content=None):
display_recipient = get_display_recipient(self.recipient)
if self.recipient.type == Recipient.STREAM:
@@ -332,7 +334,7 @@ class Subscription(models.Model):
def __str__(self):
return self.__repr__()
@cache_with_key(user_profile_by_id_cache_key)
@cache_with_key(user_profile_by_id_cache_key, timeout=3600*24*7)
def get_user_profile_by_id(uid):
return UserProfile.objects.select_related().get(id=uid)
@@ -353,7 +355,7 @@ def get_huddle(id_list):
huddle_hash = get_huddle_hash(id_list)
return get_huddle_backend(huddle_hash, id_list)
@cache_with_key(lambda huddle_hash, id_list: huddle_hash_cache_key(huddle_hash))
@cache_with_key(lambda huddle_hash, id_list: huddle_hash_cache_key(huddle_hash), timeout=3600*24*7)
def get_huddle_backend(huddle_hash, id_list):
(huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
if created: