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

Connection factory goes to cache options #680

Merged
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
29 changes: 29 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,35 @@ In order to enable this functionality you should add the following:
},
}

It is also possible to set some caches as sentinels and some as not:

.. code-block:: python

SENTINELS = [
('sentinel-1', 26379),
('sentinel-2', 26379),
('sentinel-3', 26379),
]
CACHES = {
"sentinel": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://service_name/db",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.SentinelClient",
"SENTINELS": SENTINELS,
"CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool",
"CONNECTION_FACTORY": "django_redis.pool.SentinelConnectionFactory",
},
},
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
},
}

.. _Redis Sentinels: https://redis.io/topics/sentinel

Pluggable parsers
Expand Down
1 change: 1 addition & 0 deletions changelog.d/680.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Connection factory goes to cache options
3 changes: 3 additions & 0 deletions django_redis/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def get_connection_factory(path=None, options=None):
"DJANGO_REDIS_CONNECTION_FACTORY",
"django_redis.pool.ConnectionFactory",
)
opt_conn_factory = options.get("CONNECTION_FACTORY")
if opt_conn_factory:
path = opt_conn_factory

cls = import_string(path)
return cls(options or {})
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ commands =
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_lz4 {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_msgpack {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_sentinel {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_sentinel_opts {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_sharding {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_usock {posargs}
{envpython} -m pytest --cov-append --cov-report= --ds=settings.sqlite_zlib {posargs}
Expand Down
49 changes: 49 additions & 0 deletions tests/settings/sqlite_sentinel_opts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
SECRET_KEY = "django_tests_secret_key"

SENTINELS = [("127.0.0.1", 26379)]

conn_factory = "django_redis.pool.SentinelConnectionFactory"

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": ["redis://default_service?db=5"],
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SENTINELS": SENTINELS,
"CONNECTION_FACTORY": conn_factory,
},
},
"doesnotexist": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://missing_service?db=1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SENTINELS": SENTINELS,
"CONNECTION_FACTORY": conn_factory,
},
},
"sample": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://default_service?db=1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.SentinelClient",
"SENTINELS": SENTINELS,
"CONNECTION_FACTORY": conn_factory,
},
},
"with_prefix": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://default_service?db=1",
"KEY_PREFIX": "test-prefix",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SENTINELS": SENTINELS,
"CONNECTION_FACTORY": conn_factory,
},
},
}

INSTALLED_APPS = ["django.contrib.sessions"]

USE_TZ = False
60 changes: 60 additions & 0 deletions tests/test_connection_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from django.core.exceptions import ImproperlyConfigured

from django_redis import pool


def test_connection_factory_redefine_from_opts():
cf = pool.get_connection_factory(
path="django_redis.pool.ConnectionFactory",

Check warning on line 9 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L7-L9

Added lines #L7 - L9 were not covered by tests
options={
"CONNECTION_FACTORY": "django_redis.pool.SentinelConnectionFactory",
"SENTINELS": [("127.0.0.1", "26739")],

Check warning on line 12 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L11-L12

Added lines #L11 - L12 were not covered by tests
},
)
assert cf.__class__.__name__ == "SentinelConnectionFactory"

Check warning on line 15 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L15

Added line #L15 was not covered by tests


@pytest.mark.parametrize(
"conn_factory,expected",
[
("django_redis.pool.SentinelConnectionFactory", pool.SentinelConnectionFactory),
("django_redis.pool.ConnectionFactory", pool.ConnectionFactory),
],
)
def test_connection_factory_opts(conn_factory: str, expected):
cf = pool.get_connection_factory(

Check warning on line 26 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L25-L26

Added lines #L25 - L26 were not covered by tests
path=None,
options={
"CONNECTION_FACTORY": conn_factory,
"SENTINELS": [("127.0.0.1", "26739")],
},
)
assert isinstance(cf, expected)

Check warning on line 33 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L33

Added line #L33 was not covered by tests


@pytest.mark.parametrize(
"conn_factory,expected",
[
("django_redis.pool.SentinelConnectionFactory", pool.SentinelConnectionFactory),
("django_redis.pool.ConnectionFactory", pool.ConnectionFactory),
],
)
def test_connection_factory_path(conn_factory: str, expected):
cf = pool.get_connection_factory(

Check warning on line 44 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L43-L44

Added lines #L43 - L44 were not covered by tests
path=conn_factory,
options={
"SENTINELS": [("127.0.0.1", "26739")],
},
)
assert isinstance(cf, expected)

Check warning on line 50 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L50

Added line #L50 was not covered by tests


def test_connection_factory_no_sentinels():
with pytest.raises(ImproperlyConfigured):
pool.get_connection_factory(
path=None,

Check warning on line 56 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L53-L56

Added lines #L53 - L56 were not covered by tests
options={
"CONNECTION_FACTORY": "django_redis.pool.SentinelConnectionFactory",

Check warning on line 58 in tests/test_connection_factory.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_factory.py#L58

Added line #L58 was not covered by tests
},
)
Loading