Commits

quadracik committed 75d53aa

Allow to invalidate & repopulate function's cache w/out having to call a function first

Comments (0)

Files changed (1)

cache_utils/decorators.py

 
         func_type = _func_type(func)
 
-        @wraps(func)
-        def wrapper(*args, **kwargs):
-            
+        def full_name(args):
             # full name is stored as attribute on first call
             if not hasattr(wrapper, '_full_name'):
                 name, _args = _func_info(func, args)
                 wrapper._full_name = name
+            return wrapper._full_name
 
+        @wraps(func)
+        def wrapper(*args, **kwargs):
             # try to get the value from cache
-            key = get_key(wrapper._full_name, func_type, args, kwargs)
+            key = get_key(full_name(args), 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)
 
         def invalidate(*args, **kwargs):
             ''' invalidates cache result for function called with passed arguments '''
-            if not hasattr(wrapper, '_full_name'):
-                return
-            key = get_key(wrapper._full_name, 'function', args, kwargs)
+            key = get_key(full_name(args), '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)
+            key = get_key(full_name(args), 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