11# Changelog v1.1.5
22
33> ** 发布日期** : 2026-02-10
4- > ** 版本类型** : Bug 修复 + 测试增强 + 代码优化 (Bug Fix + Test Enhancement + Code Optimization)
5- > ** 重要性** : ⭐⭐⭐⭐⭐
4+ > ** 版本类型** : 🚨 重要 Bug 修复 + 测试增强 + 代码优化 (Critical Bug Fix + Test Enhancement + Code Optimization)
5+ > ** 重要性** : ⭐⭐⭐⭐⭐ (强烈推荐升级)
66
77---
88
9- ## 🔧 Bug 修复与代码优化
9+ ## 🚨 重要 Bug 修复 (CRITICAL)
10+
11+ ### ⚠️ 修复 upsert 场景的缓存自动失效问题
12+
13+ ** 问题描述** :
14+ - ` updateOne({ upsert: true }) ` 、` updateMany({ upsert: true }) ` 、` replaceOne({ upsert: true }) ` 在** 插入新文档** 时不会失效缓存
15+ - 导致后续查询返回过时数据(缓存中是空数组,但数据库中已有新数据)
16+ - ** 影响** : 数据一致性问题,用户看不到新创建的数据(直到缓存过期)
17+
18+ ** 根本原因** :
19+ - 缓存失效条件判断不完整:仅检查 ` modifiedCount > 0 ` 或 ` matchedCount > 0 `
20+ - upsert 插入新文档时 ` modifiedCount = 0 ` (因为是插入不是修改),但 ` upsertedId ` 存在
21+
22+ ** 修复内容** :
23+ ``` diff
24+ // updateOne.js (第 146 行)
25+ - if (cache && result.modifiedCount > 0) {
26+ + if (cache && (result.modifiedCount > 0 || result.upsertedId)) {
27+
28+ // updateMany.js (第 146 行)
29+ - if (cache && result.matchedCount > 0) {
30+ + if (cache && (result.matchedCount > 0 || result.upsertedId)) {
31+
32+ // replaceOne.js (第 113 行)
33+ - if (cache && result.modifiedCount > 0) {
34+ + if (cache && (result.modifiedCount > 0 || result.upsertedId)) {
35+ ```
36+
37+ ** 影响场景** :
38+ - ✅ 用户注册:新用户注册后立即显示在列表中
39+ - ✅ 订单创建:新订单创建后立即显示
40+ - ✅ 配置管理:upsert 创建配置时缓存立即刷新
41+ - ✅ 统计查询:count/distinct 返回正确的数量
42+ - ✅ 实时数据:仪表盘数据实时更新
43+
44+ ** 测试验证** :
45+ - 新增 11 个测试用例,100% 通过
46+ - 覆盖 updateOne/updateMany/replaceOne 的所有 upsert 场景
47+ - 包含边界情况和缓存统计验证
48+
49+ ** 修复的方法** :
50+ 1 . ✅ ** updateOne** - 修复缓存失效条件
51+ 2 . ✅ ** updateMany** - 修复缓存失效条件
52+ 3 . ✅ ** replaceOne** - 修复缓存失效条件(新发现)
53+
54+ ** 验证通过的方法** :
55+ - ✅ findOneAndUpdate - 使用 ` wasDocumentModified() ` ✓
56+ - ✅ findOneAndReplace - 使用 ` wasDocumentModified() ` ✓
57+ - ✅ upsertOne - 正确判断 ` modifiedCount || upsertedCount ` ✓
58+
59+ ** 性能影响** : 可忽略 (<1%),正确性优先
60+
61+ ** 详细报告** :
62+ - [ 三轮验证报告] ( ../reports/upsert-缓存失效-三轮验证报告.md )
63+ - [ 完整修复总结] ( ../reports/upsert-缓存失效-完整修复总结.md )
64+
65+ ---
66+
67+ ## 🔧 其他 Bug 修复与代码优化
1068
1169### 1. 修复 ` register ` 方法异步调用问题
1270
@@ -101,10 +159,35 @@ console.warn('[FunctionCache] Cache get/set failed:', {...});
101159
102160### 代码变更
103161
104- | 文件 | 新增 | 删除 | 净变化 |
105- | ------| ------| ------| --------|
106- | ` lib/function-cache.js ` | +35 行 | -48 行 | ** -13 行** |
107- | ` test/unit/function-cache.test.js ` | +386 行 | - | +386 行 |
162+ | 文件 | 新增 | 删除 | 净变化 | 说明 |
163+ | ------| ------| ------| --------| ------|
164+ | ** 核心修复 (upsert 缓存失效)** ||||
165+ | ` lib/mongodb/writes/update-one.js ` | +7 行 | -3 行 | +4 行 | 修复 upsert 缓存失效 |
166+ | ` lib/mongodb/writes/update-many.js ` | +7 行 | -3 行 | +4 行 | 修复 upsert 缓存失效 |
167+ | ` lib/mongodb/writes/replace-one.js ` | +7 行 | -3 行 | +4 行 | 修复 upsert 缓存失效 |
168+ | ` test/unit/writes/update-cache-invalidation.test.js ` | +477 行 | - | +477 行 | 新增 11 个测试 |
169+ | ** 其他优化** ||||
170+ | ` lib/function-cache.js ` | +35 行 | -48 行 | ** -13 行** | 代码优化 |
171+ | ` test/unit/function-cache.test.js ` | +386 行 | - | +386 行 | 测试增强 |
172+ | ** 总计** | +919 行 | -57 行 | ** +862 行** ||
173+
174+ ### Bug 修复统计
175+
176+ | 类别 | 数量 | 优先级 | 状态 |
177+ | ------| ------| --------| ------|
178+ | ** 数据一致性 Bug** | 3 个 | 🔴 P0 (Critical) | ✅ 已修复 |
179+ | ** 性能问题** | 1 个 | 🟡 P1 | ✅ 已优化 |
180+ | ** 代码质量问题** | 4 个 | 🟢 P2 | ✅ 已改进 |
181+ | ** 总计** | ** 8 个** || ✅ 100% 完成 |
182+
183+ ### 测试覆盖提升
184+
185+ | 维度 | 优化前 | 优化后 | 提升 |
186+ | ------| --------| --------| ------|
187+ | ** upsert 测试** | 0 | ** 11** | +∞ |
188+ | ** function-cache 测试** | 52 | ** 63** | +21% |
189+ | ** 总测试数量** | 1000+ | ** 1074+** | +7.4% |
190+ | ** 测试通过率** | 100% | ** 100%** | ✓ |
108191
109192### 性能提升
110193
@@ -129,38 +212,103 @@ console.warn('[FunctionCache] Cache get/set failed:', {...});
129212
130213## 📁 文件变更
131214
132- ### 修改的文件
215+ ### 修改的核心文件
133216
217+ ** upsert 缓存失效修复** :
218+ ```
219+ lib/mongodb/writes/update-one.js (修改, +7/-3 行)
220+ - 修复缓存失效条件:检查 modifiedCount || upsertedId
221+ - 添加 upsert 标记到日志和错误信息
222+ - 增强事务元数据记录
223+
224+ lib/mongodb/writes/update-many.js (修改, +7/-3 行)
225+ - 修复缓存失效条件:检查 matchedCount || upsertedId
226+ - 添加 upsert 标记到日志和错误信息
227+ - 增强事务元数据记录
228+
229+ lib/mongodb/writes/replace-one.js (修改, +7/-3 行)
230+ - 修复缓存失效条件:检查 modifiedCount || upsertedId
231+ - 添加 upsert 标记到日志和错误信息
232+ - 增强事务元数据记录
233+
234+ test/unit/writes/update-cache-invalidation.test.js (新建, 477 行)
235+ - 11 个测试用例覆盖所有 upsert 场景
236+ - updateOne/updateMany/replaceOne 完整测试
237+ - 缓存统计验证和边界情况测试
134238```
135- lib/function-cache.js (修改, +35/-48 行)
239+
240+ ** 其他代码优化** :
241+ ```
242+ lib/function-cache.js (修改, +35/-48 行)
136243 - crypto 模块移到顶部
137244 - 移除 _registerDependencies 方法
138245 - 添加全局 Map 监控
139246 - 添加错误日志记录
140247 - 添加统计信息说明
141248
142- test/unit/function-cache.test.js (修改, +386 行)
249+ test/unit/function-cache.test.js (修改, +386 行)
143250 - 修复参数验证测试(改为异步)
144251 - 新增 17 个复杂数据类型测试
145252```
146253
147- ### 新增的文件
254+ ### 新增的报告文件
148255
149256```
150- reports/code-analysis-deep-dive.md (新建, 520 行)
151- reports/issues-verification-report.md (新建, 524 行)
152- reports/v1.1.5-release-summary.md (新建, 290 行)
153- reports/v1.1.6-fixes-completion.md (新建, 390 行)
154- reports/v1.1.6-execution-summary.md (新建, 320 行)
155- test/verification/issues-verification.test.js (新建, 325 行)
156- test/performance/function-cache-performance.test.js (新建, 300 行)
257+ reports/upsert-缓存失效机制分析报告.md (新建, 1377 行)
258+ - 问题深度分析报告
259+ - 根本原因剖析
260+ - 影响范围评估
261+
262+ reports/upsert-缓存失效-修复报告.md (新建, 820 行)
263+ - 修复执行详细报告
264+ - 代码对比和验证
265+
266+ reports/upsert-缓存失效-三轮验证报告.md (新建, 430 行)
267+ - 三轮验证完整记录
268+ - 测试结果和验证清单
269+
270+ reports/upsert-缓存失效-完整修复总结.md (新建, 350 行)
271+ - 修复总结和经验教训
272+ - 后续行动计划
273+
274+ reports/monSQLize-深度分析报告.md (新建, 1200 行)
275+ - 项目完整架构分析
276+ - 功能和性能评估
157277```
158278
159279---
160280
161281## ✅ 测试验证
162282
163- ### 单元测试
283+ ### upsert 缓存失效测试
284+
285+ ``` bash
286+ $ mocha test/unit/writes/update-cache-invalidation.test.js --timeout 30000
287+
288+ Update Operations - Upsert Cache Invalidation
289+ updateOne({ upsert: true })
290+ ✓ should invalidate cache when upsert inserts a new document
291+ ✓ should invalidate cache when upsert updates existing document
292+ ✓ should invalidate count cache when upsert inserts
293+ ✓ should invalidate find + findOne cache when upsert inserts
294+ updateMany({ upsert: true })
295+ ✓ should invalidate cache when upsert inserts a new document
296+ ✓ should invalidate cache when updateMany updates existing documents
297+ Cache Statistics Verification
298+ ✓ should track cache hits and misses correctly with upsert
299+ Edge Cases
300+ ✓ should handle upsert with same content (no modification)
301+ ✓ should handle multiple upserts in sequence
302+ replaceOne({ upsert: true })
303+ ✓ should invalidate cache when replaceOne upsert inserts a new document
304+ ✓ should invalidate cache when replaceOne upsert updates existing document
305+
306+ 11 passing (454ms)
307+ ```
308+
309+ ** 结果** : ✅ ** 100% 通过** (11/11)
310+
311+ ### function-cache 单元测试
164312
165313``` bash
166314$ mocha test/unit/function-cache.test.js --timeout 10000
0 commit comments