6

I'm using Laravel Cache facade, and the CACHE_DRIVER=redis. All data is saved in Redis successfully, but when I use redis-cli and run keys* there are not keys!

When using the command flushall in redis-cli it loads the data again from the database, so that means the keys are already stored in Redis.

karel
  • 5,489
  • 46
  • 45
  • 50
Faid
  • 554
  • 1
  • 5
  • 18

2 Answers2

21

Redis has 16 databases indexed 0 - 15. The default database index is 0, so when you run redis commands without specifying the database index, you're only running commands against database index 0. However, as of Laravel 5.7, Laravel stores all the cache data in database index 1.

In order to see the keys in your cache database, you need to query database 1. You can either use the -n switch on the command line to specify the database index, or use the select command at the redis prompt to change the active database.

redis-cli -n 1 keys "*"

or

#> redis-cli
127.0.0.1:6379> select 1
127.0.0.1:6379[1]> keys *
patricus
  • 59,488
  • 15
  • 143
  • 145
3

Update

This may have to do with Laravel cache prefixes in the redis block in the database.php config file. See here

See this answer for addtional details.

Previous Ans.

I had the same issue and I tried the accepted ans., however different databases was not the issue.

I was able to find the missing key using scan, like this Redis::scan('*'). Why using Redis::keys('*') is not returning the key, is still a mystery.

(To note only key:values created using Python were not returned with keys, those created with Laravel were.)

shmuels
  • 1,039
  • 1
  • 9
  • 22
  • I saw the same issue. I found out that it does not happen when I set both the `default` and `cache` Redis connection to the same DB number. I'm not sure what Laravel does with the `default` connection but placing the same value for `REDIS_DB` in `database.php` seems to solve it for me. – Amir Rahwane Dec 03 '22 at 02:55