Commits

Marcos Araujo Sobrinho committed bc499f0

_multi methods for file, memcache and memory

  • Participants
  • Parent commits b63c2c6

Comments (0)

Files changed (4)

File dogpile/cache/api.py

         """
         raise NotImplementedError()
 
+    def get_multi(self, keys): #pragma NO COVERAGE
+        """Retrieve multiple values from the cache.
+
+        The returned value should be a map.
+
+        """
+        raise NotImplementedError()        
+
     def set(self, key, value): #pragma NO COVERAGE
         """Set a value in the cache.
 
         """
         raise NotImplementedError()
 
+    def set_multi(self, mapping): #pragma NO COVERAGE
+        """Set multiple values in the cache.
+
+        The key will be whatever was passed
+        to the registry, processed by the
+        "key mangling" function, if any.
+        The value will always be an instance
+        of :class:`.CachedValue`.
+
+        """
+        raise NotImplementedError()
+
     def delete(self, key): #pragma NO COVERAGE
         """Delete a value from the cache.
 
         key exists.
         """
         raise NotImplementedError()
+
+    def delete_multi(self, keys): #pragma NO COVERAGE
+        """Delete multiple values from the cache.
+
+        The key will be whatever was passed
+        to the registry, processed by the
+        "key mangling" function, if any.
+
+        The behavior here should be idempotent,
+        that is, can be called any number of times
+        regardless of whether or not the
+        key exists.
+        """
+        raise NotImplementedError()

File dogpile/cache/backends/file.py

                 value = compat.pickle.loads(value)
             return value
 
+    def get_multi(self, keys):
+        values = {}
+        for key in keys:
+            value = self.get(key)
+            if value is not NO_VALUE:
+                values[key] = value
+        return values
+
     def set(self, key, value):
         with self._dbm_file(True) as dbm:
             dbm[key] = compat.pickle.dumps(value)
 
+    def set_multi(self, mapping):
+        with self._dbm_file(True) as dbm:
+            for key,value in mapping:
+                dbm[key] = compat.pickle.dumps(value)
+
     def delete(self, key):
         with self._dbm_file(True) as dbm:
             try:
             except KeyError:
                 pass
 
+    def delete_multi(self, keys):
+        with self._dbm_file(True) as dbm:
+            for key in keys:
+                try:
+                    del dbm[key]
+                except KeyError:
+                    pass
+
 class FileLock(object):
     """Use lockfiles to coordinate read/write access to a file.
 

File dogpile/cache/backends/memcached.py

         else:
             return value
 
+    def get_multi(self, keys):
+        values = self.client.get_multi(keys)
+        if values is None:
+            return NO_VALUE
+        else:
+            return values
+
     def set(self, key, value):
         self.client.set(key,
             value,
             **self.set_arguments
         )
 
+    def set_multi(self, mapping):
+        self.client.set(mapping,
+            **self.set_arguments
+        )
+
     def delete(self, key):
         self.client.delete(key)
 
+    def delete_multi(self, keys):
+        self.client.delete_multi(keys)
+
 class MemcacheArgs(object):
     """Mixin which provides support for the 'time' argument to set(),
     'min_compress_len' to other methods.

File dogpile/cache/backends/memory.py

     def get(self, key):
         return self._cache.get(key, NO_VALUE)
 
+    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
+        return values
+
     def set(self, key, value):
         self._cache[key] = value
 
+    def set_multi(self, mapping):
+        for key,value in mapping:
+            self._cache[key] = value
+
     def delete(self, key):
         self._cache.pop(key, None)
 
+    def delete_multi(self, keys):
+        for key in keys:
+            self._cache.pop(key, None)