[4.0.x] Fixed #33681 -- Made Redis client pass CACHES["OPTIONS"] to a connection pool.

Thanks Ben Picolo for the report.
Backport of d27e6b233f from main
This commit is contained in:
Mariusz Felisiak 2022-05-16 06:17:40 +02:00
parent 8b2a93ee5b
commit 5c6ebe19cc
3 changed files with 21 additions and 3 deletions

View File

@ -34,9 +34,9 @@ class RedisCacheClient:
self, self,
servers, servers,
serializer=None, serializer=None,
db=None,
pool_class=None, pool_class=None,
parser_class=None, parser_class=None,
**options,
): ):
import redis import redis
@ -60,7 +60,7 @@ class RedisCacheClient:
parser_class = import_string(parser_class) parser_class = import_string(parser_class)
parser_class = parser_class or self._lib.connection.DefaultParser 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): def _get_connection_pool_index(self, write):
# Write to the first server. Read from other servers if there are more, # Write to the first server. Read from other servers if there are more,

View File

@ -9,4 +9,5 @@ Django 4.0.5 fixes several bugs in 4.0.4.
Bugfixes Bugfixes
======== ========
* ... * Fixed a bug in Django 4.0 where not all :setting:`OPTIONS <CACHES-OPTIONS>`
were passed to a Redis client (:ticket:`33681`).

17
tests/cache/tests.py vendored
View File

@ -1898,6 +1898,23 @@ class RedisCacheTests(BaseCacheTests, TestCase):
self.assertIsInstance(cache._cache._serializer.dumps(True), bytes) self.assertIsInstance(cache._cache._serializer.dumps(True), bytes)
self.assertIsInstance(cache._cache._serializer.dumps("abc"), 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): class FileBasedCachePathLibTests(FileBasedCacheTests):
def mkdtemp(self): def mkdtemp(self):