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.")
相关推荐
Csvn16 小时前
🌟 LangChain 30 天保姆级教程 · Day 13|OutputParser 进阶!让 AI 输出自动转为结构化对象,并支持自动重试!
python·langchain
cch891817 小时前
Python主流框架全解析
开发语言·python
M--Y17 小时前
Redis常用数据类型
数据结构·数据库·redis
sg_knight17 小时前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
好运的阿财17 小时前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
张張40817 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_4235339917 小时前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
Ricky111zzz18 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
小白学大数据18 小时前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
mameng199818 小时前
Redis遇到热点key如何解决
数据库·redis·缓存