Commit c62494ad authored by Sean Bleier's avatar Sean Bleier

Allows user to define the kwargs to initialize the connection pool class.

parent 490b58bd
...@@ -2,7 +2,6 @@ from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError ...@@ -2,7 +2,6 @@ from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils import importlib from django.utils import importlib
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.importlib import import_module
from .compat import (smart_text, smart_bytes, bytes_type, from .compat import (smart_text, smart_bytes, bytes_type,
python_2_unicode_compatible, DEFAULT_TIMEOUT) python_2_unicode_compatible, DEFAULT_TIMEOUT)
...@@ -48,8 +47,9 @@ class CacheConnectionPool(object): ...@@ -48,8 +47,9 @@ class CacheConnectionPool(object):
def get_connection_pool(self, host='127.0.0.1', port=6379, db=1, def get_connection_pool(self, host='127.0.0.1', port=6379, db=1,
password=None, parser_class=None, password=None, parser_class=None,
unix_socket_path=None, max_connections=None, connection_pool_class=None): unix_socket_path=None, connection_pool_class=None,
connection_identifier = (host, port, db, parser_class, unix_socket_path, max_connections, connection_pool_class) connection_pool_class_kwargs=None):
connection_identifier = (host, port, db, parser_class, unix_socket_path, connection_pool_class)
if not self._connection_pools.get(connection_identifier): if not self._connection_pools.get(connection_identifier):
connection_class = ( connection_class = (
unix_socket_path and UnixDomainSocketConnection or Connection unix_socket_path and UnixDomainSocketConnection or Connection
...@@ -59,8 +59,8 @@ class CacheConnectionPool(object): ...@@ -59,8 +59,8 @@ class CacheConnectionPool(object):
'password': password, 'password': password,
'connection_class': connection_class, 'connection_class': connection_class,
'parser_class': parser_class, 'parser_class': parser_class,
'max_connections': max_connections,
} }
kwargs.update(connection_pool_class_kwargs)
if unix_socket_path is None: if unix_socket_path is None:
kwargs.update({ kwargs.update({
'host': host, 'host': host,
...@@ -106,8 +106,8 @@ class CacheClass(BaseCache): ...@@ -106,8 +106,8 @@ class CacheClass(BaseCache):
connection_pool = pool.get_connection_pool( connection_pool = pool.get_connection_pool(
parser_class=self.parser_class, parser_class=self.parser_class,
max_connections=self.max_connections,
connection_pool_class=self.connection_pool_class, connection_pool_class=self.connection_pool_class,
connection_pool_class_kwargs=self.connection_pool_class_kwargs,
**kwargs **kwargs
) )
self._client = redis.Redis( self._client = redis.Redis(
...@@ -127,20 +127,20 @@ class CacheClass(BaseCache): ...@@ -127,20 +127,20 @@ class CacheClass(BaseCache):
def options(self): def options(self):
return self.params.get('OPTIONS', {}) return self.params.get('OPTIONS', {})
@property
def max_connections(self):
return self.options.get('MAX_CONNECTIONS', None)
@property @property
def connection_pool_class(self): def connection_pool_class(self):
pool_class = self.options.get('CONNECTION_POOL_CLASS', 'redis.ConnectionPool') cls = self.options.get('CONNECTION_POOL_CLASS', 'redis.ConnectionPool')
module_name, class_name = pool_class.rsplit('.', 1) mod_path, cls_name = cls.rsplit('.', 1)
module = import_module(module_name)
try: try:
cls = getattr(module, class_name) mod = importlib.import_module(mod_path)
except AttributeError: pool_class = getattr(mod, cls_name)
raise ImportError('cannot import name %s' % class_name) except (AttributeError, ImportError):
return cls raise ImproperlyConfigured("Could not find connection pool class '%s'" % cls)
return pool_class
@property
def connection_pool_class_kwargs(self):
return self.options.get('CONNECTION_POOL_CLASS_KWARGS', {})
@property @property
def db(self): def db(self):
......
...@@ -20,10 +20,11 @@ cache_settings = { ...@@ -20,10 +20,11 @@ cache_settings = {
'LOCATION': '127.0.0.1:6379', 'LOCATION': '127.0.0.1:6379',
'OPTIONS': { 'OPTIONS': {
'DB': 15, 'DB': 15,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser', 'PARSER_CLASS': 'redis.connection.HiredisParser',
'MAX_CONNECTIONS': 2,
'CONNECTION_POOL_CLASS': 'redis.ConnectionPool', 'CONNECTION_POOL_CLASS': 'redis.ConnectionPool',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 2
}
}, },
}, },
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment