From d27e6b233f83c3429f21ff3c250a28ff302637ef Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 16 May 2022 06:17:40 +0200 Subject: [PATCH] Fixed #33681 -- Made Redis client pass CACHES["OPTIONS"] to a connection pool. Thanks Ben Picolo for the report. --- django/core/cache/backends/redis.py | 4 ++-- docs/releases/4.0.5.txt | 3 ++- tests/cache/tests.py | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/django/core/cache/backends/redis.py b/django/core/cache/backends/redis.py index e0d30784ff..99cdd594ca 100644 --- a/django/core/cache/backends/redis.py +++ b/django/core/cache/backends/redis.py @@ -32,9 +32,9 @@ class RedisCacheClient: self, servers, serializer=None, - db=None, pool_class=None, parser_class=None, + **options, ): import redis @@ -58,7 +58,7 @@ class RedisCacheClient: parser_class = import_string(parser_class) parser_class = parser_class or self._lib.connection.DefaultParser - self._pool_options = {"parser_class": parser_class, "db": db} + self._pool_options = {"parser_class": parser_class, **options} def _get_connection_pool_index(self, write): # Write to the first server. Read from other servers if there are more, diff --git a/docs/releases/4.0.5.txt b/docs/releases/4.0.5.txt index 219614184c..8caf03bc5d 100644 --- a/docs/releases/4.0.5.txt +++ b/docs/releases/4.0.5.txt @@ -9,4 +9,5 @@ Django 4.0.5 fixes several bugs in 4.0.4. Bugfixes ======== -* ... +* Fixed a bug in Django 4.0 where not all :setting:`OPTIONS ` + were passed to a Redis client (:ticket:`33681`). diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 460e8141d2..4a102d0cc2 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -1817,6 +1817,23 @@ class RedisCacheTests(BaseCacheTests, TestCase): self.assertIsInstance(cache._cache._serializer.dumps(True), bytes) self.assertIsInstance(cache._cache._serializer.dumps("abc"), bytes) + @override_settings( + CACHES=caches_setting_for_tests( + base=RedisCache_params, + exclude=redis_excluded_caches, + OPTIONS={ + "db": 5, + "socket_timeout": 0.1, + "retry_on_timeout": True, + }, + ) + ) + def test_redis_pool_options(self): + pool = cache._cache._get_connection_pool(write=False) + self.assertEqual(pool.connection_kwargs["db"], 5) + self.assertEqual(pool.connection_kwargs["socket_timeout"], 0.1) + self.assertIs(pool.connection_kwargs["retry_on_timeout"], True) + class FileBasedCachePathLibTests(FileBasedCacheTests): def mkdtemp(self):