redis scan删除key的方法封装

/**
* @desc 迭代式的删除redis key
* 用法:
* $redis = BaseService::S()->getRedisConfig(\Yii::$app->redis2);
* RedisHelper::delByScan(['mindCard'], $redis);
* @author yanglb@immatchu.com
* @created time 2018-12-29
* @param object $redisInstance redis数据库实例
* @param array $matchGroup 要删除的key(可以是key的前缀)所组成的数组
* @param int $count 一次最多删除多少条key
* @return bool
*/
public static function delByScan($redisInstance, array $matchGroup = [], $count = 1000)
{
if (empty($matchGroup)) {
return false;
}
$redis = $redisInstance;
$it = null;
do {
$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);
}
}
}
}
} while ($it > 0);
return true;
}

上一篇:celery使用


下一篇:app模块设计