简体   繁体   English

通过Laravel 5.3将数据存储在Redis缓存中无效

[英]Store data in Redis Cache via Laravel 5.3 is not working

I just attached the redis driver to Laravel cache . 我只是将redis驱动程序附加到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'. 它返回'baz'。 But If I delete the put method, at the next attempt it will return again the empty string. 但是如果我删除put方法,在下一次尝试时它将再次返回空字符串。

CLI monitor: CLI监视器:

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 . 看起来您的默认驱动程序不是redis For this reason, when you are calling put() or get() without specifying the store() you are putting and getting from your default store. 因此,当您调用put()get()而不指定store()您将从默认存储中进行调用。

There are two ways you can solve this. 有两种方法可以解决这个问题。

Either, make redis your default store (see config/cache.php ). 或者,将redis config/cache.php默认存储(请参阅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. 只是工作,你将访问redis商店。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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