Skip to content

Commit 478c8a3

Browse files
[11.x] Rewrites redis for L11 (#9417)
* Rewrites `redis` * wip * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d90d11d commit 478c8a3

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

redis.md

+59-45
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Before using Redis with Laravel, we encourage you to install and use the [PhpRed
2020
If you are unable to install the PhpRedis extension, you may install the `predis/predis` package via Composer. Predis is a Redis client written entirely in PHP and does not require any additional extensions:
2121

2222
```shell
23-
composer require predis/predis
23+
composer require predis/predis:^2.0
2424
```
2525

2626
<a name="configuration"></a>
@@ -32,18 +32,27 @@ You may configure your application's Redis settings via the `config/database.php
3232

3333
'client' => env('REDIS_CLIENT', 'phpredis'),
3434

35+
'options' => [
36+
'cluster' => env('REDIS_CLUSTER', 'redis'),
37+
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
38+
],
39+
3540
'default' => [
41+
'url' => env('REDIS_URL'),
3642
'host' => env('REDIS_HOST', '127.0.0.1'),
43+
'username' => env('REDIS_USERNAME'),
3744
'password' => env('REDIS_PASSWORD'),
38-
'port' => env('REDIS_PORT', 6379),
39-
'database' => env('REDIS_DB', 0),
45+
'port' => env('REDIS_PORT', '6379'),
46+
'database' => env('REDIS_DB', '0'),
4047
],
4148

4249
'cache' => [
50+
'url' => env('REDIS_URL'),
4351
'host' => env('REDIS_HOST', '127.0.0.1'),
52+
'username' => env('REDIS_USERNAME'),
4453
'password' => env('REDIS_PASSWORD'),
45-
'port' => env('REDIS_PORT', 6379),
46-
'database' => env('REDIS_CACHE_DB', 1),
54+
'port' => env('REDIS_PORT', '6379'),
55+
'database' => env('REDIS_CACHE_DB', '1'),
4756
],
4857

4958
],
@@ -54,6 +63,11 @@ Each Redis server defined in your configuration file is required to have a name,
5463

5564
'client' => env('REDIS_CLIENT', 'phpredis'),
5665

66+
'options' => [
67+
'cluster' => env('REDIS_CLUSTER', 'redis'),
68+
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
69+
],
70+
5771
'default' => [
5872
'url' => 'tcp://127.0.0.1:6379?database=0',
5973
],
@@ -69,18 +83,14 @@ Each Redis server defined in your configuration file is required to have a name,
6983

7084
By default, Redis clients will use the `tcp` scheme when connecting to your Redis servers; however, you may use TLS / SSL encryption by specifying a `scheme` configuration option in your Redis server's configuration array:
7185

72-
'redis' => [
73-
74-
'client' => env('REDIS_CLIENT', 'phpredis'),
75-
76-
'default' => [
77-
'scheme' => 'tls',
78-
'host' => env('REDIS_HOST', '127.0.0.1'),
79-
'password' => env('REDIS_PASSWORD'),
80-
'port' => env('REDIS_PORT', 6379),
81-
'database' => env('REDIS_DB', 0),
82-
],
83-
86+
'default' => [
87+
'scheme' => 'tls',
88+
'url' => env('REDIS_URL'),
89+
'host' => env('REDIS_HOST', '127.0.0.1'),
90+
'username' => env('REDIS_USERNAME'),
91+
'password' => env('REDIS_PASSWORD'),
92+
'port' => env('REDIS_PORT', '6379'),
93+
'database' => env('REDIS_DB', '0'),
8494
],
8595

8696
<a name="clusters"></a>
@@ -92,35 +102,42 @@ If your application is utilizing a cluster of Redis servers, you should define t
92102

93103
'client' => env('REDIS_CLIENT', 'phpredis'),
94104

105+
'options' => [
106+
'cluster' => env('REDIS_CLUSTER', 'redis'),
107+
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
108+
],
109+
95110
'clusters' => [
96111
'default' => [
97112
[
98-
'host' => env('REDIS_HOST', 'localhost'),
113+
'url' => env('REDIS_URL'),
114+
'host' => env('REDIS_HOST', '127.0.0.1'),
115+
'username' => env('REDIS_USERNAME'),
99116
'password' => env('REDIS_PASSWORD'),
100-
'port' => env('REDIS_PORT', 6379),
101-
'database' => 0,
117+
'port' => env('REDIS_PORT', '6379'),
118+
'database' => env('REDIS_DB', '0'),
102119
],
103120
],
104121
],
105122

123+
// ...
106124
],
107125

108-
By default, clusters will perform client-side sharding across your nodes, allowing you to pool nodes and create a large amount of available RAM. However, client-side sharding does not handle failover; therefore, it is primarily suited for transient cached data that is available from another primary data store.
126+
By default, Laravel will use native Redis clustering since the `options.cluster` configuration value is set to `redis`. Redis clustering is a great default option, as it gracefully handles failover.
127+
128+
Laravel also supports client-side sharding. However, client-side sharding does not handle failover; therefore, it is primarily suited for transient cached data that is available from another primary data store.
109129

110-
If you would like to use native Redis clustering instead of client-side sharding, you may specify this by setting the `options.cluster` configuration value to `redis` within your application's `config/database.php` configuration file:
130+
If you would like to use client-side sharding instead of native Redis clustering, you may remove the `options.cluster` configuration value within your application's `config/database.php` configuration file:
111131

112132
'redis' => [
113133

114134
'client' => env('REDIS_CLIENT', 'phpredis'),
115135

116-
'options' => [
117-
'cluster' => env('REDIS_CLUSTER', 'redis'),
118-
],
119-
120136
'clusters' => [
121137
// ...
122138
],
123139

140+
// ...
124141
],
125142

126143
<a name="predis"></a>
@@ -135,25 +152,18 @@ If you would like your application to interact with Redis via the Predis package
135152
// ...
136153
],
137154

