JaChNo
JaChNo

Reputation: 1563

Redis connection [cache] not configured error with laravel

I am trying to use redis for caching in laravel.

I have install redis locally and I know it is working as I am able to run horizon queues and workers.

I also get a response when doing

redis-cli PING

but when I try this code

$user = Cache::get('User:' .$Id , function ($Id) {
    return User::where('id', '=', $Id)->firstOrFail();
});

I get the error

 Redis connection [cache] not configured.

I have changed my .env to have the following entry

CACHE_DRIVER=redis

Upvotes: 3

Views: 18357

Answers (5)

Taziri Herrera Arocha
Taziri Herrera Arocha

Reputation: 35

The "conection [cache] not configured" error occurs when you don't have a connection with that name configured.

You need configure in config/database.php. Normaly, only have "default":

'default' => [
[
    'host' => env('REDIS_HOST_1', '127.0.0.1'),
    'password' => env('REDIS_PASSWORD'),
    'port' => env('REDIS_PORT_1', 1234),
    'database' => 0,
]

]

you need add extra:

'cache' => [
        [
            'host' => env('REDIS_HOST_1', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT_1', 1234),
            'database' => 0,
        ]
    ]

This way, you'll be able to use two connections, one specific to the cache and one for queues for example.

you'd have to put in config/cache.php:

'redis' => [
        'driver' => 'redis',
        'connection' => 'cache',
    ]

and put in config/queue.php:

'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => '{default}',
        'retry_after' => 90,
    ],

In the 'connection' section, only the values configured in 'database.php' will be available.

This also requires that the required users in the cluster be configured in redis or that the connections be separated by database. But that's already at the Redis service configuration level.

Upvotes: 0

Juan Lago
Juan Lago

Reputation: 1048

Additionally to other answer and in case that you use the redis as queue driver, verify that redis connection is set to "default" in the queue.phpconfiguration file.

Upvotes: 0

Jignesh Joisar
Jignesh Joisar

Reputation: 15175

can u set redis configration in config/database.php like this

'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', 'footbar'), //if password otherwise set null 
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

other wise set in .env file

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

then run artisan command

php artisan config:cache
php artisan config:clear 

Upvotes: 1

Bhargav Variya
Bhargav Variya

Reputation: 775

step 1 : Configuration : config/database.php add below array

'redis' => array(
'cluster' => false,
'default' => array(
'host'     => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env(‘REDIS_DATABASE',0),
),
),

Step 2 : Configuration : .env file

CACHE_DRIVER=redis
REDIS_DATABASE=0 

Step 3 : Config/cache.php

'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],

Upvotes: 0

小林智浩
小林智浩

Reputation: 71

Find this in cache.php

    'redis' => [
        'driver' => 'redis',
        'connection' => 'cache',
    ],

and change it to:

    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],

Upvotes: 7

Related Questions