Batch clear redis memory data

 

1. Export all keys of redis.

    The speed is still relatively fast, about 1G of data exports 100M of keys.

 

2. Split into small files.

    One is that large files will occupy a lot of memory on the server, and the other is to process a small part of the data to check whether the normal data has been cleared.

 

3. Loop, query the value of all keys, delete according to conditions

    Here is implemented using php script to perform some logic processing.

    The business is simple. You can simply implement redis-cli KEYS "key_prefix*" | xargs redis-cli DEL with a pipeline command. //Note the *

 

// exec('redis-cli KEYS "*" > redis_keys.txt');
// split -b 10m redis.txt redis_keys_
if( !isset($argv[1])){
	echo 'Please enter the file name of the key like this: php del.php redis_keys_a'."\n";
	the;
}
$content = file_get_contents($argv[1]);
$arr = explode("\n", $content);
$num = count($arr);
for($i=0;$i<$num ;$i++){
	echo $i."\n";
	if($arr[$i]==''){
		continue;
	}
	exec("redis-cli get $arr[$i]", $info);
        $ str = $ info [0];
        unset($info);
	if($str==''){
		echo "empty\n";
		continue;
	}
	$content = unserialize(unserialize($str));
	if( !isset($content['user'])){
		echo "no user\n";
		exec("redis-cli del $arr[$i]");
	}elseif(isset($content['user']) && strpos($content['user']['tel'], 'a')!==false){
		exec("redis-cli del $arr[$i]");
		echo $content['user']['tel']."\n";
	}else{
		echo $content['user']['tel']."\n";
	}
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326864095&siteId=291194637