redis中獲取沒有設置ttl過期時間的key


 

需求:redis作為一個內存型的數據庫,我們需要對過期key保持關注,從info keyspace中可以看出有多少key沒有設置過期時間,那么到底是哪些呢?

說明:關於redis ttl 的返回值,請參考http://redisdoc.com/key/ttl.html

測試數據:

5.5.5.101:6379> get c_100
"100"
5.5.5.101:6379> ttl c_100
(integer) -1
5.5.5.101:6379> expire c_100 600
(integer) 1
5.5.5.101:6379> expire c_1000 600
(integer) 1
5.5.5.101:6379> expire c_888 600
(integer) 1
5.5.5.101:6379> dbsize
(integer) 10000
5.5.5.101:6379> info keyspace
# Keyspace
db0:keys=10000,expires=3,avg_ttl=583699

獲取沒有設置ttl過期的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
    ttl_result=`redis-cli -h $db_ip -p $db_port -a $password ttl $line`
    if [[ $ttl_result == -1 ]];then
        echo $line >> no_ttl.log
    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
        ttl_result=`redis-cli -h $db_ip -p $db_port -a $password ttl $line`
        if [[ $ttl_result == -1 ]];then
            echo $line >> no_ttl.log
        fi
    done
done
rm -rf scan_tmp_result
rm -rf scan_result

查看結果:

[redis@lxd-vm1 ~]$ wc -l no_ttl.log 
9997 no_ttl.log
[redis@lxd-vm1 ~]$ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM