Permalink
Browse files

Second go at pooling for redis connections.

  • Loading branch information...
1 parent 367d22a commit da001c1bf1adaaec34992f29fc91390d1fce4505 Svante Paldan committed Feb 12, 2013
Showing with 11 additions and 8 deletions.
  1. +11 −8 beaker_extensions/redis_.py
@@ -6,7 +6,7 @@
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")
@@ -20,6 +20,7 @@ def __init__(self,
lock_dir=None,
**params):
self.db = params.pop('db', None)
+ self.connection_pools = {}
NoSqlManager.__init__(self,
namespace,
url=url,
@@ -28,13 +29,12 @@ def __init__(self,
**params)
def open_connection(self, host, port, **params):
- if (hasattr(self, 'db_conn') and
- self.db_conn.host == host and
- self.db_conn.port == port:
- return
- self.db_conn = StrictRedis(host=host,
- port=int(port),
- db=self.db,
+ if (not self.connection_pools or
+ self._format_pool_key(host, port, self.db) not in self.connection_pools):
+ self.connection_pools[self._format_pool_key(host, port, self.db)] = ConnectionPool(host=host,
+ port=port,
+ db=self.db)
+ self.db_conn = StrictRedis(connection_pool=self.connection_pools[self._format_pool_key(host, port, self.db)],
**params)
def __contains__(self, key):
@@ -64,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 da001c1

Please sign in to comment.