I'm trying to set a single Redis configuration in database.php
to cover both my local and production (Redis Cluster) environments in Laravel (5.8).
This config works with my local (APP_ENV=local
) Redis instance:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
And this works with my production (APP_ENV=production
) Redis cluster:
'redis' => [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
],
I want to set a single config so that if APP_ENV=local
is set, my local Redis instance is used and setting APP_ENV=production
uses my Redis cluster.
I tried the following (with REDIS_CLUSTER
set to true) in my APP_ENV=production
environment:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
but this fails with a MOVED error.
I posted a question on laracasts.com/discuss but that has left me more confused as it seems to suggest that I update my app code (rather than my Redis config in database.php
) to accommodate both environments. I feel like I'm missing something obvious here in how the configuration should work.