Add an in-memory Client object cache

Client objects are immutable and there are very few of them, so caching them in
memory is cheap and saves a trip to memcached.

(imported from commit 300b9b402f4e509f86a7fd86b5f898dc3f43738f)
This commit is contained in:
Zev Benjamin 2013-11-20 16:16:48 -05:00
parent 792e0c5976
commit d2d695dcb6

View File

@ -389,11 +389,18 @@ class Recipient(models.Model):
class Client(models.Model):
name = models.CharField(max_length=30, db_index=True, unique=True)
get_client_cache = {}
def get_client(name):
if name not in get_client_cache:
result = get_client_memcached(name)
get_client_cache[name] = result
return get_client_cache[name]
def get_client_cache_key(name):
return 'get_client:%s' % (make_safe_digest(name),)
@cache_with_key(get_client_cache_key, timeout=3600*24*7)
def get_client(name):
def get_client_memcached(name):
(client, _) = Client.objects.get_or_create(name=name)
return client