2

Problem:

I added a value to redis using Python code, and when I try to query is using Laravel

  • Redis::get('key_name') it returns null.
  • Redis::keys('*') returns values created using Laravel but not Python
  • Redis::scan('*') returns all values even those created using Python

Research:

Question:

Why is keys('*') not returning the key but scan('*') is and how can I get the value if get('key_name') is returning null?

Laravel: 7.30.4

Python: 3.8.3

Redis: 6.0

shmuels
  • 1,039
  • 1
  • 9
  • 22

1 Answers1

4

Laravel adds a prefix to all keys created. That prefix is defined in the redis config in database.php.

'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),

I haven't looked at the source code yet but most likely when laravel looks for keys it expects the prefix and appends that to what you passed to keys or get. So if you passed keys('key_name') it'll search for prefix_key_name which is why get returned null and keys didn't return my key created via Python as opposed to the one created via Laravel. I guess scan works a little differently and returns all keys regardless of its prefix.

If you set the default of your prefix to null ('prefix' => env('REDIS_PREFIX', null) then your key will be returned.

Using get and append the prefix, like this Redis::get('prefix_key_name') doesn't work.

shmuels
  • 1,039
  • 1
  • 9
  • 22