redis中获取没有设置ttl过时时间的key

 

需求:redis做为一个内存型的数据库,咱们须要对过时key保持关注,从info keyspace中能够看出有多少key没有设置过时时间,那么究竟是哪些呢?html

说明:关于redis ttl 的返回值,请参考http://redisdoc.com/key/ttl.htmlredis

测试数据:数据库

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

查看结果:spa

[redis@lxd-vm1 ~]$ wc -l no_ttl.log 
9997 no_ttl.log
[redis@lxd-vm1 ~]$
相关文章
相关标签/搜索