Commits

Mike Bayer committed fbdc14d

- still 0.4.4
- changelog
- change the approach for checking a value against expire time
so that the logic isn't duplicated
- add version tags

Comments (0)

Files changed (4)

docs/build/changelog.rst

     :version: 0.4.4
 
     .. change::
+        :tags: feature
+        :pullreq: 14
+
+      Full support for multivalue get/set/delete
+      added, using :meth:`.Region.get_multi`,
+      :meth:`.Region.set_multi`, :meth:`.Region.del_multi`,
+      courtesy Marcos Araujo Sobrinho.
+
+    .. change::
         :tags: bug
         :tickets: 27
 

dogpile/cache/__init__.py

-__version__ = '0.4.5'
+__version__ = '0.4.4'
 
 from .region import CacheRegion, register_backend, make_region

dogpile/cache/api.py

 
         The returned value should be a map.
 
+        .. versionadded:: 0.4.4
+
         """
-        raise NotImplementedError()        
+        raise NotImplementedError()
 
     def set(self, key, value): #pragma NO COVERAGE
         """Set a value in the cache.
         The value will always be an instance
         of :class:`.CachedValue`.
 
+        .. versionadded:: 0.4.4
+
         """
         raise NotImplementedError()
 
         that is, can be called any number of times
         regardless of whether or not the
         key exists.
+
+        .. versionadded:: 0.4.4
+
         """
         raise NotImplementedError()

dogpile/cache/region.py

         if self.key_mangler:
             key = self.key_mangler(key)
         value = self.backend.get(key)
-        if value is NO_VALUE:
-            return value
-        elif not ignore_expiration:
+        value = self._unexpired_value_fn(
+                        expiration_time, ignore_expiration)(value)
+
+        return value.payload
+
+    def _unexpired_value_fn(self, expiration_time, ignore_expiration):
+        if ignore_expiration:
+            return lambda value: value
+        else:
             if expiration_time is None:
                 expiration_time = self.expiration_time
-            if expiration_time is not None and \
-                  time.time() - value.metadata["ct"] > expiration_time:
-                return NO_VALUE
-            elif self._invalidated and value.metadata["ct"] < self._invalidated:
-                return NO_VALUE
 
-        return value.payload
+            current_time = time.time()
 
+            def value_fn(value):
+                if value is NO_VALUE:
+                    return value
+                elif expiration_time is not None and \
+                      current_time - value.metadata["ct"] > expiration_time:
+                    return NO_VALUE
+                elif self._invalidated and \
+                        value.metadata["ct"] < self._invalidated:
+                    return NO_VALUE
+                else:
+                    return value
+
+            return value_fn
 
     def get_multi(self, keys, expiration_time=None, ignore_expiration=False):
         """Return multiple values from the cache, based on the given keys.
         value versus the current time (as reported by ``time.time()``).
         If stale, the cached value is ignored and the ``NO_VALUE``
         token is returned.  Passing the flag ``ignore_expiration=True``
-        bypasses the expiration time check.    
+        bypasses the expiration time check.
+
+        .. versionadded:: 0.4.4
 
         """
         if self.key_mangler:
-            keys = map(lambda key:self.key_mangler(key), keys)
+            keys = map(lambda key: self.key_mangler(key), keys)
         values = {}
         backend_values = self.backend.get_multi(keys)
-        for key,value in backend_values.items():
-            if value is NO_VALUE:
-                values[key] = value
-            elif not ignore_expiration:
-                if expiration_time is None:
-                    expiration_time = self.expiration_time
-                if expiration_time is not None and \
-                      time.time() - value.metadata["ct"] > expiration_time:
-                    values[key] = NO_VALUE
-                elif self._invalidated and value.metadata["ct"] < self._invalidated:
-                    values[key] = NO_VALUE
-            values[key] = value.payload
 
+        _unexpired_value_fn = self._unexpired_value_fn(
+                            expiration_time, ignore_expiration)
+        values = dict(
+                    (key, value.payload if value is not NO_VALUE else value)
+                    for key, value in
+                    (
+                        (key, _unexpired_value_fn(value))
+                        for key, value in backend_values.items()
+                    )
+                )
         return values
 
-
     def get_or_create(self, key, creator, expiration_time=None,
                                 should_cache_fn=None):
         """Return a cached value based on the given key.
 
 
     def set_multi(self, mapping):
-        """Place new values in the cache under the given keys."""
+        """Place new values in the cache under the given keys.
+
+        .. versionadded:: 0.4.4
+
+        """
 
         if self.key_mangler:
-            mapping = dict((self.key_mangler(k), self._value(v)) for k,v in mapping.items())
+            mapping = dict((self.key_mangler(k), self._value(v))
+                                for k, v in mapping.items())
         else:
-            mapping = dict((k, self._value(v)) for k,v in mapping.items())
+            mapping = dict((k, self._value(v)) for k, v in mapping.items())
         self.backend.set_multi(mapping)
 
 
 
         This operation is idempotent (can be called multiple times, or on a
         non-existent key, safely)
+
+        .. versionadded:: 0.4.4
+
         """
 
         if self.key_mangler:
-            keys = map(lambda key:self.key_mangler(key), keys)
+            keys = map(lambda key: self.key_mangler(key), keys)
 
         self.backend.delete_multi(keys)