为什么Laravel Redis :: scan('*')返回期望的键,而Redis :: keys('*')没有返回键? [英] Why is Laravel Redis::scan('*') returning an expected key but Redis::keys('*') is not?

查看:164
本文介绍了为什么Laravel Redis :: scan('*')返回期望的键,而Redis :: keys('*')没有返回键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python代码为redis添加了一个值,而当我尝试使用Laravel进行查询时

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

  • Redis :: get('key_name')会返回 null .
  • Redis :: keys('*')返回使用Laravel创建但不使用Python创建的值
  • Redis :: scan('*')返回所有值,甚至包括使用Python创建的值
  • 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
  • https://stackoverflow.com/a/56569380/9530790, noted this may be a database number issue, however that is not the case. Since I'm using the same database in the Python code as well as the Laravel code, (database 0)
  • https://stackoverflow.com/a/65278346/9530790, noted the same issue as above.

为什么 keys('*')不返回键,但是 scan('*')是,如果 get('key_name')返回空值?

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为创建的所有键添加前缀.该前缀在 database.php 中的 redis 配置中定义.

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 _'),

我还没有看过源代码,但是最有可能在laravel查找密钥时,它需要前缀并将其附加到您传递给 keys get 的内容上.因此,如果您传递了 keys('key_name'),它将搜索 prefix_key_name ,这就是为什么 get 返回 null 的原因和 keys 并没有返回我通过Python创建的密钥,而不是通过Laravel创建的密钥.我猜想 scan 的工作方式略有不同,并且不管前缀如何,都会返回所有键.

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.

如果将前缀的默认值设置为null('prefix'=> env('REDIS_PREFIX',null ),则将返回您的密钥.

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

使用 get 并附加前缀,例如 Redis :: get('prefix_key_name')无效.

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

这篇关于为什么Laravel Redis :: scan('*')返回期望的键,而Redis :: keys('*')没有返回键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