Laravel Redis缓存前缀不匹配
Laravel Redis cache prefix missmatch
我正在使用 Redis 作为我的缓存驱动程序,我想扩展按模式删除键的功能。
当我在 Redis CLI 中列出缓存时,我得到:
127.0.0.1:6379[1]> keys *
1) "workspace_database_workspace_cache:table_def_workspace_items"
2) "workspace_database_workspace_cache:table_def_workspaces"
然而,当我从 Illuminate\Cache\RedisStore
转储 $this->prefix
时,我得到:
"workspace_cache:"
出于某种原因,我的删除不起作用。当我尝试使用以下方式获取密钥时:
public function keys($pattern = '*')
{
return $this->connection()->keys($pattern);
}
我按预期取回了钥匙。
但是如果我尝试删除它们,我会失败(调用 Cache::forgetByPattern('*items')
:
public function forgetByPattern($key)
{
foreach ($this->keys($key) as $item) {
$this->connection()->del($item);
}
return true;
}
此处的物品转储显示完全 workspace_database_workspace_cache:table_def_workspace_items
。
如果我通过在前缀后提供精确的键来删除(就像原来的 forget()
方法函数):
$this->connection()->del($this->prefix.'table_def_workspace_items');
肯定会删除密钥。
我也试过做一个:
$this->connection()->del('*items');
和
$this->connection()->del($this->prefix.'*items');
编辑:重新检查文档,Redis doesn't provide DEL by pattern。
但是 none 这些工作。为什么会失败,为什么我要添加额外的前缀?
Ersoy 使用 Redis monitor
函数让我走上了正确的道路。这是有效的最终产品:
public function forgetByPattern($key)
{
foreach ($this->keys($key) as $item) {
$item = explode(':', $item);
$this->forget($item[1]);
}
return true;
}
此外,我感到困惑的前缀来自 redis.options.prefix
键下的 database.php
配置文件。
我正在使用 Redis 作为我的缓存驱动程序,我想扩展按模式删除键的功能。
当我在 Redis CLI 中列出缓存时,我得到:
127.0.0.1:6379[1]> keys *
1) "workspace_database_workspace_cache:table_def_workspace_items"
2) "workspace_database_workspace_cache:table_def_workspaces"
然而,当我从 Illuminate\Cache\RedisStore
转储 $this->prefix
时,我得到:
"workspace_cache:"
出于某种原因,我的删除不起作用。当我尝试使用以下方式获取密钥时:
public function keys($pattern = '*')
{
return $this->connection()->keys($pattern);
}
我按预期取回了钥匙。
但是如果我尝试删除它们,我会失败(调用 Cache::forgetByPattern('*items')
:
public function forgetByPattern($key)
{
foreach ($this->keys($key) as $item) {
$this->connection()->del($item);
}
return true;
}
此处的物品转储显示完全 workspace_database_workspace_cache:table_def_workspace_items
。
如果我通过在前缀后提供精确的键来删除(就像原来的 forget()
方法函数):
$this->connection()->del($this->prefix.'table_def_workspace_items');
肯定会删除密钥。
$this->connection()->del('*items');
和
$this->connection()->del($this->prefix.'*items');
编辑:重新检查文档,Redis doesn't provide DEL by pattern。
但是 none 这些工作。为什么会失败,为什么我要添加额外的前缀?
Ersoy 使用 Redis monitor
函数让我走上了正确的道路。这是有效的最终产品:
public function forgetByPattern($key)
{
foreach ($this->keys($key) as $item) {
$item = explode(':', $item);
$this->forget($item[1]);
}
return true;
}
此外,我感到困惑的前缀来自 redis.options.prefix
键下的 database.php
配置文件。