1. quadracik
  2. django-cache-utils


David Novakovic  committed de8222d

Repopulate function for cached functions.

  • Participants
  • Parent commits a7dd4ac
  • Branches default

Comments (0)

Files changed (1)

File cache_utils/decorators.py

View file
 from django.core.cache import cache
 from django.utils.functional import wraps
 from cache_utils.utils import _cache_key, _func_info, _func_type, sanitize_memcached_key
+from django.conf import settings
-def cached(timeout, group=None):
+    timeout = settings.CACHES['default']['TIMEOUT']
+    timeout = 300
+def cached(timeout=timeout, group=None):
     """ Caching decorator. Can be applied to function, method or classmethod.
     Supports bulk cache invalidation and invalidation for exact parameter
     set. Cache keys are human-readable because they are constructed from
         def wrapper(*args, **kwargs):
             # full name is stored as attribute on first call
             if not hasattr(wrapper, '_full_name'):
                 name, _args = _func_info(func, args)
             # try to get the value from cache
             key = get_key(wrapper._full_name, func_type, args, kwargs)
             value = cache.get(key, **backend_kwargs)
             # in case of cache miss recalculate the value and put it to the cache
             if value is None:
                 value = func(*args, **kwargs)
             key = get_key(wrapper._full_name, 'function', args, kwargs)
             cache.delete(key, **backend_kwargs)
+        def repopulate(*args, **kwargs):
+            ''' repopulates cache result for function called with passed arguments '''
+            if not hasattr(wrapper, '_full_name'):
+                return
+            key = get_key(wrapper._full_name, func_type, args, kwargs)
+            value = func(*args, **kwargs)
+            print value
+            cache.set(key, value, timeout, **backend_kwargs)
+        wrapper.repopulate = repopulate
         wrapper.invalidate = invalidate
         return wrapper
     return _cached