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 3 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
11 changes: 10 additions & 1 deletion django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ 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",
settings.get(
"DJANGO_REDIS_CONNECTION_FACTORY", "django_redis.pool.ConnectionFactory"
),
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compatibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is not only 'defaultClient'

Copy link
Author

@vedantpuri vedantpuri Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't follow this comment, could you elaborate ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on this if you could clarify a bit further

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late reply, I mean that there is not only DefaultClient but also others... but now I see that others all use connection factory of the parent class which is DefaultClient

Copy link
Member

@WisdomPill WisdomPill Jan 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also why moving this logic to DefaultClient? it makes testing more difficult... please move it back, add tests and changelog file

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 {})