4

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 2

2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! The first one doesn't work but If I specify the store when I get date it works. Cheers!
Sure thing. What is the value of your default in config/cache.php?
Is 'default' => env('CACHE_DRIVER', 'redis')
And in your .env what is CACHE_DRIVER set to?
That's why the first isn't working for you. If you do want your default to be redis, change that and you won't have to specify the store.
|
1

find CACHE_DRIVER in .env, then to edit it:

CACHE_DRIVER=redis //the default is file

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.