summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-06-09 13:52:41 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-06-10 10:18:08 -0400
commitf450a785aa9156d86216b194b9bf64791120abd9 (patch)
tree8b35e64fd30c8501fe98f51ca08940b791b663fc
parentfa31deecd86bc4a4bfe19b294773ea4c0e37c44e (diff)
downloaddogpile-cache-f450a785aa9156d86216b194b9bf64791120abd9.tar.gz
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: https://github.com/sqlalchemy/dogpile.cache/pull/221 Pull-request-sha: 26c6cc97ad677d9df4e7387bbae861c15db395de Change-Id: I0991a25ddf31a64d831d3b7e56b3cb9a41685260
-rw-r--r--docs/build/unreleased/221.rst8
-rw-r--r--dogpile/cache/backends/redis.py32
-rw-r--r--tests/cache/test_redis_backend.py20
3 files changed, 49 insertions, 11 deletions
diff --git a/docs/build/unreleased/221.rst b/docs/build/unreleased/221.rst
new file mode 100644
index 0000000..9211abf
--- /dev/null
+++ b/docs/build/unreleased/221.rst
@@ -0,0 +1,8 @@
+.. change::
+ :tags: usecase, redis
+ :tickets: 221
+
+ 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.
diff --git a/dogpile/cache/backends/redis.py b/dogpile/cache/backends/redis.py
index cf646fe..1a6ba09 100644
--- a/dogpile/cache/backends/redis.py
+++ b/dogpile/cache/backends/redis.py
@@ -24,7 +24,7 @@ __all__ = ("RedisBackend", "RedisSentinelBackend")
class RedisBackend(BytesBackend):
- """A `Redis <http://redis.io/>`_ backend, using the
+ r"""A `Redis <http://redis.io/>`_ backend, using the
`redis-py <http://pypi.python.org/pypi/redis/>`_ backend.
Example configuration::
@@ -88,6 +88,14 @@ class RedisBackend(BytesBackend):
asynchronous runners, as they run in a different thread than the one
used to create the lock.
+ :param connection_kwargs: dict, additional keyword arguments are passed
+ along to the
+ ``StrictRedis.from_url()`` method or ``StrictRedis()`` constructor
+ directly, including parameters like ``ssl``, ``ssl_certfile``,
+ ``charset``, etc.
+
+ .. versionadded:: 1.1.6 Added ``connection_kwargs`` parameter.
+
"""
def __init__(self, arguments):
@@ -98,12 +106,12 @@ class RedisBackend(BytesBackend):
self.password = arguments.pop("password", None)
self.port = arguments.pop("port", 6379)
self.db = arguments.pop("db", 0)
- self.distributed_lock = arguments.get("distributed_lock", False)
+ self.distributed_lock = arguments.pop("distributed_lock", False)
self.socket_timeout = arguments.pop("socket_timeout", None)
-
- self.lock_timeout = arguments.get("lock_timeout", None)
- self.lock_sleep = arguments.get("lock_sleep", 0.1)
- self.thread_local_lock = arguments.get("thread_local_lock", True)
+ self.lock_timeout = arguments.pop("lock_timeout", None)
+ self.lock_sleep = arguments.pop("lock_sleep", 0.1)
+ self.thread_local_lock = arguments.pop("thread_local_lock", True)
+ self.connection_kwargs = arguments.pop("connection_kwargs", {})
if self.distributed_lock and self.thread_local_lock:
warnings.warn(
@@ -112,7 +120,7 @@ class RedisBackend(BytesBackend):
)
self.redis_expiration_time = arguments.pop("redis_expiration_time", 0)
- self.connection_pool = arguments.get("connection_pool", None)
+ self.connection_pool = arguments.pop("connection_pool", None)
self._create_client()
def _imports(self):
@@ -131,6 +139,7 @@ class RedisBackend(BytesBackend):
self.reader_client = self.writer_client
else:
args = {}
+ args.update(self.connection_kwargs)
if self.socket_timeout:
args["socket_timeout"] = self.socket_timeout
@@ -269,9 +278,11 @@ class RedisSentinelBackend(RedisBackend):
a normal Redis connection can be specified here.
Default is {}.
- :param connection_kwargs: dict, are keyword arguments that will be used
- when establishing a connection to a Redis server.
- Default is {}.
+ :param connection_kwargs: dict, additional keyword arguments are passed
+ along to the
+ ``StrictRedis.from_url()`` method or ``StrictRedis()`` constructor
+ directly, including parameters like ``ssl``, ``ssl_certfile``,
+ ``charset``, etc.
:param lock_sleep: integer, number of seconds to sleep when failed to
acquire a lock. This argument is only valid when
@@ -290,7 +301,6 @@ class RedisSentinelBackend(RedisBackend):
self.sentinels = arguments.pop("sentinels", None)
self.service_name = arguments.pop("service_name", "mymaster")
self.sentinel_kwargs = arguments.pop("sentinel_kwargs", {})
- self.connection_kwargs = arguments.pop("connection_kwargs", {})
super().__init__(
arguments={
diff --git a/tests/cache/test_redis_backend.py b/tests/cache/test_redis_backend.py
index 5bdadc3..284aa5e 100644
--- a/tests/cache/test_redis_backend.py
+++ b/tests/cache/test_redis_backend.py
@@ -195,3 +195,23 @@ class RedisConnectionTest(TestCase):
def test_connect_with_url(self, MockStrictRedis):
arguments = {"url": "redis://redis:password@127.0.0.1:6379/0"}
self._test_helper(MockStrictRedis.from_url, arguments)
+
+ def test_extra_arbitrary_args(self, MockStrictRedis):
+ arguments = {
+ "url": "redis://redis:password@127.0.0.1:6379/0",
+ "connection_kwargs": {
+ "ssl": True,
+ "encoding": "utf-8",
+ "new_redis_arg": 50,
+ },
+ }
+ self._test_helper(
+ MockStrictRedis.from_url,
+ {
+ "url": "redis://redis:password@127.0.0.1:6379/0",
+ "ssl": True,
+ "encoding": "utf-8",
+ "new_redis_arg": 50,
+ },
+ arguments,
+ )