需求:reids中删除某个前缀的所有key
说明:代码中的0:2标识从key前缀中截取前2个字符,这里示例的时候比如“b_”前缀,使用时候根据实际情况截取对应的长度进行判断即可。
生成测试数据
#!/bin/bash ID=1 while(($ID<10001)) do redis-cli -c -h 5.5.5.101 -p 6379 -a abc123 set "a_$ID" "$ID" redis-cli -c -h 5.5.5.101 -p 6379 -a abc123 set "b_$ID" "$ID" redis-cli -c -h 5.5.5.101 -p 6379 -a abc123 set "c_$ID" "$ID" ID=$(($ID+1)) done
删除前缀为“b_”的所有key
db_ip=5.5.5.101 db_port=6379 password=abc123 cursor=0 cnt=100 new_cursor=0 redis-cli -h $db_ip -p $db_port -a $password scan $cursor count $cnt > scan_tmp_result new_cursor=`sed -n '1p' scan_tmp_result` sed -n '2,$p' scan_tmp_result > scan_result cat scan_result |while read line do
if [[ ${line:0:2} == "b_" ]];then redis-cli -h $db_ip -p $db_port -a $password del $line > /dev/null fi done while [ $cursor -ne $new_cursor ] do redis-cli -h $db_ip -p $db_port -a $password scan $new_cursor count $cnt > scan_tmp_result new_cursor=`sed -n '1p' scan_tmp_result` sed -n '2,$p' scan_tmp_result > scan_result cat scan_result |while read line do
if [[ ${line:0:2} == "b_" ]];then redis-cli -h $db_ip -p $db_port -a $password del $line > /dev/null fi done done rm -rf scan_tmp_result rm -rf scan_result