diff --git a/README.rst b/README.rst index ffa3d482..d2d149b0 100644 --- a/README.rst +++ b/README.rst @@ -736,6 +736,40 @@ In order to enable this functionality you should add the following: }, } +You could also setup some caches as sentinel and some as not. See the following example: + +.. code-block:: python + + SENTINELS = [ + ('sentinel-1', 26379), + ('sentinel-2', 26379), + ('sentinel-3', 26379), + ] + + CACHES = { + "sentinel_cache": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://service_name/db", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.SentinelClient", + "SENTINELS": SENTINELS, + # Enable the alternate connection factory. + "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', + "CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool", + }, + }, + + "non_sentinel_cache": { + "BACKEND": "django_redis.cache.RedisCache", + + "LOCATION": "redis://minimal_service_name/db", + + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + }, + }, + } + .. _Redis Sentinels: https://redis.io/topics/sentinel Pluggable parsers diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 6886b46b..38c0afd0 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -59,7 +59,18 @@ def __init__(self, server, params: Dict[str, Any], backend: BaseCache) -> None: self._serializer = serializer_cls(options=self._options) self._compressor = compressor_cls(options=self._options) - self.connection_factory = pool.get_connection_factory(options=self._options) + # First check if local override provided, else fall back to settings + connection_factory_path = self._options.get( + "CONNECTION_FACTORY", + getattr( + settings, + "DJANGO_REDIS_CONNECTION_FACTORY", + "django_redis.pool.ConnectionFactory", + ), + ) + self.connection_factory = pool.get_connection_factory( + path=connection_factory_path, options=self._options + ) def __contains__(self, key: Any) -> bool: return self.has_key(key) diff --git a/django_redis/pool.py b/django_redis/pool.py index dda3b945..047a5cc1 100644 --- a/django_redis/pool.py +++ b/django_redis/pool.py @@ -1,7 +1,6 @@ from typing import Dict from urllib.parse import parse_qs, urlparse -from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.utils.module_loading import import_string from redis import Redis @@ -178,13 +177,6 @@ def get_connection_pool(self, params): return pool -def get_connection_factory(path=None, options=None): - if path is None: - path = getattr( - settings, - "DJANGO_REDIS_CONNECTION_FACTORY", - "django_redis.pool.ConnectionFactory", - ) - +def get_connection_factory(path, options=None): cls = import_string(path) return cls(options or {})