1. 程式人生 > >reids中刪除某個前綴的所有key

reids中刪除某個前綴的所有key

使用 示例 ash bold null 數據 spa The pass

需求: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

reids中刪除某個前綴的所有key