Commits

Lx Yu committed b5fa5b0

directly use mset/mget/delete method in redis client for multi operation

Comments (0)

Files changed (1)

dogpile/cache/backends/redis.py

 
 __all__ = 'RedisBackend', 'RedisLock'
 
+
 class RedisBackend(CacheBackend):
     """A `Redis <http://redis.io/>`_ backend, using the
     `redis-py <http://pypi.python.org/pypi/redis/>`_ backend.
             return redis.StrictRedis.from_url(url=self.url)
         else:
             return redis.StrictRedis(host=self.host, password=self.password,
-                                        port=self.port, db=self.db)
+                                     port=self.port, db=self.db)
 
     def get_mutex(self, key):
         if self.distributed_lock:
         return pickle.loads(value)
 
     def get_multi(self, keys):
-        pipe = self.client.pipeline()
-        for key in keys:
-            pipe.get(key)
-        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
+        values = self.client.mget(keys)
+        values = [pickle.loads(v) if v is not None else NO_VALUE
+                  for v in values]
+        return dict(zip(keys, values))
 
     def set(self, key, value):
         if self.redis_expiration_time:
             self.client.setex(key, self.redis_expiration_time,
-                    pickle.dumps(value))
+                              pickle.dumps(value))
         else:
             self.client.set(key, pickle.dumps(value))
 
     def set_multi(self, mapping):
-        pipe = self.client.pipeline()
-        for key,value in mapping.items():
-            if self.redis_expiration_time:
-                pipe.setex(key, self.redis_expiration_time,
-                        pickle.dumps(value))
-            else:
-                pipe.set(key, pickle.dumps(value))
-        pipe.execute()
+        mapping = dict((k, pickle.dumps(v)) for k, v in mapping.items())
+
+        if not self.redis_expiration_time:
+            self.client.mset(mapping)
+        else:
+            pipe = self.client.pipeline()
+            for key, value in mapping.items():
+                pipe.setex(key, self.redis_expiration_time, value)
+            pipe.execute()
 
     def delete(self, key):
         self.client.delete(key)
 
     def delete_multi(self, keys):
-        pipe = self.client.pipeline()
-        for key in keys:
-            pipe.delete(key)
-        pipe.execute()
+        self.client.delete(*keys)