Permalink
Browse files

Merge pull request #17 from zwant/redis_connection_pool

Redis connection pooling
  • Loading branch information...
2 parents 2b5415e + d1bd5a3 commit 171cea4db5de744e24fcf6513f87b8c20ac2ee95 @didip committed Mar 1, 2013
Showing with 24 additions and 4 deletions.
  1. +24 −4 beaker_extensions/redis_.py
@@ -6,19 +6,36 @@
from beaker_extensions.nosql import pickle
try:
- from redis import StrictRedis
+ from redis import StrictRedis, ConnectionPool
except ImportError:
raise InvalidCacheBackendError("Redis cache backend requires the 'redis' library")
log = logging.getLogger(__name__)
class RedisManager(NoSqlManager):
- def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
+ def __init__(self,
+ namespace,
+ url=None,
+ data_dir=None,
+ lock_dir=None,
+ **params):
self.db = params.pop('db', None)
- NoSqlManager.__init__(self, namespace, url=url, data_dir=data_dir, lock_dir=lock_dir, **params)
+ self.connection_pools = {}
+ NoSqlManager.__init__(self,
+ namespace,
+ url=url,
+ data_dir=data_dir,
+ lock_dir=lock_dir,
+ **params)
def open_connection(self, host, port, **params):
- self.db_conn = StrictRedis(host=host, port=int(port), db=self.db, **params)
+ pool_key = self._format_pool_key(host, port, self.db)
+ if pool_key not in self.connection_pools:
+ self.connection_pools[pool_key] = ConnectionPool(host=host,
+ port=port,
+ db=self.db)
+ self.db_conn = StrictRedis(connection_pool=self.connection_pools[pool_key],
+ **params)
def __contains__(self, key):
return self.db_conn.exists(self._format_key(key))
@@ -47,6 +64,9 @@ def __delitem__(self, key):
def _format_key(self, key):
return 'beaker:%s:%s' % (self.namespace, key.replace(' ', '\302\267'))
+ def _format_pool_key(self, host, port, db):
+ return '{0}:{1}:{2}'.format(host, port, self.db)
+
def do_remove(self):
self.db_conn.flush()

0 comments on commit 171cea4

Please sign in to comment.