Commit 6a121a18 by Őry Máté

common: add method_cache decorator

parent 79c4861d
...@@ -22,3 +22,9 @@ SOUTH_TESTS_MIGRATE = False ...@@ -22,3 +22,9 @@ SOUTH_TESTS_MIGRATE = False
INSTALLED_APPS += ( INSTALLED_APPS += (
'acl.tests', 'acl.tests',
) )
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache'
}
}
from hashlib import sha224
from time import time
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.cache import cache
from django.db.models import (CharField, DateTimeField, ForeignKey, TextField) from django.db.models import (CharField, DateTimeField, ForeignKey, TextField)
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
...@@ -46,3 +51,62 @@ class ActivityModel(TimeStampedModel): ...@@ -46,3 +51,62 @@ class ActivityModel(TimeStampedModel):
self.finished = timezone.now() self.finished = timezone.now()
self.result = result self.result = result
self.save() self.save()
def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
"""Cache return value of decorated method to memcached and memory.
:param memcached_seconds: Invalidate memcached results after this time.
:param instance_seconds: Invalidate results cached to static memory after
this time.
If a result is cached on instance, return that first. If that fails, check
memcached. If all else fails, run the method and cache on instance and in
memcached.
Do not use for methods with side effects.
Instances are hashed by their id attribute, args by their unicode
representation.
** NOTE: Methods that return None are always "recached".
Based on https://djangosnippets.org/snippets/2477/
"""
def inner_cache(method):
def get_key(instance, *args, **kwargs):
return sha224(unicode(method.__module__) +
unicode(method.__name__) +
unicode(instance.id) +
unicode(args) +
unicode(kwargs)).hexdigest()
def x(instance, *args, **kwargs):
invalidate = kwargs.pop('invalidate_cache', False)
now = time()
key = get_key(instance, *args, **kwargs)
result = None
try:
vals = getattr(instance, key)
except AttributeError:
pass
else:
if vals['time'] + instance_seconds > now:
# has valid on class cache, return that
result = vals['value']
if result is None:
result = cache.get(key)
if invalidate or (result is None):
# all caches failed, call the actual method
result = method(instance, *args, **kwargs)
# save to memcache and class attr
cache.set(key, result, memcached_seconds)
setattr(instance, key, {'time': now, 'value': result})
return result
return x
return inner_cache
from ..models import method_cache
class TestClass(object):
id = None
called = 0
def __init__(self, id):
self.id = id
@method_cache()
def method(self, s):
# print 'Called TestClass(%d).method(%s)' % (self.id, s)
self.called += 1
return self.id + len(s)
from django.test import TestCase
from .models import TestClass
class MethodCacheTestCase(TestCase):
def test_cache(self):
t1 = TestClass(1)
t2 = TestClass(2)
val1a = t1.method('a')
val1b = t1.method('a')
val2a = t2.method('a')
val2b = t2.method('a')
val2b = t2.method('a')
self.assertEqual(val1a, val1b)
self.assertEqual(val2a, val2b)
self.assertNotEqual(val1a, val2a)
t1.method('b')
self.assertEqual(t1.called, 2)
self.assertEqual(t2.called, 1)
def test_invalidate(self):
t1 = TestClass(1)
val1a = t1.method('a')
val1b = t1.method('a', invalidate_cache=True)
t1.method('a')
self.assertEqual(val1a, val1b)
self.assertEqual(t1.called, 2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment