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

reids中刪除某個字首的所有key

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