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.")
相关推荐
小江的记录本8 小时前
【JVM虚拟机】垃圾回收GC:四种引用类型:强引用、软引用、弱引用、虚引用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
APIshop8 小时前
Python 获取 1688 商品采集 API 接口 | 工厂货源自动化对接商品信息 | 无需选品
运维·python·自动化
deepin_sir8 小时前
10 - 函数
开发语言·python
charlee449 小时前
《GIS基础原理与技术实践》配套案例(Python版)
python·conda·numpy·gis·环境配置
枫叶林FYL9 小时前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
洛水水10 小时前
Redis 分布式锁详解:实现与缺陷
数据库·redis·分布式
渣渣xiong11 小时前
从零开始:前端转型AI agent直到就业第五十七天-第五十八天
前端·人工智能·python
tongluowan00712 小时前
Redisson的参数及工作原理
java·redis·lua·分布式锁
小L~~~12 小时前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
才兄说12 小时前
机器人二次开发机器人动作定制?动作迁移数据优化
python