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;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM