Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Connection Factory setting to options #620

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions django_redis/pool.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {})