Redis Cluster检查没有设置ttl的键-AI
Redis Cluster检查没有设置ttl的键-AI,AI终将取代程序员。
运行过程:
=== Redis TTL 检查工具 ===
当前时间: 2025-03-27 10:11:10
==============================
正在连接 Redis 7.2 集群…
开始扫描没有 TTL 的键…
已扫描 10000 个键,速率 3087.2 键/秒…
已扫描 20000 个键,速率 2361.4 键/秒…
已扫描 30000 个键,速率 2270.2 键/秒…
已扫描 40000 个键,速率 2368.7 键/秒…
已扫描 50000 个键,速率 2478.6 键/秒…
已扫描 60000 个键,速率 2522.9 键/秒…
已扫描 70000 个键,速率 2528.9 键/秒…
已扫描 80000 个键,速率 2540.0 键/秒…
已扫描 90000 个键,速率 2649.8 键/秒…
已扫描 100000 个键,速率 2720.2 键/秒…
已扫描 110000 个键,速率 2797.3 键/秒…
已扫描 120000 个键,速率 2860.5 键/秒…
已扫描 130000 个键,速率 2890.9 键/秒…
已扫描 140000 个键,速率 2841.9 键/秒…
已扫描 150000 个键,速率 2899.1 键/秒…
已扫描 160000 个键,速率 2948.7 键/秒…
已扫描 170000 个键,速率 2979.8 键/秒…
已扫描 180000 个键,速率 2998.0 键/秒…
已扫描 190000 个键,速率 3031.6 键/秒…
已扫描 200000 个键,速率 3041.3 键/秒…
已扫描 210000 个键,速率 3006.7 键/秒…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#!/usr/bin/env python3 import redis from redis.cluster import RedisCluster, ClusterNode import time import sys # Redis 7.2 Cluster 配置 REDIS_CLUSTER = { "NODES": [ {"host": "172.18.1.2", "port": 6379}, {"host": "172.18.1.3", "port": 6379}, {"host": "172.18.1.4", "port": 6379}, {"host": "172.18.1.5", "port": 6379}, {"host": "172.18.1.6", "port": 6379}, {"host": "172.18.1.7", "port": 6379} ], "PASSWORD": "12345678", "TIMEOUT": 7 * 24 * 60 * 60 } def find_keys_without_ttl(): """查找没有设置 TTL 的键""" print("正在连接 Redis 7.2 集群...") try: # 连接到 Redis 集群 cluster = RedisCluster( startup_nodes=[ClusterNode(node["host"], node["port"]) for node in REDIS_CLUSTER["NODES"]], password=REDIS_CLUSTER["PASSWORD"], decode_responses=True, skip_full_coverage_check=True ) # 测试连接 cluster.ping() except Exception as e: print(f"连接 Redis 集群出错: {str(e)}") sys.exit(1) print("开始扫描没有 TTL 的键...") # 统计信息 keys_without_ttl = [] total_keys = 0 start_time = time.time() # 使用 scan_iter 遍历所有键 batch_size = 1000 for key in cluster.scan_iter(match="*", count=batch_size): total_keys += 1 ttl = cluster.ttl(key) if ttl == -1: # -1 表示没有过期时间 keys_without_ttl.append(key) # 如果找到一个没有 TTL 的键,立即输出并继续扫描 print(f"找到一个没有 TTL 的键: {key}") # 每扫描 10000 个键显示一次进度 if total_keys % 10000 == 0: elapsed = time.time() - start_time rate = total_keys / elapsed if elapsed > 0 else 0 print(f"已扫描 {total_keys} 个键,速率 {rate:.1f} 键/秒...") elapsed_time = time.time() - start_time print(f"\n扫描完成,耗时 {elapsed_time:.2f} 秒。") print(f"总共扫描 {total_keys} 个键。") print(f"找到 {len(keys_without_ttl)} 个没有 TTL 的键。") # 输出所有没有 TTL 的键 if keys_without_ttl: print("\n没有 TTL 的键列表:") for key in keys_without_ttl: print(f" - {key}") return keys_without_ttl if __name__ == "__main__": print("=== Redis TTL 检查工具 ===") print("当前时间: 2025-03-27 10:11:10") print("="*30) start_time = time.time() keys_without_ttl = find_keys_without_ttl() elapsed_time = time.time() - start_time print(f"\n操作完成,耗时 {elapsed_time:.2f} 秒。") if len(keys_without_ttl) == 1: # 如果恰好有一个键没有设置 TTL,特别标出来 print(f"\n根据要求,找到的没有 TTL 的键是: {keys_without_ttl[0]}") |