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.")
相关推荐
赵钰老师14 小时前
【ADCIRC】基于“python+”潮汐、风驱动循环、风暴潮等海洋水动力模拟实践技术应用
python·信息可视化·数据分析
爬山算法14 小时前
MongoDB(80)如何在MongoDB中使用多文档事务?
数据库·python·mongodb
YuanDaima204815 小时前
基于 LangChain 1.0 的检索增强生成(RAG)实战
人工智能·笔记·python·langchain·个人开发·langgraph
RopenYuan15 小时前
FastAPI -API Router的应用
前端·网络·python
听风吹等浪起15 小时前
用Python和Pygame从零实现坦克大战
开发语言·python·pygame
书到用时方恨少!15 小时前
Python Pandas 使用指南:数据分析的瑞士军刀
python·数据分析·pandas
智算菩萨16 小时前
【Pygame】第8章 文字渲染与字体系统(支持中文字体)
开发语言·python·pygame
:mnong16 小时前
全图纸语义理解升级分析
python·openvino·paddleocr·qt6.3·paddleocr-vl
qh0526wy16 小时前
pathlib 核心功能一览
python
lifewange17 小时前
Redis的测试要点和测试方法
数据库·redis·缓存