From a01d2f5d3f4fe5ff9f2d904654db2736108ac552 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Fri, 21 Oct 2022 13:47:07 -0400 Subject: [PATCH 1/4] Push Connection Factory setting to options --- README.rst | 17 ++++++++++++----- django_redis/client/default.py | 7 ++++++- django_redis/pool.py | 10 +--------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index ffa3d482..6046a230 100644 --- a/README.rst +++ b/README.rst @@ -667,9 +667,6 @@ In order to enable this functionality you should add the following: .. code-block:: python - # Enable the alternate connection factory. - DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory' - # These sentinels are shared between all the examples, and are passed # directly to redis Sentinel. These can also be defined inline. SENTINELS = [ @@ -693,6 +690,9 @@ In order to enable this functionality you should add the following: # Sentinels which are passed directly to redis Sentinel. "SENTINELS": SENTINELS, + # Enable the alternate connection factory. + "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', + # kwargs for redis Sentinel (optional). "SENTINEL_KWARGS": {}, @@ -711,6 +711,7 @@ In order to enable this functionality you should add the following: "OPTIONS": { "CLIENT_CLASS": "django_redis.client.SentinelClient", + "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', "SENTINELS": SENTINELS, }, }, @@ -724,7 +725,10 @@ In order to enable this functionality you should add the following: "redis://other_service_name/db?is_master=1", "redis://other_service_name/db?is_master=0", ], - "OPTIONS": {"SENTINELS": SENTINELS}, + "OPTIONS": { + "SENTINELS": SENTINELS, + "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory' + }, }, # A minimal example only using only replicas in read only mode (and @@ -732,7 +736,10 @@ In order to enable this functionality you should add the following: "readonly": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://readonly_service_name/db?is_master=0", - "OPTIONS": {"SENTINELS": SENTINELS}, + "OPTIONS": { + "SENTINELS": SENTINELS, + "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', + }, }, } diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 6886b46b..ad4f420a 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -59,7 +59,12 @@ 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) + connection_factory_path = self._options.get( + "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 {}) From 73c43691e8333b370269005c6d92cd2565dfc024 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Fri, 21 Oct 2022 14:00:36 -0400 Subject: [PATCH 2/4] Make backwards compatible --- README.rst | 41 +++++++++++++++++++++++++++++----- django_redis/client/default.py | 6 ++++- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 6046a230..6fb70eb4 100644 --- a/README.rst +++ b/README.rst @@ -667,6 +667,9 @@ In order to enable this functionality you should add the following: .. code-block:: python + # Enable the alternate connection factory. + DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory' + # These sentinels are shared between all the examples, and are passed # directly to redis Sentinel. These can also be defined inline. SENTINELS = [ @@ -690,9 +693,6 @@ In order to enable this functionality you should add the following: # Sentinels which are passed directly to redis Sentinel. "SENTINELS": SENTINELS, - # Enable the alternate connection factory. - "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', - # kwargs for redis Sentinel (optional). "SENTINEL_KWARGS": {}, @@ -711,7 +711,6 @@ In order to enable this functionality you should add the following: "OPTIONS": { "CLIENT_CLASS": "django_redis.client.SentinelClient", - "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory', "SENTINELS": SENTINELS, }, }, @@ -727,7 +726,6 @@ In order to enable this functionality you should add the following: ], "OPTIONS": { "SENTINELS": SENTINELS, - "CONNECTION_FACTORY": 'django_redis.pool.SentinelConnectionFactory' }, }, @@ -738,7 +736,40 @@ In order to enable this functionality you should add the following: "LOCATION": "redis://readonly_service_name/db?is_master=0", "OPTIONS": { "SENTINELS": SENTINELS, + }, + }, + } + +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", }, }, } diff --git a/django_redis/client/default.py b/django_redis/client/default.py index ad4f420a..0045f4f3 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -59,8 +59,12 @@ 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) + # First check if local override provided, else fall back to settings connection_factory_path = self._options.get( - "CONNECTION_FACTORY", "django_redis.pool.ConnectionFactory" + "CONNECTION_FACTORY", + settings.get( + "DJANGO_REDIS_CONNECTION_FACTORY", "django_redis.pool.ConnectionFactory" + ), ) self.connection_factory = pool.get_connection_factory( path=connection_factory_path, options=self._options From 2c6845e790b40a6ab90c7c12da9acd830d563fb3 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Fri, 21 Oct 2022 14:01:22 -0400 Subject: [PATCH 3/4] Don't touch these --- README.rst | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 6fb70eb4..d2d149b0 100644 --- a/README.rst +++ b/README.rst @@ -724,9 +724,7 @@ In order to enable this functionality you should add the following: "redis://other_service_name/db?is_master=1", "redis://other_service_name/db?is_master=0", ], - "OPTIONS": { - "SENTINELS": SENTINELS, - }, + "OPTIONS": {"SENTINELS": SENTINELS}, }, # A minimal example only using only replicas in read only mode (and @@ -734,9 +732,7 @@ In order to enable this functionality you should add the following: "readonly": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://readonly_service_name/db?is_master=0", - "OPTIONS": { - "SENTINELS": SENTINELS, - }, + "OPTIONS": {"SENTINELS": SENTINELS}, }, } From 62e22e6bc1e8cf9cebcf50d9548b6399b854d8bb Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Fri, 21 Oct 2022 14:55:16 -0400 Subject: [PATCH 4/4] use getattr --- django_redis/client/default.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 0045f4f3..38c0afd0 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -62,8 +62,10 @@ def __init__(self, server, params: Dict[str, Any], backend: BaseCache) -> None: # First check if local override provided, else fall back to settings connection_factory_path = self._options.get( "CONNECTION_FACTORY", - settings.get( - "DJANGO_REDIS_CONNECTION_FACTORY", "django_redis.pool.ConnectionFactory" + getattr( + settings, + "DJANGO_REDIS_CONNECTION_FACTORY", + "django_redis.pool.ConnectionFactory", ), ) self.connection_factory = pool.get_connection_factory(