Skip to content

perf: replace redis KEYS with SCAN#6101

Merged
c121914yu merged 5 commits intolabring:test-redisfrom
lgphone:main
Dec 18, 2025
Merged

perf: replace redis KEYS with SCAN#6101
c121914yu merged 5 commits intolabring:test-redisfrom
lgphone:main

Conversation

@lgphone
Copy link
Copy Markdown
Contributor

@lgphone lgphone commented Dec 16, 2025

  • 使用 Redis SCAN 替代 KEYS,避免大数量扫描时阻塞 Redis
  • SCAN 为官方推荐的非阻塞方案
  • 不影响现有缓存语义

@gru-agent
Copy link
Copy Markdown
Contributor

gru-agent bot commented Dec 16, 2025

TestGru Assignment

Summary

Link CommitId Status Reason
Detail ce88915 🚫 Skipped No files need to be tested {"packages/service/common/cache/index.ts":"File path does not match include patterns.","packages/service/common/redis/index.ts":"File path does not match include patterns."}

History Assignment

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 16, 2025

Preview sandbox Image:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:fatsgpt_sandbox_f94cdc8b36619543cfd4e3fc664cc092914b50ff

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 16, 2025

Preview mcp_server Image:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:fatsgpt_mcp_server_f94cdc8b36619543cfd4e3fc664cc092914b50ff

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 16, 2025

Preview fastgpt Image:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:fatsgpt_f94cdc8b36619543cfd4e3fc664cc092914b50ff

@c121914yu c121914yu requested a review from Copilot December 16, 2025 15:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 KEYS command with SCAN in getAllKeysByPrefix function to iterate through keys non-blockingly
  • Updated refreshVersionKey to use SCAN with pipelined deletions for efficient batch processing
  • Added mock support for scan and pipeline operations 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.

Comment thread test/mocks/common/redis.ts Outdated
const pattern = `${prefix}${key}:*`;

let cursor = '0';
const batchSize = 1000; // SCAN 每次取多少
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread packages/service/common/cache/index.ts
lgphone and others added 2 commits December 17, 2025 10:12
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@c121914yu c121914yu changed the base branch from main to test-redis December 18, 2025 02:35
@c121914yu
Copy link
Copy Markdown
Collaborator

image 发现无法在 pr 上进行修改,我先合到测试分支修改后再合并到 main。

主要调整下 refreshVersionKey 方法,直接复用 getAllKeysByprefix 方法
image

@c121914yu c121914yu merged commit 342aca0 into labring:test-redis Dec 18, 2025
5 checks passed
c121914yu added a commit that referenced this pull request Dec 18, 2025
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants