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.")
相关推荐
花酒锄作田3 小时前
企业微信机器人与 DeepAgents 集成实践
python·mcp·deepagents
SPC的存折3 小时前
1、Redis数据库基础
linux·运维·服务器·数据库·redis·缓存
likerhood5 小时前
java中`==`和`.equals()`区别
java·开发语言·python
qq_283720055 小时前
Python Celery + FastAPI + Vue 全栈异步任务实战
vue.js·python·fastapi
2401_885885046 小时前
营销推广短信接口集成:结合营销策略实现的API接口动态变量填充方案
前端·python
telllong6 小时前
Python异步编程从入门到不懵:asyncio实战踩坑7连发
开发语言·python
lulu12165440788 小时前
Claude Code Harness架构技术深度解析:生产级AI Agent工程化实践
java·人工智能·python·ai编程
Rick19939 小时前
Redis 分布式锁:核心使用场景
数据库·redis·分布式
身如柳絮随风扬10 小时前
Redis如何实现高效插入大量数据
数据库·redis·缓存
7年前端辞职转AI10 小时前
Python 文件操作
python·编程语言