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.")
相关推荐
爱睡懒觉的焦糖玛奇朵2 小时前
【从视频到数据集:焦糖玛奇朵的魔法工具使用说明】
人工智能·python·深度学习·学习·算法·yolo·音视频
yangshicong3 小时前
第11章:结构化输出与数据提取 —— 让 AI 直接返回你想要的数据格式
数据库·人工智能·redis·python·langchain·ai编程
言之。3 小时前
【Python】免费的中文 AI 配音方案
开发语言·人工智能·python
Warson_L3 小时前
python dict key详解
python
天天进步20153 小时前
Python全栈项目:从零手操一个高性能 API 网关
开发语言·python
一路向北·重庆分伦4 小时前
09:Redis-高级特性与原理
redis
安生生申4 小时前
使用pygame实现2048
开发语言·python·pygame
徐图图不糊涂5 小时前
搭建简易版的Rag系统
python·pycharm
灰灰勇闯IT5 小时前
pyasc:用 Python 调用 CANN 的推理能力
开发语言·python
明月_清风6 小时前
FastAPI 从入门到实战:3 分钟构建高性能异步 API
后端·python·fastapi