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 ~]$
欢迎多交流(QQ:616793474/329003748),谢谢!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· 对象命名为何需要避免'-er'和'-or'后缀
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· SQL Server如何跟踪自动统计信息更新?