1. Tim Babych
  2. django-cache-utils


Tim Babych  committed cdd2fa5

added omit parameter

  • Participants
  • Parent commits a7dd4ac
  • Branches default

Comments (0)

Files changed (1)

File cache_utils/decorators.py

View file
 from django.utils.functional import wraps
 from cache_utils.utils import _cache_key, _func_info, _func_type, sanitize_memcached_key
-def cached(timeout, group=None):
+def cached(timeout, group=None, omit=0):
     """ 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
                 name, _args = _func_info(func, args)
                 wrapper._full_name = name
+            # sometimes we do not want to include first N args into cache key
+            # for example, request or context
+            n_args, n_kwargs = args, kwargs
+            print args, kwargs
+            if omit:
+                if type(omit) == int:
+                    n_args = n_args[omit:]
+                if type(omit) == str:
+                    del n_kwargs[omit]
             # try to get the value from cache
-            key = get_key(wrapper._full_name, func_type, args, kwargs)
+            key = get_key(wrapper._full_name, func_type, n_args, n_kwargs)
             value = cache.get(key, **backend_kwargs)
             # in case of cache miss recalculate the value and put it to the cache