1. 程式人生 > >redis scan刪除key的方法封裝

redis scan刪除key的方法封裝

public function delByScan(array $matchGroup = [])
{
if (empty($matchGroup)) return true;
$redis = RedisUtil::instance(['prefix' => '']);
$it = null;
$count = 1000;
do {
echo "scan start\n";
$arr_keys = $redis->scan($it, null, $count);
if (is_array($arr_keys) && count($arr_keys) > 0){
foreach ($arr_keys as $key){
foreach ($matchGroup as $match){
if (strpos($key, $match) !== false){
$redis->del($key);
echo "delete key => {$key} done\n";
}
}
}
}
echo "scan restart\n";
} while ($it > 0);

    return true;
}