【Redis】用python操作redis集群
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,https://blog.csdn.net/bitcarmanlee/article/details/51852126密码不能写到列表中去:有密码 就是这样 redisconn = StrictRed
千家信息网最后更新 2025年11月07日【Redis】用python操作redis集群
结合上一篇文章,改进脚本后操作redis-cluster
https://blog.csdn.net/bitcarmanlee/article/details/51852126
密码不能写到列表中去:
- 有密码 就是这样 redisconn = StrictRedisCluster(startup_nodes=redis_nodes,password=password)
结合上一篇文章,改进脚本后操作redis-cluster
- [root@ip-172-31-47-226 tmp]# more /root/get_nottl.py
- # encoding: utf-8
- """
- author: yangyi@youzan.com
- time: 2018/4/26 下午4:34
- func: 获取数据库中没有设置ttl的 key
- """
- import redis
- import argparse
- import time
- import sys
- import rediscluster
- 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(int(self.i) * int(self.max_arrow) / int(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
- allkey = redis_conn.dbsize()
- keys_num = sum(list(set(allkey.values())))
- print "key分布如下:",allkey
- 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=10):
- process_bar.show_process()
- if redis_conn.ttl(key) == -1:
- no_ttl_num += 1
- if no_ttl_num < 10000:
- 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('-a', type=str, dest='password', action='store', help='password 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
- pwd = args.password
- 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:
- startup_nodes = [{"host": "172.31.47.226", "port": port,"db":index}]
- r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password=pwd)
- except Exception, 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()
进度
处理
函数
字符
密码
次数
字符串
就是
效果
数据
数据库
格式
篇文章
终端
脚本
长度
utf-8
上一
输出
集群
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
东营轻工业软件开发咨询
幼儿园小班暑假网络安全
2018湖湘杯网络安全
深圳pdu服务器电源生产公司
java 组态软件开发
服务器日租
上海寻梦网络技术有限公司
游戏软件开发人
郑州学习网络安全
矿山企业开发利用信息公示数据库
改行学软件开发
长宁区工商数据库系统销售要求
sas把txt转为数据库
高级网络安全工程
网络安全是一盘大棋申论
我的世界花雨庭空岛服务器
数据库的分析技术
安徽互助盘软件开发
云服务器操作系统下载
数据库语言次数
魔兽btc审判服务器如何
网络安全标准化委员会
苏州直播软件开发要多少钱
软件开发流程图模板双人
网络安全周先进个人遵义
东南大学网络安全学科评估
数据库安全的第一道屏障
kibana数据库数据导入
分布式数据库主键特点
网络安全知识竞赛证书电子版