Skip to content

Commit f450a78

Browse files
committed
add connection_kwargs for plain redis
Added :paramref:`.RedisBackend.connection_kwargs` parameter, which is a dictionary of additional keyword arguments that will be passed directly to ``StrictRedis()`` or ``StrictRedis.from_url()``, in the same way that this parameter works with the :class:`.RedisSentinelBackend` already. Thanks to Pim Beenes for assistance. Closes: #221 Pull-request: #221 Pull-request-sha: 26c6cc9 Change-Id: I0991a25ddf31a64d831d3b7e56b3cb9a41685260
1 parent fa31dee commit f450a78

3 files changed

Lines changed: 49 additions & 11 deletions

File tree

docs/build/unreleased/221.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: usecase, redis
3+
:tickets: 221
4+
5+
Added :paramref:`.RedisBackend.connection_kwargs` parameter, which is a
6+
dictionary of additional keyword arguments that will be passed directly to
7+
``StrictRedis()`` or ``StrictRedis.from_url()``, in the same way that this
8+
parameter works with the :class:`.RedisSentinelBackend` already.

dogpile/cache/backends/redis.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
class RedisBackend(BytesBackend):
27-
"""A `Redis <http://redis.io/>`_ backend, using the
27+
r"""A `Redis <http://redis.io/>`_ backend, using the
2828
`redis-py <http://pypi.python.org/pypi/redis/>`_ backend.
2929
3030
Example configuration::
@@ -88,6 +88,14 @@ class RedisBackend(BytesBackend):
8888
asynchronous runners, as they run in a different thread than the one
8989
used to create the lock.
9090
91+
:param connection_kwargs: dict, additional keyword arguments are passed
92+
along to the
93+
``StrictRedis.from_url()`` method or ``StrictRedis()`` constructor
94+
directly, including parameters like ``ssl``, ``ssl_certfile``,
95+
``charset``, etc.
96+
97+
.. versionadded:: 1.1.6 Added ``connection_kwargs`` parameter.
98+
9199
"""
92100

93101
def __init__(self, arguments):
@@ -98,12 +106,12 @@ def __init__(self, arguments):
98106
self.password = arguments.pop("password", None)
99107
self.port = arguments.pop("port", 6379)
100108
self.db = arguments.pop("db", 0)
101-
self.distributed_lock = arguments.get("distributed_lock", False)
109+
self.distributed_lock = arguments.pop("distributed_lock", False)
102110
self.socket_timeout = arguments.pop("socket_timeout", None)
103-
104-
self.lock_timeout = arguments.get("lock_timeout", None)
105-
self.lock_sleep = arguments.get("lock_sleep", 0.1)
106-
self.thread_local_lock = arguments.get("thread_local_lock", True)
111+
self.lock_timeout = arguments.pop("lock_timeout", None)
112+
self.lock_sleep = arguments.pop("lock_sleep", 0.1)
113+
self.thread_local_lock = arguments.pop("thread_local_lock", True)
114+
self.connection_kwargs = arguments.pop("connection_kwargs", {})
107115

108116
if self.distributed_lock and self.thread_local_lock:
109117
warnings.warn(
@@ -112,7 +120,7 @@ def __init__(self, arguments):
112120
)
113121

114122
self.redis_expiration_time = arguments.pop("redis_expiration_time", 0)
115-
self.connection_pool = arguments.get("connection_pool", None)
123+
self.connection_pool = arguments.pop("connection_pool", None)
116124
self._create_client()
117125

118126
def _imports(self):
@@ -131,6 +139,7 @@ def _create_client(self):
131139
self.reader_client = self.writer_client
132140
else:
133141
args = {}
142+
args.update(self.connection_kwargs)
134143
if self.socket_timeout:
135144
args["socket_timeout"] = self.socket_timeout
136145

@@ -269,9 +278,11 @@ class RedisSentinelBackend(RedisBackend):
269278
a normal Redis connection can be specified here.
270279
Default is {}.
271280
272-
:param connection_kwargs: dict, are keyword arguments that will be used
273-
when establishing a connection to a Redis server.
274-
Default is {}.
281+
:param connection_kwargs: dict, additional keyword arguments are passed
282+
along to the
283+
``StrictRedis.from_url()`` method or ``StrictRedis()`` constructor
284+
directly, including parameters like ``ssl``, ``ssl_certfile``,
285+
``charset``, etc.
275286
276287
:param lock_sleep: integer, number of seconds to sleep when failed to
277288
acquire a lock. This argument is only valid when
@@ -290,7 +301,6 @@ def __init__(self, arguments):
290301
self.sentinels = arguments.pop("sentinels", None)
291302
self.service_name = arguments.pop("service_name", "mymaster")
292303
self.sentinel_kwargs = arguments.pop("sentinel_kwargs", {})
293-
self.connection_kwargs = arguments.pop("connection_kwargs", {})
294304

295305
super().__init__(
296306
arguments={

tests/cache/test_redis_backend.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,23 @@ def test_connect_with_connection_pool(self, MockStrictRedis):
195195
def test_connect_with_url(self, MockStrictRedis):
196196
arguments = {"url": "redis://redis:password@127.0.0.1:6379/0"}
197197
self._test_helper(MockStrictRedis.from_url, arguments)
198+
199+
def test_extra_arbitrary_args(self, MockStrictRedis):
200+
arguments = {
201+
"url": "redis://redis:password@127.0.0.1:6379/0",
202+
"connection_kwargs": {
203+
"ssl": True,
204+
"encoding": "utf-8",
205+
"new_redis_arg": 50,
206+
},
207+
}
208+
self._test_helper(
209+
MockStrictRedis.from_url,
210+
{
211+
"url": "redis://redis:password@127.0.0.1:6379/0",
212+
"ssl": True,
213+
"encoding": "utf-8",
214+
"new_redis_arg": 50,
215+
},
216+
arguments,
217+
)

0 commit comments

Comments
 (0)