138-
In addition to the default `host`, `port`, `database`, and `password` server configuration options, Predis supports additional [connection parameters](https://github.com/nrk/predis/wiki/Connection-Parameters) that may be defined for each of your Redis servers. To utilize these additional configuration options, add them to your Redis server configuration in your application's `config/database.php` configuration file:
155+
In addition to the default configuration options, Predis supports additional [connection parameters](https://github.com/nrk/predis/wiki/Connection-Parameters) that may be defined for each of your Redis servers. To utilize these additional configuration options, add them to your Redis server configuration in your application's `config/database.php` configuration file:
139156

140157
'default' => [
141-
'host' => env('REDIS_HOST', 'localhost'),
158+
'url' => env('REDIS_URL'),
159+
'host' => env('REDIS_HOST', '127.0.0.1'),
160+
'username' => env('REDIS_USERNAME'),
142161
'password' => env('REDIS_PASSWORD'),
143-
'port' => env('REDIS_PORT', 6379),
144-
'database' => 0,
162+
'port' => env('REDIS_PORT', '6379'),
163+
'database' => env('REDIS_DB', '0'),
145164
'read_write_timeout' => 60,
146165
],
147166

148-
<a name="the-redis-facade-alias"></a>
149-
#### The Redis Facade Alias
150-
151-
Laravel's `config/app.php` configuration file contains an `aliases` array which defines all of the class aliases that will be registered by the framework. By default, no `Redis` alias is included because it would conflict with the `Redis` class name provided by the PhpRedis extension. If you are using the Predis client and would like to add a `Redis` alias, you may add it to the `aliases` array in your application's `config/app.php` configuration file:
152-
153-
'aliases' => Facade::defaultAliases()->merge([
154-
'Redis' => Illuminate\Support\Facades\Redis::class,
155-
])->toArray(),
156-
157167
<a name="phpredis"></a>
158168
### PhpRedis
159169

@@ -163,16 +173,18 @@ By default, Laravel will use the PhpRedis extension to communicate with Redis. T
163173

164174
'client' => env('REDIS_CLIENT', 'phpredis'),
165175

166-
// Rest of Redis configuration...
176+
// ...
167177
],
168178

169-
In addition to the default `scheme`, `host`, `port`, `database`, and `password` server configuration options, PhpRedis supports the following additional connection parameters: `name`, `persistent`, `persistent_id`, `prefix`, `read_timeout`, `retry_interval`, `timeout`, and `context`. You may add any of these options to your Redis server configuration in the `config/database.php` configuration file:
179+
In addition to the default configuration options, PhpRedis supports the following additional connection parameters: `name`, `persistent`, `persistent_id`, `prefix`, `read_timeout`, `retry_interval`, `timeout`, and `context`. You may add any of these options to your Redis server configuration in the `config/database.php` configuration file:
170180

171181
'default' => [
172-
'host' => env('REDIS_HOST', 'localhost'),
182+
'url' => env('REDIS_URL'),
183+
'host' => env('REDIS_HOST', '127.0.0.1'),
184+
'username' => env('REDIS_USERNAME'),
173185
'password' => env('REDIS_PASSWORD'),
174-
'port' => env('REDIS_PORT', 6379),
175-
'database' => 0,
186+
'port' => env('REDIS_PORT', '6379'),
187+
'database' => env('REDIS_DB', '0'),
176188
'read_timeout' => 60,
177189
'context' => [
178190
// 'auth' => ['username', 'secret'],
@@ -190,11 +202,13 @@ The PhpRedis extension may also be configured to use a variety of serializers an
190202
'client' => env('REDIS_CLIENT', 'phpredis'),
191203

192204
'options' => [
205+
'cluster' => env('REDIS_CLUSTER', 'redis'),
206+
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
193207
'serializer' => Redis::SERIALIZER_MSGPACK,
194208
'compression' => Redis::COMPRESSION_LZ4,
195209
],
196210

197-
// Rest of Redis configuration...
211+
// ...
198212
],
199213

200214
Currently supported serializers include: `Redis::SERIALIZER_NONE` (default), `Redis::SERIALIZER_PHP`, `Redis::SERIALIZER_JSON`, `Redis::SERIALIZER_IGBINARY`, and `Redis::SERIALIZER_MSGPACK`.

0 commit comments

Comments
 (0)