Commits

David Novakovic committed de8222d

Repopulate function for cached functions.

Comments (0)

Files changed (1)

cache_utils/decorators.py

 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):
+try:
+    timeout = settings.CACHES['default']['TIMEOUT']
+except:
+    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
 
         @wraps(func)
         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)
                 return
             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