2
votes

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
4

4 Answers

4
votes

Find this in cache.php

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

and change it to:

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

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 
0
votes

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',
    ],
0
votes

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.