monsqlize 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -60
- package/LICENSE +201 -21
- package/README.md +537 -1828
- package/changelogs/README.md +160 -0
- package/changelogs/v2.0.0.md +222 -0
- package/dist/cjs/index.cjs +10600 -0
- package/dist/cjs/mongodb/common/transaction-aware.cjs +10 -0
- package/dist/cjs/transaction/CacheLockManager.cjs +100 -0
- package/dist/cjs/transaction/Transaction.cjs +158 -0
- package/dist/cjs/transaction/TransactionManager.cjs +298 -0
- package/dist/esm/index.mjs +10650 -0
- package/dist/types/base.d.ts +81 -0
- package/dist/types/collection.d.ts +1031 -0
- package/dist/types/expression.d.ts +115 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/lock.d.ts +74 -0
- package/dist/types/model.d.ts +526 -0
- package/dist/types/mongodb.d.ts +49 -0
- package/dist/types/monsqlize.d.ts +491 -0
- package/dist/types/pool.d.ts +84 -0
- package/dist/types/runtime.d.ts +362 -0
- package/dist/types/saga.d.ts +143 -0
- package/dist/types/slow-query-log.d.ts +126 -0
- package/dist/types/sync.d.ts +103 -0
- package/dist/types/transaction.d.ts +132 -0
- package/package.json +67 -69
- package/index.d.ts +0 -206
- package/index.mjs +0 -52
- package/lib/cache-invalidation.js +0 -279
- package/lib/cache.js +0 -530
- package/lib/common/cursor.js +0 -59
- package/lib/common/docs-urls.js +0 -73
- package/lib/common/index-options.js +0 -223
- package/lib/common/log.js +0 -61
- package/lib/common/namespace.js +0 -22
- package/lib/common/normalize.js +0 -34
- package/lib/common/page-result.js +0 -43
- package/lib/common/runner.js +0 -57
- package/lib/common/server-features.js +0 -232
- package/lib/common/shape-builders.js +0 -27
- package/lib/common/validation.js +0 -113
- package/lib/connect.js +0 -99
- package/lib/constants.js +0 -55
- package/lib/count-queue.js +0 -188
- package/lib/distributed-cache-invalidator.js +0 -260
- package/lib/errors.js +0 -168
- package/lib/expression/cache/ExpressionCache.js +0 -114
- package/lib/expression/compiler/ExpressionCompiler.js +0 -1090
- package/lib/expression/compiler/ExpressionCompilerExtensions.js +0 -531
- package/lib/expression/detector.js +0 -84
- package/lib/expression/factory.js +0 -29
- package/lib/expression/index.js +0 -19
- package/lib/function-cache.js +0 -533
- package/lib/index.js +0 -1251
- package/lib/infrastructure/ConnectionPoolManager.js +0 -464
- package/lib/infrastructure/HealthChecker.js +0 -281
- package/lib/infrastructure/PoolConfig.js +0 -199
- package/lib/infrastructure/PoolSelector.js +0 -225
- package/lib/infrastructure/PoolStats.js +0 -244
- package/lib/infrastructure/ssh-tunnel-ssh2.js +0 -212
- package/lib/infrastructure/ssh-tunnel.js +0 -41
- package/lib/infrastructure/uri-parser.js +0 -36
- package/lib/lock/Lock.js +0 -67
- package/lib/lock/errors.js +0 -28
- package/lib/lock/index.js +0 -13
- package/lib/logger.js +0 -225
- package/lib/model/examples/test.js +0 -311
- package/lib/model/features/defaults.js +0 -161
- package/lib/model/features/populate.js +0 -568
- package/lib/model/features/relations.js +0 -120
- package/lib/model/features/soft-delete.js +0 -349
- package/lib/model/features/version.js +0 -157
- package/lib/model/features/virtuals.js +0 -219
- package/lib/model/index.js +0 -1265
- package/lib/mongodb/common/accessor-helpers.js +0 -59
- package/lib/mongodb/common/agg-pipeline.js +0 -36
- package/lib/mongodb/common/aggregation-validator.js +0 -127
- package/lib/mongodb/common/iid.js +0 -28
- package/lib/mongodb/common/lexicographic-expr.js +0 -53
- package/lib/mongodb/common/shape.js +0 -32
- package/lib/mongodb/common/sort.js +0 -39
- package/lib/mongodb/common/transaction-aware.js +0 -25
- package/lib/mongodb/connect.js +0 -234
- package/lib/mongodb/index.js +0 -639
- package/lib/mongodb/management/admin-ops.js +0 -200
- package/lib/mongodb/management/bookmark-ops.js +0 -167
- package/lib/mongodb/management/cache-ops.js +0 -50
- package/lib/mongodb/management/collection-ops.js +0 -387
- package/lib/mongodb/management/database-ops.js +0 -202
- package/lib/mongodb/management/index-ops.js +0 -475
- package/lib/mongodb/management/index.js +0 -17
- package/lib/mongodb/management/namespace.js +0 -31
- package/lib/mongodb/management/validation-ops.js +0 -268
- package/lib/mongodb/queries/aggregate.js +0 -172
- package/lib/mongodb/queries/chain.js +0 -631
- package/lib/mongodb/queries/count.js +0 -99
- package/lib/mongodb/queries/distinct.js +0 -78
- package/lib/mongodb/queries/find-and-count.js +0 -193
- package/lib/mongodb/queries/find-by-ids.js +0 -236
- package/lib/mongodb/queries/find-one-by-id.js +0 -171
- package/lib/mongodb/queries/find-one.js +0 -71
- package/lib/mongodb/queries/find-page.js +0 -618
- package/lib/mongodb/queries/find.js +0 -171
- package/lib/mongodb/queries/index.js +0 -51
- package/lib/mongodb/queries/watch.js +0 -538
- package/lib/mongodb/writes/common/batch-retry.js +0 -65
- package/lib/mongodb/writes/delete-batch.js +0 -323
- package/lib/mongodb/writes/delete-many.js +0 -181
- package/lib/mongodb/writes/delete-one.js +0 -173
- package/lib/mongodb/writes/find-one-and-delete.js +0 -203
- package/lib/mongodb/writes/find-one-and-replace.js +0 -239
- package/lib/mongodb/writes/find-one-and-update.js +0 -240
- package/lib/mongodb/writes/increment-one.js +0 -259
- package/lib/mongodb/writes/index.js +0 -46
- package/lib/mongodb/writes/insert-batch.js +0 -508
- package/lib/mongodb/writes/insert-many.js +0 -223
- package/lib/mongodb/writes/insert-one.js +0 -169
- package/lib/mongodb/writes/replace-one.js +0 -226
- package/lib/mongodb/writes/result-handler.js +0 -237
- package/lib/mongodb/writes/update-batch.js +0 -416
- package/lib/mongodb/writes/update-many.js +0 -275
- package/lib/mongodb/writes/update-one.js +0 -273
- package/lib/mongodb/writes/upsert-one.js +0 -203
- package/lib/multi-level-cache.js +0 -244
- package/lib/operators.js +0 -330
- package/lib/redis-cache-adapter.js +0 -267
- package/lib/saga/SagaContext.js +0 -67
- package/lib/saga/SagaDefinition.js +0 -32
- package/lib/saga/SagaExecutor.js +0 -201
- package/lib/saga/SagaOrchestrator.js +0 -186
- package/lib/saga/index.js +0 -11
- package/lib/slow-query-log/base-storage.js +0 -70
- package/lib/slow-query-log/batch-queue.js +0 -97
- package/lib/slow-query-log/config-manager.js +0 -196
- package/lib/slow-query-log/index.js +0 -238
- package/lib/slow-query-log/mongodb-storage.js +0 -324
- package/lib/slow-query-log/query-hash.js +0 -39
- package/lib/sync/ChangeStreamSyncManager.js +0 -405
- package/lib/sync/ResumeTokenStore.js +0 -192
- package/lib/sync/SyncConfig.js +0 -127
- package/lib/sync/SyncTarget.js +0 -240
- package/lib/sync/index.js +0 -20
- package/lib/transaction/CacheLockManager.js +0 -162
- package/lib/transaction/DistributedCacheLockManager.js +0 -475
- package/lib/transaction/Transaction.js +0 -315
- package/lib/transaction/TransactionManager.js +0 -267
- package/lib/transaction/index.js +0 -11
- package/lib/utils/objectid-converter.js +0 -632
- package/types/README.md +0 -122
- package/types/base.ts +0 -94
- package/types/batch.ts +0 -187
- package/types/cache.ts +0 -71
- package/types/chain.ts +0 -254
- package/types/collection.ts +0 -357
- package/types/expression.ts +0 -109
- package/types/function-cache.d.ts +0 -135
- package/types/lock.ts +0 -95
- package/types/model/definition.ts +0 -152
- package/types/model/index.ts +0 -10
- package/types/model/instance.ts +0 -121
- package/types/model/relations.ts +0 -121
- package/types/model/virtuals.ts +0 -32
- package/types/monsqlize.ts +0 -245
- package/types/options.ts +0 -192
- package/types/pagination.ts +0 -154
- package/types/pool.ts +0 -125
- package/types/query.ts +0 -71
- package/types/saga.ts +0 -125
- package/types/stream.ts +0 -64
- package/types/sync.ts +0 -79
- package/types/transaction.ts +0 -79
- package/types/write.ts +0 -77
package/index.mjs
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* monSQLize ES Module Entry Point
|
|
3
|
-
*
|
|
4
|
-
* 使用方式:
|
|
5
|
-
* ```javascript
|
|
6
|
-
* import MonSQLize from 'monsqlize';
|
|
7
|
-
* // 或
|
|
8
|
-
* import { MonSQLize } from 'monsqlize';
|
|
9
|
-
*
|
|
10
|
-
* const db = new MonSQLize({
|
|
11
|
-
* type: 'mongodb',
|
|
12
|
-
* config: { uri: 'mongodb://localhost:27017/mydb' }
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* await db.connect();
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { createRequire } from 'module';
|
|
20
|
-
const require = createRequire(import.meta.url);
|
|
21
|
-
|
|
22
|
-
// 使用 require 导入 CommonJS 模块
|
|
23
|
-
const MonSQLizeClass = require('./lib/index.js');
|
|
24
|
-
const Logger = require('./lib/logger.js');
|
|
25
|
-
const MemoryCache = require('./lib/cache.js');
|
|
26
|
-
const { createRedisCacheAdapter } = require('./lib/redis-cache-adapter.js');
|
|
27
|
-
const TransactionManager = require('./lib/transaction/TransactionManager.js');
|
|
28
|
-
const CacheLockManager = require('./lib/transaction/CacheLockManager.js');
|
|
29
|
-
const DistributedCacheInvalidator = require('./lib/distributed-cache-invalidator.js');
|
|
30
|
-
const { withCache, FunctionCache } = require('./lib/function-cache.js');
|
|
31
|
-
|
|
32
|
-
// 默认导出
|
|
33
|
-
export default MonSQLizeClass;
|
|
34
|
-
|
|
35
|
-
// 🆕 v1.0.9: 导出表达式函数
|
|
36
|
-
const { expr, createExpression } = MonSQLizeClass;
|
|
37
|
-
|
|
38
|
-
// 命名导出
|
|
39
|
-
export {
|
|
40
|
-
MonSQLizeClass as MonSQLize,
|
|
41
|
-
Logger,
|
|
42
|
-
MemoryCache,
|
|
43
|
-
createRedisCacheAdapter,
|
|
44
|
-
TransactionManager,
|
|
45
|
-
CacheLockManager,
|
|
46
|
-
DistributedCacheInvalidator,
|
|
47
|
-
expr, // 🆕 v1.0.9: 统一表达式函数
|
|
48
|
-
createExpression, // 🆕 v1.0.9: 表达式函数别名
|
|
49
|
-
withCache, // 🆕 v1.1.4: 函数缓存装饰器
|
|
50
|
-
FunctionCache // 🆕 v1.1.4: 函数缓存类
|
|
51
|
-
};
|
|
52
|
-
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 缓存精准失效引擎
|
|
3
|
-
* @description 提供精准缓存失效功能,只清除受影响的查询缓存
|
|
4
|
-
* @module lib/cache-invalidation
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const CacheFactory = require('./cache');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 缓存精准失效引擎
|
|
11
|
-
* @class
|
|
12
|
-
*/
|
|
13
|
-
class CacheInvalidationEngine {
|
|
14
|
-
/**
|
|
15
|
-
* 判断文档字段值是否匹配查询条件
|
|
16
|
-
* @param {any} docValue - 文档字段值
|
|
17
|
-
* @param {any} queryValue - 查询条件值
|
|
18
|
-
* @returns {boolean} true=匹配, false=不匹配
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* matchesField('active', 'active') // → true
|
|
22
|
-
* matchesField(25, { $gt: 20 }) // → true
|
|
23
|
-
* matchesField('test', { $in: ['test', 'demo'] }) // → true
|
|
24
|
-
*/
|
|
25
|
-
static matchesField(docValue, queryValue) {
|
|
26
|
-
// 1. 简单等值匹配
|
|
27
|
-
if (typeof queryValue !== 'object' || queryValue === null) {
|
|
28
|
-
return docValue === queryValue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 2. $in 操作符
|
|
32
|
-
if (queryValue.$in && Array.isArray(queryValue.$in)) {
|
|
33
|
-
return queryValue.$in.includes(docValue);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// 3. $gt/$gte/$lt/$lte 操作符
|
|
37
|
-
if (queryValue.$gt !== undefined) {
|
|
38
|
-
return docValue > queryValue.$gt;
|
|
39
|
-
}
|
|
40
|
-
if (queryValue.$gte !== undefined) {
|
|
41
|
-
return docValue >= queryValue.$gte;
|
|
42
|
-
}
|
|
43
|
-
if (queryValue.$lt !== undefined) {
|
|
44
|
-
return docValue < queryValue.$lt;
|
|
45
|
-
}
|
|
46
|
-
if (queryValue.$lte !== undefined) {
|
|
47
|
-
return docValue <= queryValue.$lte;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 4. $ne 操作符
|
|
51
|
-
if (queryValue.$ne !== undefined) {
|
|
52
|
-
return docValue !== queryValue.$ne;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 5. $exists 操作符
|
|
56
|
-
if (queryValue.$exists !== undefined) {
|
|
57
|
-
const exists = docValue !== undefined;
|
|
58
|
-
return queryValue.$exists ? exists : !exists;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 6. $eq 操作符(显式等值)
|
|
62
|
-
if (queryValue.$eq !== undefined) {
|
|
63
|
-
return docValue === queryValue.$eq;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 7. 不支持的操作符 → 返回 false(跳过匹配)
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 判断文档是否匹配查询条件
|
|
72
|
-
* @param {Object} doc - 写入的文档
|
|
73
|
-
* @param {Object} query - 查询条件
|
|
74
|
-
* @returns {boolean} true=匹配(需要失效), false=不匹配
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* matchesQuery({ status: 'active' }, { status: 'active' }) // → true
|
|
78
|
-
* matchesQuery({ status: 'inactive' }, { status: 'active' }) // → false
|
|
79
|
-
* matchesQuery({ age: 25 }, { age: { $gt: 20 } }) // → true
|
|
80
|
-
*/
|
|
81
|
-
static matchesQuery(doc, query) {
|
|
82
|
-
// 1. 空查询或 null/undefined → 匹配所有
|
|
83
|
-
if (!query || Object.keys(query).length === 0) {
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 2. 逐字段检查(AND 逻辑)
|
|
88
|
-
for (const [field, value] of Object.entries(query)) {
|
|
89
|
-
if (!this.matchesField(doc[field], value)) {
|
|
90
|
-
return false; // 任何字段不匹配 → 不失效
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 3. 所有字段都匹配 → 失效
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 检测查询是否包含复杂操作符
|
|
100
|
-
* @param {Object} query - 查询条件
|
|
101
|
-
* @returns {boolean} true=包含复杂操作符
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* hasComplexOperators({ $or: [...] }) // → true
|
|
105
|
-
* hasComplexOperators({ status: 'active' }) // → false
|
|
106
|
-
*/
|
|
107
|
-
static hasComplexOperators(query) {
|
|
108
|
-
if (!query || typeof query !== 'object') {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 复杂操作符列表(不支持精准失效)
|
|
113
|
-
const complexOps = [
|
|
114
|
-
'$or', // 逻辑或
|
|
115
|
-
'$nor', // 逻辑非或
|
|
116
|
-
'$and', // 逻辑与(保守跳过,可选支持)
|
|
117
|
-
'$expr', // 表达式求值
|
|
118
|
-
'$where', // JavaScript 函数
|
|
119
|
-
'$text', // 全文检索
|
|
120
|
-
'$regex', // 正则匹配
|
|
121
|
-
'$jsonSchema' // Schema 验证
|
|
122
|
-
];
|
|
123
|
-
|
|
124
|
-
return complexOps.some(op => query[op] !== undefined);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* 从缓存键中提取查询条件
|
|
129
|
-
* @param {string} cacheKey - 缓存键(stableStringify 序列化后的)
|
|
130
|
-
* @returns {Object|null} 查询条件对象,无法解析返回 null
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* const key = '{"ns":{...},"op":"find","query":{"status":"active"},...}';
|
|
134
|
-
* extractQueryFromKey(key) // → { status: 'active' }
|
|
135
|
-
*/
|
|
136
|
-
static extractQueryFromKey(cacheKey) {
|
|
137
|
-
try {
|
|
138
|
-
// 缓存键格式: stableStringify({ns, op, query, projection, ...})
|
|
139
|
-
// 注意:stableStringify 可能输出 undefined(不是有效 JSON),需要先替换
|
|
140
|
-
const sanitizedKey = cacheKey.replace(/:undefined/g, ':null');
|
|
141
|
-
const keyObj = JSON.parse(sanitizedKey);
|
|
142
|
-
|
|
143
|
-
// 提取 query 字段
|
|
144
|
-
if (keyObj && typeof keyObj === 'object' && keyObj.query !== undefined && keyObj.query !== null) {
|
|
145
|
-
return keyObj.query;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return null;
|
|
149
|
-
} catch (err) {
|
|
150
|
-
// 解析失败,返回 null
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 合并 filter 和 update 构造文档(用于 upsert)
|
|
157
|
-
* @param {Object} filter - 查询条件
|
|
158
|
-
* @param {Object} update - 更新操作
|
|
159
|
-
* @returns {Object} 合并后的文档
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* mergeFilterAndUpdate(
|
|
163
|
-
* { userId: 'user123' },
|
|
164
|
-
* { $set: { name: 'Alice', age: 25 } }
|
|
165
|
-
* ) // → { userId: 'user123', name: 'Alice', age: 25 }
|
|
166
|
-
*/
|
|
167
|
-
static mergeFilterAndUpdate(filter, update) {
|
|
168
|
-
const doc = { ...filter };
|
|
169
|
-
|
|
170
|
-
if (!update || typeof update !== 'object') {
|
|
171
|
-
return doc;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// 提取 $set 字段
|
|
175
|
-
if (update.$set && typeof update.$set === 'object') {
|
|
176
|
-
Object.assign(doc, update.$set);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// 提取 $inc 字段(简化处理:假设增量字段也可能匹配)
|
|
180
|
-
if (update.$inc && typeof update.$inc === 'object') {
|
|
181
|
-
Object.assign(doc, update.$inc);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// 提取 $setOnInsert 字段(仅插入时)
|
|
185
|
-
if (update.$setOnInsert && typeof update.$setOnInsert === 'object') {
|
|
186
|
-
Object.assign(doc, update.$setOnInsert);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// 提取 $unset 字段(设为 undefined)
|
|
190
|
-
if (update.$unset && typeof update.$unset === 'object') {
|
|
191
|
-
for (const key of Object.keys(update.$unset)) {
|
|
192
|
-
doc[key] = undefined;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return doc;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 精准失效缓存
|
|
201
|
-
* @param {Object} cache - 缓存实例
|
|
202
|
-
* @param {Object} context - 失效上下文
|
|
203
|
-
* @param {string} context.instanceId - 实例ID
|
|
204
|
-
* @param {string} context.type - 数据库类型
|
|
205
|
-
* @param {string} context.db - 数据库名
|
|
206
|
-
* @param {string} context.collection - 集合名
|
|
207
|
-
* @param {Object} context.document - 受影响的文档
|
|
208
|
-
* @param {string} context.operation - 操作类型
|
|
209
|
-
* @returns {Promise<number>} 删除的缓存键数量
|
|
210
|
-
*
|
|
211
|
-
* @example
|
|
212
|
-
* await invalidatePrecise(cache, {
|
|
213
|
-
* instanceId: 'test',
|
|
214
|
-
* type: 'mongodb',
|
|
215
|
-
* db: 'shop',
|
|
216
|
-
* collection: 'users',
|
|
217
|
-
* document: { status: 'active', name: 'Alice' },
|
|
218
|
-
* operation: 'insertOne'
|
|
219
|
-
* })
|
|
220
|
-
*/
|
|
221
|
-
static async invalidatePrecise(cache, context) {
|
|
222
|
-
const { instanceId, type, db, collection, document, operation } = context;
|
|
223
|
-
|
|
224
|
-
// 🆕 v1.1.6: 规范化 document 中的 ObjectId 为字符串
|
|
225
|
-
// 确保与缓存键中的 query 格式一致(缓存键生成时已规范化)
|
|
226
|
-
const CacheFactory = require('./cache');
|
|
227
|
-
const normalizedDocument = CacheFactory._normalizeObjectIds(document);
|
|
228
|
-
|
|
229
|
-
// Step 1: 构建 namespace 模式,获取集合的所有缓存键
|
|
230
|
-
const pattern = CacheFactory.buildNamespacePattern({
|
|
231
|
-
iid: instanceId,
|
|
232
|
-
type,
|
|
233
|
-
db,
|
|
234
|
-
collection
|
|
235
|
-
});
|
|
236
|
-
const allKeys = cache.keys(pattern);
|
|
237
|
-
|
|
238
|
-
// 没有缓存,直接返回
|
|
239
|
-
if (!allKeys || allKeys.length === 0) {
|
|
240
|
-
return 0;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// Step 2: 遍历每个缓存键,判断是否需要失效
|
|
244
|
-
const keysToDelete = [];
|
|
245
|
-
|
|
246
|
-
for (const key of allKeys) {
|
|
247
|
-
// Step 2.1: 解析查询条件
|
|
248
|
-
const query = this.extractQueryFromKey(key);
|
|
249
|
-
if (query === null) {
|
|
250
|
-
// 解析失败(返回 null),跳过该键
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Step 2.2: 检测是否包含复杂操作符
|
|
255
|
-
if (this.hasComplexOperators(query)) {
|
|
256
|
-
// 复杂查询,跳过失效(按 TTL 自然过期)
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Step 2.3: 判断文档是否匹配查询条件
|
|
261
|
-
if (this.matchesQuery(normalizedDocument, query)) {
|
|
262
|
-
// 匹配成功,标记为需要失效
|
|
263
|
-
keysToDelete.push(key);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Step 3: 批量删除匹配的缓存键
|
|
268
|
-
if (keysToDelete.length > 0) {
|
|
269
|
-
const deleted = await cache.delMany(keysToDelete);
|
|
270
|
-
return deleted;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
return 0;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
module.exports = CacheInvalidationEngine;
|
|
278
|
-
|
|
279
|
-
|