Anonymous avatar Anonymous committed 9e9bf07

Updated cache code

Comments (0)

Files changed (1)


         self.propname = '__cache__%s' % getter.__name__
     def __get__(self, instance, owner):
+        if instance is None:
+            return None
         if not hasattr(instance, self.propname):
             setattr(instance, self.propname, self.getter(instance))
         return getattr(instance, self.propname)
 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):
+        use_pickle=False, buffer=True):
         '''Get values from cache'''
         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__
     def _make_key(self, instance):
         '''Build the cache key to use'''
         return ':'.join(parts)
     def __get__(self, instance, owner):
+        if instance is None:
+            return None
+        if self.buffer:
+            try:
+                return getattr(instance, self.propname)
+            except AttributeError:
+                pass
         key = self._make_key(instance)
         value = self.cache.get(key)
         if value is None:
             self.cache.set(key, value, self.timeout)
         if self.pickle:
             value = pickle.loads(value)
+        if self.buffer:
+            setattr(instance, self.propname, value)
         return value
     def __delete__(self, instance):
         key = self._make_key(instance)
+        if self.buffer:
+            try:
+                delattr(instance, self.propname)
+            except AttributeError:
+                pass
-def cached_property(func=None, backend='default', timeout=None, use_pickle=False):
+def cached_property(func=None, backend='default', timeout=None, use_pickle=False, buffer=True):
     if func is None:
         return functools.partial(cached_property,
     return _cached_property(func, backend=backend, timeout=timeout,
-        use_pickle=use_pickle)
+        use_pickle=use_pickle, buffer=buffer)
 class CacheDict(object):
     '''A wrapper around a Cache to give it a dict feel'''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.