Skip to content

feat: 添加供应商缓存开关配置, 默认启用#556

Merged
ding113 merged 2 commits intoding113:devfrom
hank9999:feat/provider-cache-flag
Jan 7, 2026
Merged

feat: 添加供应商缓存开关配置, 默认启用#556
ding113 merged 2 commits intoding113:devfrom
hank9999:feat/provider-cache-flag

Conversation

@hank9999
Copy link
Collaborator

@hank9999 hank9999 commented Jan 7, 2026

Summary

添加 ENABLE_PROVIDER_CACHE 环境变量配置,允许用户按需禁用供应商进程级缓存。

Problem

PR #554 引入的供应商缓存功能在某些场景下可能需要禁用:

  • 调试场景:需要绕过缓存直接查询数据库验证数据
  • 单机低并发环境:缓存带来的性能提升有限,而增加了系统复杂度
  • 问题排查:怀疑缓存相关问题时需要快速关闭验证

Related:

Solution

getCachedProviders() 函数入口处添加配置检查,当 ENABLE_PROVIDER_CACHE=false 时直接调用 fetcher 绕过缓存逻辑。

设计决策

  • 默认启用:保持与 feat: 添加供应商查询缓存, 改善性能 #554 一致的行为,不影响现有部署
  • 运行时检查:每次调用时检查配置,无需重启即可生效(需重新部署或配置更新机制)
  • 日志追踪:禁用时输出 debug 日志,便于排查

Changes

Core Changes

  • src/lib/cache/provider-cache.ts (+7):
    • getCachedProviders() 开头检查 ENABLE_PROVIDER_CACHE 配置
    • 禁用时直接返回 fetcher() 结果,完全绕过缓存逻辑

Supporting Changes

  • src/lib/config/env.schema.ts (+4):

    • 新增 ENABLE_PROVIDER_CACHE 配置项,默认值 "true"
    • 使用 booleanTransform 转换字符串为布尔值
  • .env.example (+6):

    • 添加配置说明和使用场景文档

Testing

Automated Tests

  • bun run lint 通过
  • bun run typecheck 通过

Manual Testing

  1. 设置 ENABLE_PROVIDER_CACHE=false 并启动服务
  2. 发送代理请求,验证日志中出现 [ProviderCache] Cache disabled, fetching from DB
  3. 验证请求正常处理(每次都查询数据库)
  4. 设置 ENABLE_PROVIDER_CACHE=true 或删除该配置
  5. 验证缓存功能恢复正常

Checklist

  • Base branch set to dev
  • Code follows project conventions
  • Self-review completed
  • Tests pass locally

Description enhanced by Claude AI

@coderabbitai
Copy link

coderabbitai bot commented Jan 7, 2026

📝 Walkthrough

Walkthrough

在 .env.example、src/lib/config/env.schema.ts 和 src/lib/cache/provider-cache.ts 中添加了 ENABLE_PROVIDER_CACHE 配置;模块级读取该配置并在 getCachedProviders 中根据其值早期返回以绕过或启用进程级提供者缓存(30s TTL,Redis Pub/Sub 跨实例失效)。

Changes

内聚组 / 文件(s) 变更摘要
环境样例与说明
\.env\.example
新增 ENABLE_PROVIDER_CACHE 条目及注释,说明默认开启、30s TTL 及 Redis Pub/Sub 跨实例失效机制
运行时环境 schema
src/lib/config/env.schema.ts
EnvSchema 中新增 ENABLE_PROVIDER_CACHE: z.string().default("true").transform(booleanTransform),将该变量纳入配置校验与转换
缓存执行逻辑
src/lib/cache/provider-cache.ts
在模块顶层通过 getEnvConfig() 读取配置并设置 ENABLE_PROVIDER_CACHE 常量;getCachedProviders 增加早期分支:当配置为 false 时记录调试并直接返回 fetcher() 结果,绕过缓存逻辑

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题清晰准确地描述了PR的主要变更:添加供应商缓存的配置开关,默认启用。与文件改动内容完全相符。
Description check ✅ Passed 描述详细说明了问题背景、解决方案、具体代码改动、设计决策和测试步骤,与PR的所有改动内容直接相关。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom Pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

