zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [题解] [JSOI2011] 任务调度
    [题解] [JSOI2011] 棒棒糖
    [题解] [JSOI2011] 柠檬
    [题解] [JSOI2010] 排名
    [湖南集训] 谈笑风生
    BZOJ 4695 最假女选手 线段树
    HNOI 2010 物品调度 并查集 置换
    Luogu P4299 首都 LCT
    BZOJ 2738 矩阵乘法 整体二分
    51nod 1175 区间第k大 整体二分
  • 原文地址:https://www.cnblogs.com/imdba/p/10161145.html
Copyright © 2011-2022 走看看