python scan方式查询redis所有数据是否含有重复的key

demo

python 复制代码
import redis

# 连接到 Redis 服务器
client = redis.StrictRedis(host='192.168.1.1', port=6379)

# 用于存储所有键和重复键的集合
all_keys = set()
duplicates = set()

# Redis 默认有 16 个数据库,索引从 0 到 15
for db in range(8):
    print(f"Scanning database {db}...")
    client.execute_command('SELECT', db)

    # 使用 SCAN 命令迭代获取 key
    cursor = '0'
    while cursor != 0:
        cursor, keys = client.scan(cursor=cursor, count=100)
        for key in keys:
            # 检查是否已经存在于集合中
            if key in all_keys:
                duplicates.add(key)
            else:
                all_keys.add(key)

# 输出结果
if duplicates:
    print("Duplicate keys found:")
    for key in duplicates:
        print(key.decode('utf-8'))  # keys are returned as bytes, so decode them
else:
    print("No duplicates found.")
相关推荐
凯瑟琳.奥古斯特11 小时前
数据库原理选择题精选
数据库·python·职场和发展
彦为君12 小时前
JavaSE-07-异常机制
java·开发语言·后端·python·spring
适应规律12 小时前
【无标题】
人工智能·python·算法
XLYcmy13 小时前
全链路验证测试系统:一个针对智能代理(Agent)系统全链路能力的自动化验证脚本
分布式·python·http·网络安全·ai·llm·agent
有味道的男人13 小时前
电商效率翻倍:京东全量商品信息抓取
python
原来是猿13 小时前
博客系统自动化测试实战总结
python
小江的记录本14 小时前
【JVM虚拟机】JVM调优:常用JVM参数、调优核心指标、OOM排查、GC日志分析、Arthas工具使用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
大数据魔法师14 小时前
Streamlit(十三)- API 参考文档(六)- 媒体展示组件
python·web
爱写代码的倒霉蛋14 小时前
Hello-Agents的第一个练习-5分钟实现一个智能体(实现详解)
python