Skip to content

Commit 6286e3b

Browse files
committed
fixes #16310 unclear alternate DSN format
1 parent 903ddbe commit 6286e3b

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

components/cache/adapters/redis_adapter.rst

+64-17
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ replaced by ``rediss`` (the second ``s`` means "secure").
6767

6868
.. note::
6969

70-
A `Data Source Name (DSN)`_ for this adapter must use the following format.
70+
A `Data Source Name (DSN)`_ for this adapter must use either one of the following formats.
7171

7272
.. code-block:: text
7373
7474
redis[s]://[pass@][ip|host|socket[:port]][/db-index]
7575
76+
.. code-block:: text
77+
78+
redis[s]:[[user]:pass@]?[ip|host|socket[:port]][&params]
79+
80+
Values for placeholders ``[user]``, ``[:port]``, ``[/db-index]`` and ``[&params]`` are optional.
81+
7682
Below are common examples of valid DSNs showing a combination of available values::
7783

7884
use Symfony\Component\Cache\Adapter\RedisAdapter;
@@ -89,20 +95,35 @@ Below are common examples of valid DSNs showing a combination of available value
8995
// socket "/var/run/redis.sock" and auth "bad-pass"
9096
RedisAdapter::createConnection('redis://bad-pass@/var/run/redis.sock');
9197

92-
// a single DSN can define multiple servers using the following syntax:
93-
// host[hostname-or-IP:port] (where port is optional). Sockets must include a trailing ':'
98+
// host "redis1" (docker container) with alternate DSN syntax and selecting database index "3"
99+
RedisAdapter::createConnection('redis:?host[redis1:6379]&dbindex=3');
100+
101+
// providing credentials with alternate DSN syntax
102+
RedisAdapter::createConnection('redis:default:verysecurepassword@?host[redis1:6379]&dbindex=3');
103+
104+
// a single DSN can also define multiple servers
94105
RedisAdapter::createConnection(
95106
'redis:?host[localhost]&host[localhost:6379]&host[/var/run/redis.sock:]&auth=my-password&redis_cluster=1'
96107
);
97108

98109
`Redis Sentinel`_, which provides high availability for Redis, is also supported
99-
when using the Predis library. Use the ``redis_sentinel`` parameter to set the
100-
name of your service group::
110+
when using the PHP Redis Extension v5.2+ or the Predis library. Use the ``redis_sentinel``
111+
parameter to set the name of your service group::
101112

102113
RedisAdapter::createConnection(
103114
'redis:?host[redis1:26379]&host[redis2:26379]&host[redis3:26379]&redis_sentinel=mymaster'
104115
);
105116

117+
// providing credentials
118+
RedisAdapter::createConnection(
119+
'redis:default:verysecurepassword@?host[redis1:26379]&host[redis2:26379]&host[redis3:26379]&redis_sentinel=mymaster'
120+
);
121+
122+
// providing credentials and selecting database index "3"
123+
RedisAdapter::createConnection(
124+
'redis:default:verysecurepassword@?host[redis1:26379]&host[redis2:26379]&host[redis3:26379]&redis_sentinel=mymaster&dbindex=3'
125+
);
126+
106127
.. versionadded:: 4.2
107128

108129
The option to define multiple servers in a single DSN was introduced in Symfony 4.2.
@@ -132,29 +153,31 @@ array of ``key => value`` pairs representing option names and their respective v
132153

133154
// associative array of configuration options
134155
[
135-
'lazy' => false,
156+
'class' => null,
136157
'persistent' => 0,
137158
'persistent_id' => null,
138-
'tcp_keepalive' => 0,
139159
'timeout' => 30,
140160
'read_timeout' => 0,
141161
'retry_interval' => 0,
162+
'tcp_keepalive' => 0,
163+
'lazy' => null,
164+
'redis_cluster' => false,
165+
'redis_sentinel' => null,
166+
'dbindex' => 0,
167+
'failover' => 'none',
168+
'ssl' => null,
142169
]
143170

144171
);
145172

146173
Available Options
147174
~~~~~~~~~~~~~~~~~
148175

149-
``class`` (type: ``string``)
176+
``class`` (type: ``string``, default: ``null``)
150177
Specifies the connection library to return, either ``\Redis`` or ``\Predis\Client``.
151178
If none is specified, it will return ``\Redis`` if the ``redis`` extension is
152-
available, and ``\Predis\Client`` otherwise.
153-
154-
``lazy`` (type: ``bool``, default: ``false``)
155-
Enables or disables lazy connections to the backend. It's ``false`` by
156-
default when using this as a stand-alone component and ``true`` by default
157-
when using it inside a Symfony application.
179+
available, and ``\Predis\Client`` otherwise. Explicitly set this to ``\Predis\Client`` for Sentinel if you are
180+
running into issues when retrieving master information.
158181

159182
``persistent`` (type: ``int``, default: ``0``)
160183
Enables or disables use of persistent connections. A value of ``0`` disables persistent
@@ -163,6 +186,10 @@ Available Options
163186
``persistent_id`` (type: ``string|null``, default: ``null``)
164187
Specifies the persistent id string to use for a persistent connection.
165188

189+
``timeout`` (type: ``int``, default: ``30``)
190+
Specifies the time (in seconds) used to connect to a Redis server before the
191+
connection attempt times out.
192+
166193
``read_timeout`` (type: ``int``, default: ``0``)
167194
Specifies the time (in seconds) used when performing read operations on the underlying
168195
network resource before the operation times out.
@@ -175,9 +202,28 @@ Available Options
175202
Specifies the `TCP-keepalive`_ timeout (in seconds) of the connection. This
176203
requires phpredis v4 or higher and a TCP-keepalive enabled server.
177204

178-
``timeout`` (type: ``int``, default: ``30``)
179-
Specifies the time (in seconds) used to connect to a Redis server before the
180-
connection attempt times out.
205+
``lazy`` (type: ``bool``, default: ``null``)
206+
Enables or disables lazy connections to the backend. It's ``false`` by
207+
default when using this as a stand-alone component and ``true`` by default
208+
when using it inside a Symfony application.
209+
210+
``redis_cluster`` (type: ``bool``, default: ``false``)
211+
Enables or disables redis cluster. The actual value passed is irrelevant as long as it passes loose comparison
212+
checks: `redis_cluster=1` will suffice.
213+
214+
``redis_sentinel`` (type: ``string``, default: ``null``)
215+
Specifies the master name connected to the sentinels.
216+
217+
``dbindex`` (type: ``int``, default: ``0``)
218+
Specifies the database index to select.
219+
220+
``failover`` (type: ``string``, default: ``none``)
221+
Specifies failover for cluster implementations. For ``\RedisCluster`` valid options are ``none`` (default),
222+
``error``, ``distribute`` or ``slaves``. For ``\Predis\ClientInterface`` valid options are ``slaves``
223+
or ``distribute``.
224+
225+
``ssl`` (type: ``bool``, default: ``null``)
226+
SSL context options. See `php.net/context.ssl`_ for more information.
181227

182228
.. note::
183229

@@ -225,3 +271,4 @@ Read more about this topic in the offical `Redis LRU Cache Documentation`_.
225271
.. _`TCP-keepalive`: https://redis.io/topics/clients#tcp-keepalive
226272
.. _`Redis Sentinel`: https://redis.io/topics/sentinel
227273
.. _`Redis LRU Cache Documentation`: https://redis.io/topics/lru-cache
274+
.. _`php.net/context.ssl`: https://php.net/context.ssl

0 commit comments

Comments
 (0)