Skip to content

Commit

Permalink
Connection factory goes to cache options
Browse files Browse the repository at this point in the history
  • Loading branch information
RealFatCat committed Sep 11, 2023
1 parent 3619218 commit 77c2d92
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
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
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
21 changes: 21 additions & 0 deletions tests/test_connection_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ def test_connection_strings(connection_string: str):
)
res = cf.make_connection_params(connection_string)
assert res["url"] == connection_string


@pytest.mark.parametrize(
"connection_string",
[
"unix://tmp/foo.bar?db=1",
"redis://localhost/2",
"rediss://localhost:3333?db=2",
],
)
def test_connection_strings_with_conn_factory_in_opts(connection_string: str):

Check warning on line 30 in tests/test_connection_string.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_string.py#L30

Added line #L30 was not covered by tests
sentinel_conn_factory = "django_redis.pool.SentinelConnectionFactory"
cf = pool.get_connection_factory(

Check warning on line 32 in tests/test_connection_string.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_string.py#L32

Added line #L32 was not covered by tests
path=None,
options={
"CONNECTION_FACTORY": sentinel_conn_factory,
"SENTINELS": [("127.0.0.1", "26739")],
},
)
res = cf.make_connection_params(connection_string)
assert res["url"] == connection_string

Check warning on line 40 in tests/test_connection_string.py

View check run for this annotation

Codecov / codecov/patch

tests/test_connection_string.py#L39-L40

Added lines #L39 - L40 were not covered by tests

0 comments on commit 77c2d92

Please sign in to comment.