Redis查找所有不过期的redis值

admin 发布时间:2022-12-05 分类:记事 阅读:1034次 1 条评论

Redis使用Scan扫描获取所有的redis值,并将主键列表导出成no_ttl.log文件。如果无法执行redis-cli命令,需要安装redis:yum install redis

db_ip=127.0.0.1       # redis 连接IP
db_port=6379              # redis 端口
password='123456'  # redis 密码
cursor=0                  # 第一次游标
cnt=100                   # 每次迭代的数量
new_cursor=0              # 下一次游标
 
redis-cli -c -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  # 获取 key
cat scan_result |while read line             # 循环遍历所有 key
do
    ttl_result=`redis-cli -c -h $db_ip -p $db_port -a $password ttl $line`  # 获取key过期时间
    if [[ $ttl_result == -1 ]];then
    #if [ $ttl_result -eq -1 ];then          # 判断过期时间,-1 是不过期
        echo $line >> no_ttl.log             # 追加到指定日志
    fi
done
 
while [ $cursor -ne $new_cursor ]            # 若游标不为0,则证明没有迭代完所有的key,继续执行,直至游标为0
do
    redis-cli -c -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
        ttl_result=`redis-cli -c -h $db_ip -p $db_port -a $password ttl $line`
        if [[ $ttl_result == -1 ]];then
        #if [ $ttl_result -eq -1 ];then
            echo $line >> no_ttl.log
        fi
    done
done
rm -rf scan_tmp_result
rm -rf scan_result


上一篇:nexus搭建流程

已有1条留言

发表评论:

◎欢迎您的参与讨论。