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