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.")
相关推荐
编织幻境的妖6 分钟前
Python垃圾回收机制详解
开发语言·python
李剑一11 分钟前
Python学习笔记4
python
必胜刻22 分钟前
Redis哨兵模式(Linux)
linux·数据库·redis
listhi5201 小时前
MOEAD算法实现详解(基于Python与MATLAB)
python·算法·matlab
byzh_rc1 小时前
[认知计算] 循环神经网络
人工智能·python·rnn·深度学习·神经网络·机器学习
黑客思维者1 小时前
Python 机器学习TensorFlow 2.x 入门实战:CNN/RNN/Transformer
python·机器学习·tensorflow
二川bro1 小时前
类型错误详解:Python TypeError排查手册
android·java·python
计算机学姐1 小时前
基于Python的B站数据分析及可视化系统【2026最新】
开发语言·vue.js·python·信息可视化·数据挖掘·数据分析·推荐算法
haiyu_y1 小时前
Day 33 类的装饰器
python