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.")
相关推荐
Amelia1111117 分钟前
day47
python
Chris_121936 分钟前
Halcon学习笔记-Day6进阶:工业级视觉系统核心技术详解
人工智能·python·深度学习·halcon
ChineHe1 小时前
Redis基础篇004_Redis Pipeline流水线详解
数据库·redis·缓存
飞鸟真人1 小时前
关于python -m http.server的一些安全问题
python·安全·http
tjjucheng1 小时前
小程序定制开发哪家性价比高
python
No0d1es2 小时前
2025年12月 GESP CCF编程能力等级认证Python六级真题
python·青少年编程·gesp·ccf·六级
tc&2 小时前
redis_cmd 内置防注入功能的原理与验证
数据库·redis·bootstrap
亮子AI2 小时前
【Python】比较两个cli库:Click vs Typer
开发语言·python
CappuccinoRose2 小时前
流计算概述
python·flink·流计算·数据流·pyflink