3

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

5 Answers 5

7

Find this in cache.php

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

and change it to:

    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],
Sign up to request clarification or add additional context in comments.

Comments

1

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 

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.