如何检测指定元素是否在Redis list中?

fgt1t5y

问题描述

Redis的lpos命令可以获得指定元素在指定list中的索引值,可以快速知道元素是否存在。但是webman自带的Redis客户端不支持。

请问大佬还有检测指定元素在list是否存在的其他办法吗?

252 1 1
1个回答

北月

redis 服务端版本需要大于 5.0,如果没有安装 phpredis 扩展,而是使用 predis/predis 包,可以使用 executeRaw
以下是测试代码:

$listKey = 'mylist';
// 测试 LPOS 命令
$searchElement = 'apple';
// 客户端为 phpreids
// $position = Redis::lPos($listKey, $searchElement);
// 客户端为 predis
$position = Redis::executeRaw(['LPOS', $listKey, $searchElement]);
if ($position !== false) {
    echo "Element '$searchElement' found at position: $position\n";
} else {
    echo "Element '$searchElement' not found\n";
}
  • 暂无评论
×
🔝