Commits

Marcos Araujo Sobrinho committed fbffdc1

python 3 compatibility

Comments (0)

Files changed (5)

dogpile/cache/backends/file.py

     def get_multi(self, keys):
         values = {}
         for key in keys:
-            value = self.get(key)
-            if value is not NO_VALUE:
-                values[key] = value
+            values[key] = self.get(key)
         return values
 
     def set(self, key, value):
 
     def set_multi(self, mapping):
         with self._dbm_file(True) as dbm:
-            for key,value in mapping:
+            for key,value in mapping.items():
                 dbm[key] = compat.pickle.dumps(value)
 
     def delete(self, key):

dogpile/cache/backends/memcached.py

 
     def get_multi(self, keys):
         values = self.client.get_multi(keys)
-        if values is None:
-            return NO_VALUE
-        else:
-            return values
+        for key in keys:
+            if not key in values or values[key] is None:
+                values[key] = NO_VALUE
+        return values
 
     def set(self, key, value):
         self.client.set(key,

dogpile/cache/backends/memory.py

     def get_multi(self, keys):
         values = {}
         for key in keys:
-            value = self._cache.get(key, NO_VALUE)
-            if value is not NO_VALUE:
-                values[key] = value
+            values[key] = self._cache.get(key, NO_VALUE)
         return values
 
     def set(self, key, value):
         self._cache[key] = value
 
     def set_multi(self, mapping):
-        for key,value in mapping:
+        for key,value in mapping.items():
             self._cache[key] = value
 
     def delete(self, key):

dogpile/cache/backends/redis.py

         pipe = self.client.pipeline()
         for key in keys:
             pipe.get(key)
-        return dict(zip(keys, pipe.execute()))
+        values = dict(zip(keys, pipe.execute()))
+        for key in keys:
+            if key in values and values[key] is not None:
+                values[key] = pickle.loads(values[key])
+            else:
+                values[key] = NO_VALUE
+        return values        
 
     def set(self, key, value):
         if self.redis_expiration_time:
 
     def set_multi(self, mapping):
         pipe = self.client.pipeline()
-        for key,value in mapping:
+        for key,value in mapping.items():
             if self.redis_expiration_time:
                 pipe.setex(key, self.redis_expiration_time,
                         pickle.dumps(value))

dogpile/cache/region.py

 
         return value.payload
 
+
+    def get_multi(self, keys, expiration_time=None, ignore_expiration=False):
+        """Return multiple values from the cache, based on the given keys.
+
+        If the value is not present, the method returns the token
+        ``NO_VALUE``. ``NO_VALUE`` evaluates to False, but is separate from
+        ``None`` to distinguish between a cached value of ``None``.
+
+        By default, the configured expiration time of the
+        :class:`.CacheRegion`, or alternatively the expiration
+        time supplied by the ``expiration_time`` argument,
+        is tested against the creation time of the retrieved
+        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.    
+
+        """
+        if self.key_mangler:
+            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
+
+        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.
         self.backend.set(key, self._value(value))
 
 
+    def set_multi(self, mapping):
+        """Place new values in the cache under the given keys."""
+
+        if self.key_mangler:
+            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())
+        self.backend.set_multi(mapping)
+
+
     def delete(self, key):
         """Remove a value from the cache.
 
 
         self.backend.delete(key)
 
+
+    def delete_multi(self, keys):
+        """Remove multiple values from the cache.
+
+        This operation is idempotent (can be called multiple times, or on a
+        non-existent key, safely)
+        """
+
+        if self.key_mangler:
+            keys = map(lambda key:self.key_mangler(key), keys)
+
+        self.backend.delete_multi(keys)
+
+
     def cache_on_arguments(self, namespace=None, expiration_time=None,
                                         should_cache_fn=None):
         """A function decorator that will cache the return