【Redis】获取没有设置ttl的key脚本
发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,一 前言在运维Redis的时候,总会遇到使用不规范的业务设计,比如没有对key设置ttl,进而导致内存空间吃紧,通常的解决方法是在slave上dump 出来所有的key ,然后对文件进行遍历再分析。遇
千家信息网最后更新 2024年09月21日【Redis】获取没有设置ttl的key脚本
注意:
代码里面对没有ttl的key的输出做了限制,大家使用的时候可以调整阈值 或者去掉 全部输出到指定的文件里面。欢迎大家使用,并给出功能或者算法上的改进措施。
一 前言
在运维Redis的时候,总会遇到使用不规范的业务设计,比如没有对key设置ttl,进而导致内存空间吃紧,通常的解决方法是在slave上dump 出来所有的key ,然后对文件进行遍历再分析。遇到几十G的Redis实例,dump + 分析 会是一个比较耗时的操作,为此,我开发了一个小脚本直接连接Redis 进行scan 遍历所有的key,然后在检查key的ttl,将没有ttl的key输出到指定的文件里面。二 代码实现
- # encoding: utf-8
- """
- author: yangyi@youzan.com
- time: 2018/4/26 下午4:34
- func: 获取数据库中没有设置ttl的 key
- """
- import redis
- import argparse
- import time
- import sys
- class ShowProcess:
- """
- 显示处理进度的类
- 调用该类相关函数即可实现处理进度的显示
- """
- i = 0 # 当前的处理进度
- max_steps = 0 # 总共需要处理的次数
- max_arrow = 50 # 进度条的长度
- # 初始化函数,需要知道总共的处理次数
- def __init__(self, max_steps):
- self.max_steps = max_steps
- self.i = 0
- # 显示函数,根据当前的处理进度i显示进度
- # 效果为[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
- def show_process(self, i = None):
- if i is not None:
- self.i = i
- else:
- self.i += 1
- num_arrow = int(self.i * self.max_arrow / self.max_steps) # 计算显示多少个'>'
- num_line = self.max_arrow - num_arrow # 计算显示多少个'-'
- percent = self.i * 100.0 / self.max_steps # 计算完成进度,格式为xx.xx%
- process_bar = '[' + '>' * num_arrow + ' ' * num_line + ']'\
- + '%.2f' % percent + '%' + '\r' # 带输出的字符串,'\r'表示不换行回到最左边
- sys.stdout.write(process_bar) # 这两句打印字符到终端
- sys.stdout.flush()
- def close(self, words='done'):
- print ''
- print words
- self.i = 0
- def check_ttl(redis_conn, no_ttl_file, dbindex):
- start_time = time.time()
- no_ttl_num = 0
- keys_num = redis_conn.dbsize()
- print "there are {num} keys in db {index} ".format(num=keys_num, index=dbindex)
- process_bar = ShowProcess(keys_num)
- with open(no_ttl_file, 'a') as f:
- for key in redis_conn.scan_iter(count=1000):
- process_bar.show_process()
- if redis_conn.ttl(key) == -1:
- no_ttl_num += 1
- if no_ttl_num < 1000:
- f.write(key+'\n')
- else:
- continue
- process_bar.close()
- print "cost time(s):", time.time() - start_time
- print "no ttl keys number:", no_ttl_num
- print "we write keys with no ttl to the file: %s" % no_ttl_file
- def main():
- parser = argparse.ArgumentParser()
- parser.add_argument('-p', type=int, dest='port', action='store', help='port of redis ')
- parser.add_argument('-d', type=str, dest='db_list', action='store', default=0,
- help='ex : -d all / -d 1,2,3,4 ')
- args = parser.parse_args()
- port = args.port
- if args.db_list == 'all':
- db_list = [i for i in xrange(0, 16)]
- else:
- db_list = [int(i) for i in args.db_list.split(',')]
- for index in db_list:
- try:
- pool = redis.ConnectionPool(host='127.0.0.1', port=port, db=index)
- r = redis.StrictRedis(connection_pool=pool)
- except redis.exceptions.ConnectionError as e:
- print e
- else:
- no_ttl_keys_file = "/tmp/{port}_{db}_no_ttl_keys.txt".format(port=port, db=index)
- check_ttl(r, no_ttl_keys_file, index)
- if __name__ == '__main__':
- main()
注意:
代码里面对没有ttl的key的输出做了限制,大家使用的时候可以调整阈值 或者去掉 全部输出到指定的文件里面。欢迎大家使用,并给出功能或者算法上的改进措施。
进度
处理
输出
函数
文件
代码
字符
时候
次数
分析
脚本
吃紧
为此
业务
内存
前言
功能
字符串
实例
总会
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
苏州软件开发工程师待遇
中职网络技术试卷
亳州oa管理软件开发费用
计算机网络技术的个人技能怎么写
山西互联网软件开发公司
普陀区网络技术服务哪家好
php 嵌入式软件开发
宝塔mysql数据库配置
2021深度学习服务器硬件配置
小学生网络安全防范
什么是数据库sqlserver
我的世界服务器java
华为平板软件更新无法连接服务器
网络技术员证书有用吗
德州企业管理软件开发费用
淄博的软件开发公司招聘信息
金山区品质软件开发信息中心
中视电子软件开发公司
永恒纪元服务器选择
谷歌服务器代码
阿里服务器申请微信测试号
如何打开已备份的数据库
停车管理系统中数据库的作用
网络安全你我有责黑板报
计算机软件开发工程师的方向
讯致网络技术有限公司
剑魂软件开发平台
gis基础软件开发
服务器显示屏颜色不对
黑茶网络安全手抄报