📥 Commits

Reviewing files that changed from the base of the PR and between 554da10 and 30af748.

📒 Files selected for processing (1)
  • src/lib/cache/provider-cache.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lib/cache/provider-cache.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: 🌐 API Tests
  • GitHub Check: Docker Build Test

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hank9999, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在通过引入一个可配置的开关来增强系统对供应商数据缓存的控制。这一改动允许开发者和运维人员根据具体需求(例如调试或生产环境的性能优化)灵活地启用或禁用供应商进程级缓存,从而在性能和数据实时性之间取得平衡。

Highlights

  • 新增供应商缓存开关: 引入了一个新的环境变量 ENABLE_PROVIDER_CACHE,用于控制供应商进程级缓存的启用与禁用。
  • 默认启用缓存: ENABLE_PROVIDER_CACHE 默认设置为 true,这意味着在不进行额外配置的情况下,供应商缓存将默认启用,以提升性能。
  • 缓存逻辑集成: 在 getCachedProviders 函数中增加了对 ENABLE_PROVIDER_CACHE 配置的检查,当缓存被禁用时,将直接从数据库获取数据。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

这个 PR 添加了一个供应商缓存的开关,这是一个很好的功能,可以方便地在需要时禁用缓存进行调试或应对突发问题。代码实现整体上是正确的,我在 provider-cache.ts 中提了一个关于性能优化的建议,通过在模块加载时读取一次配置来避免在热路径函数中重复调用配置获取函数。请查看具体评论。

@github-actions github-actions bot added enhancement New feature or request area:provider labels Jan 7, 2026
@greptile-apps
Copy link

greptile-apps bot commented Jan 7, 2026

Greptile's behavior is changing!

From now on, if a review finishes with no comments, we will not post an additional "statistics" comment to confirm that our review found nothing to comment on. However, you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

@github-actions github-actions bot added the size/XS Extra Small PR (< 50 lines) label Jan 7, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

No significant issues identified in this PR. The implementation is clean and follows established patterns in the codebase.

PR Size: XS

  • Lines changed: 17
  • Files changed: 3

Analysis Notes

The PR adds a new environment variable ENABLE_PROVIDER_CACHE that allows disabling the provider cache. The implementation:

  1. Schema addition (env.schema.ts:102-105): Correctly uses the existing booleanTransform pattern with a "true" default, consistent with other boolean env vars like ENABLE_RATE_LIMIT and AUTO_MIGRATE.

  2. Cache bypass logic (provider-cache.ts:93-97): Properly short-circuits to the fetcher when cache is disabled, with appropriate debug logging.

  3. Documentation (.env.example): Clear explanation of the feature and use cases.

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean (direct passthrough to fetcher, no new error paths)
  • Type safety - Clean (uses existing typed config)
  • Documentation accuracy - Clean
  • Test coverage - No tests exist for this module (pre-existing gap)
  • Code clarity - Good

Automated review by Claude AI

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/lib/cache/provider-cache.ts (1)

14-14: 实现简洁正确!

缓存开关实现评估:

  • 提前返回模式高效,避免不必要的订阅初始化和缓存查找
  • 日志级别(debug)适当,不会污染生产日志
  • warmupProviderCache 通过调用 getCachedProviders 自动继承此行为,设计合理
💡 可选优化:getProviderCacheStats 增加缓存启用状态字段

当前 getProviderCacheStats 在缓存禁用时仍返回 hasData: falsecount: 0,可能造成歧义。建议在返回类型中添加 enabled 字段以提升可观测性:

 export function getProviderCacheStats(): {
+  enabled: boolean;
   hasData: boolean;
   count: number;
   expiresIn: number;
   version: number;
   isRefreshing: boolean;
 } {
   const now = Date.now();
   return {
+    enabled: getEnvConfig().ENABLE_PROVIDER_CACHE,
     hasData: cache.data !== null,
     count: cache.data?.length ?? 0,
     expiresIn: Math.max(0, cache.expiresAt - now),
     version: cache.version,
     isRefreshing: cache.refreshPromise !== null,
   };
 }

这样在监控/调试时可以明确区分"缓存禁用"和"缓存已过期"两种状态。

Also applies to: 93-97

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

