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.")
相关推荐
kyle~9 小时前
Python---Flask 轻量级Web框架
开发语言·python·flask
xinhuanjieyi10 小时前
python获取股票分红
chrome·python·elasticsearch
2401_8480097210 小时前
Redis零基础入门学习
数据库·redis·学习
Tangcan-10 小时前
在Ubuntu 22.04上安装redis
linux·redis·ubuntu
喵手10 小时前
Python爬虫实战:小红书热门笔记爬虫实战 - 搜索关键词驱动的内容采集指南!
爬虫·python·爬虫实战·零基础python爬虫教学·小红书热门笔记·搜索关键词·采集小红书热门笔记数据
nita张11 小时前
战略定位实战:案例分享与经验总结
大数据·人工智能·python
MadPrinter11 小时前
Python 异步爬虫实战:FindQC 商品数据爬取系统完整教程
爬虫·python·算法·自动化
清水白石00811 小时前
Python 函数式编程实战:从零构建函数组合系统
开发语言·python
喵手12 小时前
Python爬虫实战:数据质量治理实战 - 构建企业级规则引擎与异常检测系统!
爬虫·python·爬虫实战·异常检测·零基础python爬虫教学·数据质量治理·企业级规则引擎
头发够用的程序员12 小时前
Python 魔法方法 vs C++ 运算符重载全方位深度对比
开发语言·c++·python