SCAN 命令是 Redis 中用于安全迭代 key 空间的推荐方式,相比 KEYS 命令,它不会阻塞服务器且更适合生产环境使用。
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
cursor
: 游标值,初始为 0,迭代完成时返回 0MATCH pattern
: 可选,匹配 key 的模式COUNT count
: 可选,建议返回的元素数量(实际可能不同)TYPE type
: Redis 6.0+ 可选,按数据类型过滤127.0.0.1:6379> SCAN 0
1) "17" # 下一次迭代的游标
2) 1) "key:1" # 返回的 key 列表
2) "key:2"
3) "key:3"
import redis
r = redis.Redis()
def scan_all_keys():
cursor = 0
all_keys = []
while True:
cursor, keys = r.scan(cursor=cursor, count=1000)
all_keys.extend(keys)
if cursor == 0: # 迭代完成
break
return all_keys
keys = scan_all_keys()
print(f"Total keys: {len(keys)}")
# 查找所有以 "user:" 开头的 key
127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 100
# 只返回 string 类型的 key
127.0.0.1:6379> SCAN 0 TYPE string
对于集群环境,需要使用 SCAN
的集群版本:
def cluster_scan_all_keys(redis_cluster):
all_keys = []
for node in redis_cluster.nodes.values():
cursor = 0
while True:
cursor, keys = node.execute_command('SCAN', cursor, 'COUNT', 1000)
all_keys.extend(keys)
if cursor == 0:
break
return all_keys
SCAN 是处理大量 key 时的最佳实践,避免了 KEYS 命令可能导致的性能问题。