Redis 通过 scan 找出不过期的 key
2024-09-08 01:51:20
SCAN 命令是一个基于游标的迭代器(cursor based iterator):SCAN 命令每次被调用之后,都会向用户返回一个新的游标,用户在下次迭代时需要使用这个新游标作为 SCAN 命令的游标参数,以此来延续之前的迭代过程。
注意:当 SCAN 命令的游标参数被设置为 0 时,服务器将开始一次新的迭代,而当服务器向用户返回值为 0 的游标时,表示迭代已结束!
编写脚本
#!/bin/bash
db_ip=127.0.0.1 # redis ip
db_port=6379 # redis 端口
password=ibalife # redis 密码
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 # 获取 keys
cat scan_result |while read line # 循环遍历所有 keys
do
ttl_result=`redis-cli -h $db_ip -p $db_port -a $password ttl $line` # 获取 key 过期时间
if [[ $ttl_result == -1 ]];then # 判断过期时间,-1 是不过期
echo $line >> no_ttl.log # 追加到指定文件
fi
done
while [ $cursor -ne $new_cursor ] # 若 游标 不为 0 ,则证明没有迭代完所有的 key,继续执行
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 占用内存很大,可以使用 tmux 命令新开一个窗口。最后所有符合的要求的 key 都会保存在 no_ttl.log 文件中
最新文章
- oracle 修改密码
- Oracle常用
- java下载网络图片
- 我的MYSQL学习心得(六)
- linux_rsync定时备份
- PHP中使用jQuery+Ajax实现分页查询多功能操作
- [web前端] css3 transition属性实现3d动画效果
- python框架之Django(6)-查询优化之select_related&;prefetch_related
- 【转】MySQL 当记录不存在时insert,当记录存在时update
- url中是否加斜杠/
- PYQT5学习笔记之各模块介绍
- Rendering with Replaced Shaders
- 谈谈Flash图表中数据的采集
- 河南省队选拔 HAOI2015 解题报告
- Rails 5 Test Prescriptions 第7章 double stub mock
- 20162326 齐力锋 2017-2018学期 Bag类的补写博客
- API 接口收集
- android根据原图片的路径得到该图片的缩略图
- elasticsearch2.x安装部署
- 运用Python制作你心目中的完美女神脸!
热门文章
- electron初探问题总结
- 【译】ModSecurity
- SpringBoot应用篇(二):SpringSecurity实现带验证码的登录认证 附代码
- 【BZOJ 2138】stone
- C# Task 多任务 限制Task并发数量
- How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象
- swift冒泡排序,swift快速排序,swift归并排序,swift插入排序,swift基数排序
- django-xadmin自定义widget插件(自定义详情页字段的显示样式)
- Oracle查看 open_cursors 和 session_cached_cursors
- 12C新功能:在线移动数据文件 (Doc ID 1566797.1)