按过期时间定期删除 redis Key

2023-11-11 09:09:34 浏览数 (1)

脚本文件 /home/redis/expired_system_message/unlink.sh 内容如下:

代码语言:javascript复制
#!/bin/bash

cd ~/expired_system_message/

ip1=10.10.10.10
ip2=10.10.10.11
ip3=10.10.10.12
   
master_ip=`/home/redis/redis-5.0.3/src/redis-cli -h $ip3 -p 30001 info | grep 20004 | awk -F: '{print $2}' | awk -F= '{print $4}'`

case "$master_ip" in
   $ip1)
   slave_ip="$ip3"
   ;;
   $ip2)
   slave_ip="$ip1"
   ;;
   $ip3)
   slave_ip="$ip2"
   ;;
esac 

cat a.txt | /home/redis/redis-5.0.3/src/redis-cli -h $slave_ip -p 20004 -a plzasmdn > b.txt
cat b.txt | awk '{print "ttl "$0}' > c.txt
cat c.txt | /home/redis/redis-5.0.3/src/redis-cli -h $slave_ip -p 20004 -a plzasmdn > d.txt
paste b.txt d.txt > e.txt
cat e.txt | egrep -v '-1|-2' | sort -k2n > f.txt

# num=13824000
num=14256000
awk '{if($2<='$num') print "unlink "$1}' f.txt > g.txt

cat g.txt | /home/redis/redis-5.0.3/src/redis-cli -h $master_ip -p 20004 -a plzasmdn --pipe

a.txt 内容如下:

代码语言:javascript复制
keys *

crontab 调度执行:

代码语言:javascript复制
0 4 * * * /home/redis/expired_system_message/unlink.sh > /home/redis/expired_system_message/unlink.log 2>&1

0 人点赞