-
Notifications
You must be signed in to change notification settings - Fork 57
refactor(coze): 使用 node-cache 替代自定义缓存实现 #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
shenjingnan
commented
Dec 19, 2025
- 为什么改:原有的 CozeApiCache 自定义缓存实现功能有限,使用成熟的 node-cache 库可以提供更好的性能、稳定性和功能支持
- 改了什么:
- 移除了自定义的 CozeApiCache 类实现(apps/backend/lib/coze/cache.ts)
- 在 CozeApiService 中改用 node-cache 库替代自定义缓存
- 调整了缓存初始化配置,设置合理的默认 TTL(5分钟)和性能优化选项
- 更新了缓存操作的实现方式以适配 node-cache API
- 从 index.ts 中移除了 CozeApiCache 的导出
- 清理了 types/coze.ts 中不再需要的 CacheItem 接口定义
- 添加了 node-cache 依赖(v5.1.2)
- 影响范围:
- 仅影响 coze 模块内部实现,对外的 API 接口保持不变
- 缓存行为保持一致(工作空间30分钟、工作流5分钟过期)
- 性能有所提升(useClones: false 配置)
- 验证方式:
- 确保工作空间和工作流的缓存功能正常工作
- 验证缓存过期时间符合预期
- 测试缓存清理和统计功能
- 为什么改:原有的 CozeApiCache 自定义缓存实现功能有限,使用成熟的 node-cache 库可以提供更好的性能、稳定性和功能支持 - 改了什么: - 移除了自定义的 CozeApiCache 类实现(apps/backend/lib/coze/cache.ts) - 在 CozeApiService 中改用 node-cache 库替代自定义缓存 - 调整了缓存初始化配置,设置合理的默认 TTL(5分钟)和性能优化选项 - 更新了缓存操作的实现方式以适配 node-cache API - 从 index.ts 中移除了 CozeApiCache 的导出 - 清理了 types/coze.ts 中不再需要的 CacheItem 接口定义 - 添加了 node-cache 依赖(v5.1.2) - 影响范围: - 仅影响 coze 模块内部实现,对外的 API 接口保持不变 - 缓存行为保持一致(工作空间30分钟、工作流5分钟过期) - 性能有所提升(useClones: false 配置) - 验证方式: - 确保工作空间和工作流的缓存功能正常工作 - 验证缓存过期时间符合预期 - 测试缓存清理和统计功能
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
本 PR 将自定义的 CozeApiCache 实现替换为成熟的 node-cache 库,以提升缓存功能的稳定性和性能。
主要改动:
- 移除自定义的 CozeApiCache 类实现,改用 node-cache 库(v5.1.2)
- 更新 CozeApiService 中的缓存操作以适配 node-cache API
- 调整缓存过期时间配置方式(工作空间30分钟,工作流5分钟)
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pnpm-lock.yaml | 添加 [email protected] 及其依赖 [email protected] 的锁定信息 |
| package.json | 在依赖列表中添加 [email protected] |
| apps/backend/types/coze.ts | 移除不再使用的 CacheItem 接口定义 |
| apps/backend/lib/coze/service.ts | 重构 CozeApiService 以使用 node-cache,包括缓存初始化、设置、清理和统计方法 |
| apps/backend/lib/coze/index.ts | 从导出列表中移除 CozeApiCache |
| apps/backend/lib/coze/cache.ts | 完全删除自定义的 CozeApiCache 实现文件 |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
| clearCache(pattern?: string): void { | ||
| this.cache.clear(pattern); | ||
| if (!pattern) { | ||
| // 清除所有缓存 | ||
| this.cache.flushAll(); | ||
| return; | ||
| } | ||
|
|
||
| // node-cache 不支持模式匹配,需要手动实现 | ||
| // 使用前缀匹配,避免意外匹配 | ||
| const keys = this.cache.keys(); | ||
| const keysToDelete = keys.filter((key) => key.startsWith(pattern)); | ||
| this.cache.del(keysToDelete); |
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clearCache 方法中模式匹配行为发生了改变。原实现使用 key.includes(pattern) 进行包含匹配,而新实现使用 key.startsWith(pattern) 进行前缀匹配。这是一个行为变更:
原行为示例:
- 模式 "workflows" 可以匹配 "workflows:123" 和 "my_workflows:456"
新行为示例:
- 模式 "workflows" 只能匹配 "workflows:123",不能匹配 "my_workflows:456"
建议保持与原实现一致的行为,或者在 PR 描述中明确说明这一行为变更的原因和影响。如果要保持原行为,应该使用 key.includes(pattern)。
| getCacheStats(): { | ||
| size: number; | ||
| keys: string[]; | ||
| hits: number; | ||
| misses: number; | ||
| hitRate: number; | ||
| ksize: number; | ||
| vsize: number; | ||
| } { | ||
| const stats = this.cache.getStats(); | ||
| const keys = this.cache.keys(); | ||
| const totalRequests = stats.hits + stats.misses; | ||
| const hitRate = totalRequests > 0 ? stats.hits / totalRequests : 0; | ||
|
|
||
| return { | ||
| size: stats.keys, | ||
| keys, | ||
| hits: stats.hits, | ||
| misses: stats.misses, | ||
| hitRate, | ||
| ksize: stats.ksize, | ||
| vsize: stats.vsize, | ||
| }; | ||
| } |
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCacheStats 返回类型发生了 breaking change。新实现添加了多个新字段(hits, misses, hitRate, ksize, vsize),这可能会破坏现有的类型契约。虽然这些字段提供了更丰富的统计信息,但应该考虑:
- 确保所有调用此方法的地方都能正确处理新增字段
- 在 PR 描述中明确说明这是一个 API 扩展,不会影响现有代码(因为是添加字段而非移除)
建议验证所有使用 getCacheStats 的地方是否需要更新来使用新增的统计数据。