简体   繁体   中英

Store data in Redis Cache via Laravel 5.3 is not working

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?

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);

找到CACHE_DRIVER.env ,然后进行编辑:

CACHE_DRIVER=redis //the default is file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM