Commits

funkybob  committed f37007e

Remove pickling from cache utils

  • Participants
  • Parent commits 9e9bf07

Comments (0)

Files changed (1)

File gnocchi/tools/cache.py

 '''Buffering and Caching Descriptors'''
 import functools
 from django.core.cache import get_cache
-import pickle
 
 class buffered_property(object):
     '''Buffer the result of a method on the class instance'''
 
 class _cached_property(object):
     '''Cache the result of a method in the Django cache'''
-    def __init__(self, getter, backend='default', timeout=None,
-        use_pickle=False, buffer=True):
-        '''Get values from cache'''
+    def __init__(self, getter, backend='default', timeout=None, buffer=True):
+        '''
+        getter(instance) = function to call to generate value
+        backend = Django Cache backend to use
+        timeout = cache timeout [None = Default, False = Never]
+        buffer = Store value on instance to avoid repeated cache hits
+        '''
         functools.update_wrapper(self, getter)
         self.getter = getter
         self.timeout = timeout
-        self.pickle = use_pickle
         self.cache = get_cache(backend)
         self.buffer = buffer
         self.propname = '__cache__%s' % getter.__name__
         return ':'.join(parts)
 
     def __get__(self, instance, owner):
+        '''Get the value, or generate if needed'''
         if instance is None:
             return None
         if self.buffer:
         value = self.cache.get(key)
         if value is None:
             value = self.getter(instance)
-            if self.pickle:
-                value = pickle.dumps(value)
-            self.cache.set(key, value, self.timeout)
-        if self.pickle:
-            value = pickle.loads(value)
+            self.__set__(instance, value)
         if self.buffer:
+            # Save a buffered copy on the instance
             setattr(instance, self.propname, value)
         return value
 
+    def __set__(self, instance, value):
+        if self.buffer:
+            setattr(instance, self.propname, value)
+        key = self._make_key(instance)
+        self.cache.set(key, value, self.timeout)
+
     def __delete__(self, instance):
+        '''Delete this value.
+        Will not raise an exception.
+        '''
         key = self._make_key(instance)
         self.cache.delete(key)
         if self.buffer:
             except AttributeError:
                 pass
 
-def cached_property(func=None, backend='default', timeout=None, use_pickle=False, buffer=True):
+def cached_property(func=None, backend='default', timeout=None, buffer=True):
+    '''A decorator for marking methods as cached properties'''
     if func is None:
         return functools.partial(cached_property,
             backend=backend,
             timeout=timeout,
-            use_pickle=use_pickle
+            buffer=buffer,
         )
-    return _cached_property(func, backend=backend, timeout=timeout,
-        use_pickle=use_pickle, buffer=buffer)
+    return _cached_property(func, backend=backend, timeout=timeout, buffer=buffer)
 
 class CacheDict(object):
     '''A wrapper around a Cache to give it a dict feel'''
-    def __init__(self, backend='default', timeout=None, prefix=None, pickle=False):
-        self.pickle = pickle
+    def __init__(self, backend='default', timeout=None, prefix=None):
         self.timeout = timeout
         self.prefix = prefix
         self.cache = get_cache(backend)
         _key = self._key(key)
         if not self.cache.has_key(_key):
             raise KeyError(key)
-        value = self.cache.get(_key)
-        if self.pickle:
-            value = pickle.dumps(value)
-        return value
+        return self.cache.get(_key)
 
     def __setitem__(self, key, value):
-        if self.pickle:
-            value = pickle.dumps(value)
         self.cache.set(self._key(key), value, timeout=self.timeout)
 
     def __delitem__(self, key):