Store data in Redis Cache via Laravel 5.3 is not working

Viewed 6622

I just attached the redis driver to Laravel cache.

Config:

   'redis' => [
        'driver' => 'redis',
        'client' => 'predis',
        'cluster' => false,

        'default' => [
          'host' => env('REDIS_HOST', 'localhost'),
          'password' => env('REDIS_PASSWORD', null),
          'port' => env('REDIS_PORT', 6379),
          'database' => 0,
          'read_write_timeout' => 60,
        ],

A basic one:

  Cache::store('redis')->put('bar', 'baz', 10);
  $val = Cache::get('bar');
  Log::Debug($val);`

It returns an empty string. If I do:

  Cache::put('bar', 'baz', 10);
  $val = Cache::get('bar');
  Log::Debug($val);

It returns 'baz'. But If I delete the put method, at the next attempt it will return again the empty string.

CLI monitor:

1505914946.350596 [0 127.0.0.1:39102] "SELECT" "0" 1505914946.351143 [0 127.0.0.1:39102] "SETEX" "laravel:bar" "600000" "s:3:\"baz\";"

Where I get it wrong?

2 Answers

It looks like your default driver is not redis. For this reason, when you are calling put() or get() without specifying the store() you are putting and getting from your default store.

There are two ways you can solve this.

Either, make redis your default store (see config/cache.php). Then this:

Cache::put('bar', 'baz', 10);
$val = Cache::get('bar');
Log::Debug($val);

Will just work and you'll be accessing the redis store.

Or, specify the store when your putting and getting:

Cache::store('redis')->put('bar', 'baz', 10);
$val = Cache::store('redis')->get('bar');
Log::Debug($val);

find CACHE_DRIVER in .env, then to edit it:

CACHE_DRIVER=redis //the default is file
Related