Commits

David Novakovic committed 1ba0ed5

Lets try and shorten cache keys.

  • Participants
  • Parent commits de8222d

Comments (0)

Files changed (1)

cache_utils/decorators.py

 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
+import hashlib
 
 try:
     timeout = settings.CACHES['default']['TIMEOUT']
 except:
     timeout = 300
+    
+    
+def md5_string(string):
+    m = hashlib.md5()
+    m.update(string)
+    return m.digest()
 
 def cached(timeout=timeout, group=None):
     """ Caching decorator. Can be applied to function, method or classmethod.
             if not hasattr(wrapper, '_full_name'):
                 name, _args = _func_info(func, args)
                 wrapper._full_name = name
+                
+            cache_key_kwargs = md5_string(repr(kwargs)).encode('base64').strip()
+            cache_key_args = md5_string(repr(args)).encode('base64').strip()
 
             # 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, cache_key_args, cache_key_kwargs)
             value = cache.get(key, **backend_kwargs)
             
             # in case of cache miss recalculate the value and put it to the cache