perf: replace redis KEYS with SCAN#6101
Conversation
lgphone
commented
Dec 16, 2025
- 使用 Redis SCAN 替代 KEYS,避免大数量扫描时阻塞 Redis
- SCAN 为官方推荐的非阻塞方案
- 不影响现有缓存语义
TestGru AssignmentSummary
Tip You can |
Preview sandbox Image: |
Preview mcp_server Image: |
Preview fastgpt Image: |
There was a problem hiding this comment.
Pull request overview
This PR replaces blocking Redis KEYS operations with non-blocking SCAN commands to prevent Redis from being blocked during large-scale key scans. The changes implement the officially recommended pattern for iterating over keys in production environments while maintaining existing cache semantics.
Key Changes
- Replaced
KEYScommand withSCANingetAllKeysByPrefixfunction to iterate through keys non-blockingly - Updated
refreshVersionKeyto useSCANwith pipelined deletions for efficient batch processing - Added mock support for
scanandpipelineoperations in test utilities
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/service/common/redis/index.ts | Refactored getAllKeysByPrefix to use SCAN with cursor-based iteration instead of blocking KEYS |
| packages/service/common/cache/index.ts | Updated refreshVersionKey to use SCAN for pattern matching and pipelined deletions for batch operations |
| test/mocks/common/redis.ts | Added mock implementations for scan and pipeline operations to support new Redis patterns |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const pattern = `${prefix}${key}:*`; | ||
|
|
||
| let cursor = '0'; | ||
| const batchSize = 1000; // SCAN 每次取多少 |
There was a problem hiding this comment.
The magic number 1000 for batch size should be extracted as a named constant at the module level. This would allow for consistent configuration across the codebase, as the same value is used in the cache module.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* perf: replace redis KEYS with SCAN (#6101) * perf: replace redis KEYS with SCAN * test: add redis scan mock to fix unit tests * Fix formatting in redis.ts mock functions * fix comment word Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * perf: get redis keys function * replace prefix code * add pipeline delete keys --------- Co-authored-by: lgphone <inboxcvt@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