📥 Commits

Reviewing files that changed from the base of the PR and between 9e8f9fe and 554da10.

📒 Files selected for processing (3)
  • .env.example
  • src/lib/cache/provider-cache.ts
  • src/lib/config/env.schema.ts
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{ts,tsx,js,jsx,json}

📄 CodeRabbit inference engine (CLAUDE.md)

Use 2-space indentation in all code files

Files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx,js,jsx}: Use double quotes for strings instead of single quotes
Use trailing commas in multi-line structures
Enforce maximum line length of 100 characters
Use path alias @/* to reference files from ./src/* directory

**/*.{ts,tsx,js,jsx}: Use Biome for linting and formatting with 2-space indent, double quotes, trailing commas, and 100 character max line length
Use path alias @/* to reference files in ./src/* directory

Files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx}: Use TypeScript strict mode for type safety
Use readonly or const assertions for immutable data structures

Files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
src/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

src/**/*.ts: Hash API keys using SHA-256 before storing in database, never store plaintext keys
Mask API keys and sensitive data in application logs
Validate required environment variables at startup with clear error messages

Files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
src/lib/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

Use connection pooling for database and Redis connections

Files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
src/**/*provider*.ts

📄 CodeRabbit inference engine (AGENTS.md)

Set provider circuit breaker failure threshold, open duration, and half-open success threshold in configuration

Files:

  • src/lib/cache/provider-cache.ts
🧠 Learnings (3)
📚 Learning: 2026-01-03T09:08:49.020Z
Learnt from: CR
Repo: ding113/claude-code-hub PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-03T09:08:49.020Z
Learning: Use environment variables for all configuration; provide .env.example as reference

Applied to files:

  • .env.example
📚 Learning: 2026-01-03T09:08:49.019Z
Learnt from: CR
Repo: ding113/claude-code-hub PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-03T09:08:49.019Z
Learning: Applies to src/**/*provider*.ts : Set provider circuit breaker failure threshold, open duration, and half-open success threshold in configuration

Applied to files:

  • .env.example
📚 Learning: 2026-01-05T03:01:39.354Z
Learnt from: NieiR
Repo: ding113/claude-code-hub PR: 539
File: src/types/user.ts:158-170
Timestamp: 2026-01-05T03:01:39.354Z
Learning: In TypeScript interfaces, explicitly document and enforce distinct meanings for null and undefined. Example: for numeric limits like limitTotalUsd, use 'number | null | undefined' when null signifies explicitly unlimited (e.g., matches DB schema or special UI logic) and undefined signifies 'inherit default'. This pattern should be consistently reflected in type definitions across related fields to preserve semantic clarity between database constraints and UI behavior.

Applied to files:

  • src/lib/config/env.schema.ts
  • src/lib/cache/provider-cache.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: pr-review
  • GitHub Check: 🌐 API Tests
  • GitHub Check: Docker Build Test
  • GitHub Check: Greptile Review
  • GitHub Check: pr-description
🔇 Additional comments (2)
.env.example (1)

75-79: 文档清晰,配置合理!

环境变量文档编写规范:

  • 功能说明清晰,包含启用/禁用两种场景的具体行为
  • 默认值选择合理(true),符合生产环境性能优化需求
  • 适用场景说明到位(调试或单机低并发场景可禁用)

文档风格与现有配置保持一致。

src/lib/config/env.schema.ts (1)

102-105: Schema 定义规范,与现有模式保持一致!

验证要点:

  • ✓ 使用 booleanTransform 正确处理字符串 "false" 和 "0"(避免 z.coerce.boolean() 的已知陷阱)
  • ✓ 默认值 "true" 与 PR 目标一致
  • ✓ 注释风格与其他布尔标志(AUTO_MIGRATE、ENABLE_RATE_LIMIT 等)保持统一
  • ✓ 将自动包含在 EnvConfig 类型中,保证类型安全

实现符合项目编码规范。

@ding113 ding113 merged commit 9703331 into ding113:dev Jan 7, 2026
7 of 8 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Jan 7, 2026
@github-actions github-actions bot mentioned this pull request Jan 7, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider enhancement New feature or request size/XS Extra Small PR (< 50 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants