@taicode/common-base 1.7.1 → 1.7.2
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/output/batching-buffer/batching-buffer.d.ts +10 -17
- package/output/batching-buffer/batching-buffer.d.ts.map +1 -1
- package/output/batching-buffer/batching-buffer.js +37 -46
- package/output/batching-buffer/batching-buffer.test.js +8 -161
- package/output/index.d.ts +1 -0
- package/output/index.d.ts.map +1 -1
- package/output/index.js +1 -0
- package/output/logger/logger.d.ts +3 -3
- package/output/logger/logger.d.ts.map +1 -1
- package/output/logger/logger.js +2 -3
- package/output/logger/logger.test.js +1 -1
- package/output/ttl-cache/index.d.ts +2 -0
- package/output/ttl-cache/index.d.ts.map +1 -0
- package/output/ttl-cache/index.js +1 -0
- package/output/ttl-cache/ttl-cache.d.ts +148 -0
- package/output/ttl-cache/ttl-cache.d.ts.map +1 -0
- package/output/ttl-cache/ttl-cache.js +313 -0
- package/output/ttl-cache/ttl-cache.test.d.ts +2 -0
- package/output/ttl-cache/ttl-cache.test.d.ts.map +1 -0
- package/output/ttl-cache/ttl-cache.test.js +222 -0
- package/package.json +1 -1
|
@@ -25,12 +25,6 @@ export interface BatchingBufferOptions<T> {
|
|
|
25
25
|
* @default true
|
|
26
26
|
*/
|
|
27
27
|
autoFlush?: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* 错误处理回调函数
|
|
30
|
-
* @param error 发生的错误
|
|
31
|
-
* @param failedItems 处理失败的项目
|
|
32
|
-
*/
|
|
33
|
-
onError?: (error: Error, failedItems: T[]) => void;
|
|
34
28
|
}
|
|
35
29
|
/**
|
|
36
30
|
* 批处理缓冲区
|
|
@@ -48,11 +42,7 @@ export interface BatchingBufferOptions<T> {
|
|
|
48
42
|
* await saveToDatabase(items)
|
|
49
43
|
* },
|
|
50
44
|
* bufferSize: 10,
|
|
51
|
-
* flushInterval: 5000
|
|
52
|
-
* onError: (error, failedItems) => {
|
|
53
|
-
* console.error('处理失败:', error, '失败项目:', failedItems)
|
|
54
|
-
* // 可以进行重试或其他错误处理逻辑
|
|
55
|
-
* }
|
|
45
|
+
* flushInterval: 5000
|
|
56
46
|
* })
|
|
57
47
|
*
|
|
58
48
|
* // 单个处理模式
|
|
@@ -66,9 +56,8 @@ export interface BatchingBufferOptions<T> {
|
|
|
66
56
|
* })
|
|
67
57
|
*
|
|
68
58
|
* // 添加数据
|
|
69
|
-
* buffer.add('data1')
|
|
70
|
-
* buffer.add('data2'
|
|
71
|
-
* buffer.add(...array) // 展开数组添加
|
|
59
|
+
* buffer.add('data1')
|
|
60
|
+
* buffer.add('data2')
|
|
72
61
|
*
|
|
73
62
|
* // 手动刷新
|
|
74
63
|
* await buffer.flush()
|
|
@@ -87,13 +76,17 @@ export declare class BatchingBuffer<T> {
|
|
|
87
76
|
private readonly bufferSize;
|
|
88
77
|
private readonly flushInterval;
|
|
89
78
|
private readonly autoFlush;
|
|
90
|
-
private readonly onError?;
|
|
91
79
|
constructor(options: BatchingBufferOptions<T>);
|
|
92
80
|
/**
|
|
93
81
|
* 添加数据到缓冲区
|
|
94
|
-
* @param
|
|
82
|
+
* @param item 要添加的数据项
|
|
83
|
+
*/
|
|
84
|
+
add(item: T): void;
|
|
85
|
+
/**
|
|
86
|
+
* 批量添加数据到缓冲区
|
|
87
|
+
* @param items 要添加的数据项数组
|
|
95
88
|
*/
|
|
96
|
-
|
|
89
|
+
addBatch(items: T[]): void;
|
|
97
90
|
/**
|
|
98
91
|
* 手动刷新缓冲区,处理所有待处理的数据
|
|
99
92
|
* @returns Promise,在处理完成后 resolve
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batching-buffer.d.ts","sourceRoot":"","sources":["../../source/batching-buffer/batching-buffer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACtC;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAErD;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAEjD;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"batching-buffer.d.ts","sourceRoot":"","sources":["../../source/batching-buffer/batching-buffer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACtC;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAErD;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAEjD;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,cAAc,CAAC,CAAC;IAC3B,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAsC;IACtE,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAmC;IAClE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;IACnC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAQ;IACtC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC;IA0B7C;;;OAGG;IACH,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI;IAmBlB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI;IAyB1B;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;OAEG;IACH,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;OAEG;IACH,IAAI,kBAAkB,IAAI,MAAM,CAE/B;IAED;;;OAGG;IACG,OAAO,CAAC,cAAc,GAAE,OAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB5D;;OAEG;IACH,OAAO,CAAC,UAAU;IAQlB;;OAEG;IACH,OAAO,CAAC,UAAU;IAOlB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAiDzB;;OAEG;YACW,kBAAkB;CA2BjC"}
|
|
@@ -14,11 +14,7 @@
|
|
|
14
14
|
* await saveToDatabase(items)
|
|
15
15
|
* },
|
|
16
16
|
* bufferSize: 10,
|
|
17
|
-
* flushInterval: 5000
|
|
18
|
-
* onError: (error, failedItems) => {
|
|
19
|
-
* console.error('处理失败:', error, '失败项目:', failedItems)
|
|
20
|
-
* // 可以进行重试或其他错误处理逻辑
|
|
21
|
-
* }
|
|
17
|
+
* flushInterval: 5000
|
|
22
18
|
* })
|
|
23
19
|
*
|
|
24
20
|
* // 单个处理模式
|
|
@@ -32,9 +28,8 @@
|
|
|
32
28
|
* })
|
|
33
29
|
*
|
|
34
30
|
* // 添加数据
|
|
35
|
-
* buffer.add('data1')
|
|
36
|
-
* buffer.add('data2'
|
|
37
|
-
* buffer.add(...array) // 展开数组添加
|
|
31
|
+
* buffer.add('data1')
|
|
32
|
+
* buffer.add('data2')
|
|
38
33
|
*
|
|
39
34
|
* // 手动刷新
|
|
40
35
|
* await buffer.flush()
|
|
@@ -53,7 +48,6 @@ export class BatchingBuffer {
|
|
|
53
48
|
bufferSize;
|
|
54
49
|
flushInterval;
|
|
55
50
|
autoFlush;
|
|
56
|
-
onError;
|
|
57
51
|
constructor(options) {
|
|
58
52
|
// 验证处理器配置
|
|
59
53
|
if (!options.batchProcessor && !options.itemProcessor) {
|
|
@@ -67,7 +61,6 @@ export class BatchingBuffer {
|
|
|
67
61
|
this.bufferSize = options.bufferSize ?? 100;
|
|
68
62
|
this.flushInterval = options.flushInterval ?? 5000;
|
|
69
63
|
this.autoFlush = options.autoFlush ?? true;
|
|
70
|
-
this.onError = options.onError;
|
|
71
64
|
// 验证配置参数
|
|
72
65
|
if (this.bufferSize <= 0) {
|
|
73
66
|
throw new Error('bufferSize 必须大于 0');
|
|
@@ -78,33 +71,46 @@ export class BatchingBuffer {
|
|
|
78
71
|
}
|
|
79
72
|
/**
|
|
80
73
|
* 添加数据到缓冲区
|
|
81
|
-
* @param
|
|
74
|
+
* @param item 要添加的数据项
|
|
82
75
|
*/
|
|
83
|
-
add(
|
|
76
|
+
add(item) {
|
|
84
77
|
if (this.isDestroyed) {
|
|
85
78
|
throw new Error('BatchingBuffer 已被销毁,不能添加新数据');
|
|
86
79
|
}
|
|
87
|
-
|
|
80
|
+
this.buffer.push(item);
|
|
81
|
+
// 检查是否需要基于大小触发处理
|
|
82
|
+
if (this.buffer.length >= this.bufferSize) {
|
|
83
|
+
this.processBufferSync();
|
|
88
84
|
return;
|
|
89
85
|
}
|
|
86
|
+
// 启动或重置定时器
|
|
87
|
+
if (this.autoFlush) {
|
|
88
|
+
this.resetTimer();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 批量添加数据到缓冲区
|
|
93
|
+
* @param items 要添加的数据项数组
|
|
94
|
+
*/
|
|
95
|
+
addBatch(items) {
|
|
96
|
+
if (this.isDestroyed) {
|
|
97
|
+
throw new Error('BatchingBuffer 已被销毁,不能添加新数据');
|
|
98
|
+
}
|
|
90
99
|
let i = 0;
|
|
91
100
|
while (i < items.length) {
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
for (let j = 0; j < itemsToAdd; j++) {
|
|
97
|
-
this.buffer.push(items[i + j]);
|
|
101
|
+
// 填充缓冲区直到达到大小限制
|
|
102
|
+
while (i < items.length && this.buffer.length < this.bufferSize) {
|
|
103
|
+
this.buffer.push(items[i]);
|
|
104
|
+
i++;
|
|
98
105
|
}
|
|
99
|
-
i += itemsToAdd;
|
|
100
106
|
// 如果缓冲区满了,处理它
|
|
101
107
|
if (this.buffer.length >= this.bufferSize) {
|
|
102
108
|
this.processBufferSync();
|
|
103
109
|
}
|
|
104
110
|
}
|
|
105
|
-
//
|
|
106
|
-
if (this.buffer.length > 0
|
|
107
|
-
this.
|
|
111
|
+
// 处理剩余的项目(如果有的话)
|
|
112
|
+
if (this.buffer.length > 0) {
|
|
113
|
+
this.processBufferSync();
|
|
108
114
|
}
|
|
109
115
|
}
|
|
110
116
|
/**
|
|
@@ -188,44 +194,29 @@ export class BatchingBuffer {
|
|
|
188
194
|
const result = this.batchProcessor(itemsToProcess);
|
|
189
195
|
// 如果返回 Promise,处理异步
|
|
190
196
|
if (result && typeof result.then === 'function') {
|
|
191
|
-
result.catch((
|
|
192
|
-
//
|
|
193
|
-
|
|
194
|
-
this.buffer.unshift(...itemsToProcess);
|
|
195
|
-
}
|
|
196
|
-
// 调用错误处理回调
|
|
197
|
-
if (this.onError) {
|
|
198
|
-
this.onError(error, itemsToProcess);
|
|
199
|
-
}
|
|
197
|
+
result.catch(() => {
|
|
198
|
+
// 处理失败时,将数据重新放回缓冲区开头
|
|
199
|
+
this.buffer.unshift(...itemsToProcess);
|
|
200
200
|
});
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
203
|
else if (this.itemProcessor) {
|
|
204
204
|
// 单个处理模式 - 同步处理每个项目
|
|
205
|
-
const failedItems = [];
|
|
206
205
|
for (let i = 0; i < itemsToProcess.length; i++) {
|
|
207
206
|
const item = itemsToProcess[i];
|
|
208
207
|
try {
|
|
209
208
|
const result = this.itemProcessor(item);
|
|
210
209
|
// 如果返回 Promise,处理异步
|
|
211
210
|
if (result && typeof result.then === 'function') {
|
|
212
|
-
result.catch((
|
|
213
|
-
//
|
|
214
|
-
|
|
215
|
-
this.buffer.unshift(item);
|
|
216
|
-
}
|
|
217
|
-
// 调用错误处理回调
|
|
218
|
-
if (this.onError) {
|
|
219
|
-
this.onError(error, [item]);
|
|
220
|
-
}
|
|
211
|
+
result.catch(() => {
|
|
212
|
+
// 处理失败时,将数据重新放回缓冲区开头
|
|
213
|
+
this.buffer.unshift(item);
|
|
221
214
|
});
|
|
222
215
|
}
|
|
223
216
|
}
|
|
224
217
|
catch (error) {
|
|
225
|
-
// 同步错误处理 -
|
|
226
|
-
|
|
227
|
-
failedItems.push(...itemsToProcess.slice(i + 1));
|
|
228
|
-
this.buffer.unshift(...failedItems);
|
|
218
|
+
// 同步错误处理 - 只把未处理的项目放回缓冲区
|
|
219
|
+
this.buffer.unshift(...itemsToProcess.slice(i));
|
|
229
220
|
throw error;
|
|
230
221
|
}
|
|
231
222
|
}
|
|
@@ -122,32 +122,12 @@ describe('BatchingBuffer', () => {
|
|
|
122
122
|
expect(buffer.size).toBe(0);
|
|
123
123
|
});
|
|
124
124
|
it('应该支持批量添加数据', () => {
|
|
125
|
-
buffer.
|
|
126
|
-
expect(batchProcessor).toHaveBeenCalledTimes(
|
|
125
|
+
buffer.addBatch(['item1', 'item2', 'item3', 'item4', 'item5']);
|
|
126
|
+
expect(batchProcessor).toHaveBeenCalledTimes(2);
|
|
127
127
|
expect(batchProcessor).toHaveBeenNthCalledWith(1, ['item1', 'item2', 'item3']);
|
|
128
|
-
|
|
129
|
-
expect(buffer.size).toBe(2);
|
|
130
|
-
});
|
|
131
|
-
it('应该支持添加空参数', () => {
|
|
132
|
-
buffer.add();
|
|
133
|
-
expect(batchProcessor).not.toHaveBeenCalled();
|
|
134
|
-
expect(buffer.size).toBe(0);
|
|
135
|
-
});
|
|
136
|
-
it('应该支持混合添加单个和多个数据', () => {
|
|
137
|
-
buffer.add('item1'); // 单个
|
|
138
|
-
buffer.add('item2', 'item3'); // 多个
|
|
139
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
140
|
-
expect(batchProcessor).toHaveBeenCalledWith(['item1', 'item2', 'item3']);
|
|
128
|
+
expect(batchProcessor).toHaveBeenNthCalledWith(2, ['item4', 'item5']);
|
|
141
129
|
expect(buffer.size).toBe(0);
|
|
142
130
|
});
|
|
143
|
-
it('应该支持使用数组展开语法', () => {
|
|
144
|
-
const items = ['item1', 'item2', 'item3', 'item4', 'item5'];
|
|
145
|
-
buffer.add(...items);
|
|
146
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
147
|
-
expect(batchProcessor).toHaveBeenNthCalledWith(1, ['item1', 'item2', 'item3']);
|
|
148
|
-
// 剩余的项目应该在缓冲区中
|
|
149
|
-
expect(buffer.size).toBe(2);
|
|
150
|
-
});
|
|
151
131
|
it('应该正确处理处理器异常', () => {
|
|
152
132
|
const error = new Error('处理失败');
|
|
153
133
|
batchProcessor.mockImplementationOnce(() => {
|
|
@@ -236,11 +216,10 @@ describe('BatchingBuffer', () => {
|
|
|
236
216
|
it('应该正确报告距离上次刷新的时间', async () => {
|
|
237
217
|
const startTime = Date.now();
|
|
238
218
|
buffer.add(1);
|
|
239
|
-
// 使用假定时器来精确控制时间
|
|
240
219
|
vi.advanceTimersByTime(500);
|
|
241
|
-
expect(buffer.timeSinceLastFlush).
|
|
220
|
+
expect(buffer.timeSinceLastFlush).toBeGreaterThanOrEqual(500);
|
|
242
221
|
await buffer.flush();
|
|
243
|
-
expect(buffer.timeSinceLastFlush).
|
|
222
|
+
expect(buffer.timeSinceLastFlush).toBeLessThan(100);
|
|
244
223
|
});
|
|
245
224
|
});
|
|
246
225
|
describe('销毁功能', () => {
|
|
@@ -275,7 +254,7 @@ describe('BatchingBuffer', () => {
|
|
|
275
254
|
buffer.add('item1');
|
|
276
255
|
}).toThrow('BatchingBuffer 已被销毁,不能添加新数据');
|
|
277
256
|
expect(() => {
|
|
278
|
-
buffer.
|
|
257
|
+
buffer.addBatch(['item1', 'item2']);
|
|
279
258
|
}).toThrow('BatchingBuffer 已被销毁,不能添加新数据');
|
|
280
259
|
});
|
|
281
260
|
it('应该支持重复销毁', async () => {
|
|
@@ -314,45 +293,6 @@ describe('BatchingBuffer', () => {
|
|
|
314
293
|
expect(batchProcessor).toHaveBeenCalledWith(['item1', 'item2']);
|
|
315
294
|
await buffer.destroy();
|
|
316
295
|
});
|
|
317
|
-
it('应该正确处理定时器重置', async () => {
|
|
318
|
-
const buffer = new BatchingBuffer({
|
|
319
|
-
batchProcessor: batchProcessor,
|
|
320
|
-
bufferSize: 5,
|
|
321
|
-
flushInterval: 1000,
|
|
322
|
-
autoFlush: true
|
|
323
|
-
});
|
|
324
|
-
// 添加第一个项目,启动定时器
|
|
325
|
-
buffer.add('item1');
|
|
326
|
-
vi.advanceTimersByTime(500);
|
|
327
|
-
// 添加第二个项目,应该重置定时器
|
|
328
|
-
buffer.add('item2');
|
|
329
|
-
vi.advanceTimersByTime(500);
|
|
330
|
-
// 此时应该还没触发(总共过了1000ms,但最后一次重置后只过了500ms)
|
|
331
|
-
expect(batchProcessor).not.toHaveBeenCalled();
|
|
332
|
-
// 再等500ms,现在应该触发了
|
|
333
|
-
vi.advanceTimersByTime(500);
|
|
334
|
-
await vi.runAllTimersAsync();
|
|
335
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
336
|
-
expect(batchProcessor).toHaveBeenCalledWith(['item1', 'item2']);
|
|
337
|
-
await buffer.destroy();
|
|
338
|
-
});
|
|
339
|
-
it('应该在销毁时清除定时器避免内存泄漏', async () => {
|
|
340
|
-
const buffer = new BatchingBuffer({
|
|
341
|
-
batchProcessor: batchProcessor,
|
|
342
|
-
bufferSize: 5,
|
|
343
|
-
flushInterval: 1000,
|
|
344
|
-
autoFlush: true
|
|
345
|
-
});
|
|
346
|
-
buffer.add('item1');
|
|
347
|
-
// 立即销毁,应该处理剩余数据但不会因为定时器再次触发
|
|
348
|
-
await buffer.destroy();
|
|
349
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
350
|
-
// 等待原本的定时器时间,不应该再次触发
|
|
351
|
-
vi.advanceTimersByTime(1000);
|
|
352
|
-
await vi.runAllTimersAsync();
|
|
353
|
-
// 仍然只被调用一次
|
|
354
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
355
|
-
});
|
|
356
296
|
});
|
|
357
297
|
describe('复杂数据类型', () => {
|
|
358
298
|
let buffer;
|
|
@@ -381,11 +321,11 @@ describe('BatchingBuffer', () => {
|
|
|
381
321
|
let asyncItemProcessor;
|
|
382
322
|
beforeEach(() => {
|
|
383
323
|
asyncBatchProcessor = vi.fn().mockImplementation(async (items) => {
|
|
384
|
-
|
|
324
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
385
325
|
return `处理了 ${items.length} 个项目`;
|
|
386
326
|
});
|
|
387
327
|
asyncItemProcessor = vi.fn().mockImplementation(async (item) => {
|
|
388
|
-
|
|
328
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
389
329
|
return `处理了项目: ${item}`;
|
|
390
330
|
});
|
|
391
331
|
});
|
|
@@ -415,97 +355,4 @@ describe('BatchingBuffer', () => {
|
|
|
415
355
|
buffer.destroy();
|
|
416
356
|
});
|
|
417
357
|
});
|
|
418
|
-
describe('错误处理回调', () => {
|
|
419
|
-
it('应该支持配置错误处理回调', () => {
|
|
420
|
-
const onError = vi.fn();
|
|
421
|
-
const buffer = new BatchingBuffer({
|
|
422
|
-
batchProcessor: vi.fn(),
|
|
423
|
-
bufferSize: 10,
|
|
424
|
-
flushInterval: 1000,
|
|
425
|
-
onError
|
|
426
|
-
});
|
|
427
|
-
expect(buffer).toBeDefined();
|
|
428
|
-
buffer.destroy();
|
|
429
|
-
});
|
|
430
|
-
it('错误处理回调应该可以正确配置', () => {
|
|
431
|
-
const onError = vi.fn();
|
|
432
|
-
const buffer = new BatchingBuffer({
|
|
433
|
-
batchProcessor: vi.fn(),
|
|
434
|
-
bufferSize: 10,
|
|
435
|
-
flushInterval: 1000,
|
|
436
|
-
onError
|
|
437
|
-
});
|
|
438
|
-
// 验证缓冲区正常工作
|
|
439
|
-
buffer.add('item1');
|
|
440
|
-
expect(buffer.size).toBe(1);
|
|
441
|
-
buffer.destroy();
|
|
442
|
-
});
|
|
443
|
-
});
|
|
444
|
-
describe('边界条件测试', () => {
|
|
445
|
-
it('应该正确处理极小的 bufferSize', () => {
|
|
446
|
-
const buffer = new BatchingBuffer({
|
|
447
|
-
batchProcessor: batchProcessor,
|
|
448
|
-
bufferSize: 1,
|
|
449
|
-
flushInterval: 1000
|
|
450
|
-
});
|
|
451
|
-
buffer.add('item1');
|
|
452
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
453
|
-
expect(batchProcessor).toHaveBeenCalledWith(['item1']);
|
|
454
|
-
buffer.destroy();
|
|
455
|
-
});
|
|
456
|
-
it('应该正确处理极小的 flushInterval', async () => {
|
|
457
|
-
const buffer = new BatchingBuffer({
|
|
458
|
-
batchProcessor: batchProcessor,
|
|
459
|
-
bufferSize: 5,
|
|
460
|
-
flushInterval: 1 // 1ms
|
|
461
|
-
});
|
|
462
|
-
buffer.add('item1');
|
|
463
|
-
vi.advanceTimersByTime(1);
|
|
464
|
-
await vi.runAllTimersAsync();
|
|
465
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
466
|
-
expect(batchProcessor).toHaveBeenCalledWith(['item1']);
|
|
467
|
-
await buffer.destroy();
|
|
468
|
-
});
|
|
469
|
-
it('应该正确处理同时达到大小和时间限制', async () => {
|
|
470
|
-
const buffer = new BatchingBuffer({
|
|
471
|
-
batchProcessor: batchProcessor,
|
|
472
|
-
bufferSize: 2,
|
|
473
|
-
flushInterval: 100
|
|
474
|
-
});
|
|
475
|
-
buffer.add('item1');
|
|
476
|
-
// 在时间限制之前添加第二个项目,应该立即触发
|
|
477
|
-
buffer.add('item2');
|
|
478
|
-
expect(batchProcessor).toHaveBeenCalledTimes(1);
|
|
479
|
-
expect(batchProcessor).toHaveBeenCalledWith(['item1', 'item2']);
|
|
480
|
-
await buffer.destroy();
|
|
481
|
-
});
|
|
482
|
-
it('应该正确处理在处理过程中继续添加数据', () => {
|
|
483
|
-
const processedBatches = [];
|
|
484
|
-
let processingCount = 0;
|
|
485
|
-
const batchProcessorWithCallback = vi.fn().mockImplementation((items) => {
|
|
486
|
-
processingCount++;
|
|
487
|
-
processedBatches.push([...items]);
|
|
488
|
-
// 在第一次处理过程中添加新数据
|
|
489
|
-
if (processingCount === 1) {
|
|
490
|
-
buffer.add('item4');
|
|
491
|
-
}
|
|
492
|
-
});
|
|
493
|
-
const buffer = new BatchingBuffer({
|
|
494
|
-
batchProcessor: batchProcessorWithCallback,
|
|
495
|
-
bufferSize: 2,
|
|
496
|
-
flushInterval: 1000
|
|
497
|
-
});
|
|
498
|
-
buffer.add('item1', 'item2', 'item3');
|
|
499
|
-
// 验证处理次数
|
|
500
|
-
expect(batchProcessorWithCallback).toHaveBeenCalledTimes(2);
|
|
501
|
-
// 验证第一次处理了前两个项目
|
|
502
|
-
expect(processedBatches[0]).toEqual(['item1', 'item2']);
|
|
503
|
-
// 第二次处理的顺序可能是 ['item4', 'item3'] 或 ['item3', 'item4']
|
|
504
|
-
// 这取决于具体实现,我们验证包含了正确的项目
|
|
505
|
-
expect(processedBatches[1]).toHaveLength(2);
|
|
506
|
-
expect(processedBatches[1]).toContain('item3');
|
|
507
|
-
expect(processedBatches[1]).toContain('item4');
|
|
508
|
-
buffer.destroy();
|
|
509
|
-
});
|
|
510
|
-
});
|
|
511
358
|
});
|
package/output/index.d.ts
CHANGED
package/output/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAG1B,cAAc,iBAAiB,CAAA;AAG/B,cAAc,UAAU,CAAA;AAGxB,cAAc,SAAS,CAAA;AAGvB,cAAc,WAAW,CAAA;AAGzB,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAG5B,cAAc,mBAAmB,CAAA;AAGjC,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AAGvB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAG1B,cAAc,iBAAiB,CAAA;AAG/B,cAAc,UAAU,CAAA;AAGxB,cAAc,SAAS,CAAA;AAGvB,cAAc,WAAW,CAAA;AAGzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAG5B,cAAc,mBAAmB,CAAA;AAGjC,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AAGvB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
package/output/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LogFormatter } from './formatter';
|
|
2
1
|
import { LogTransport } from './transport';
|
|
2
|
+
import { LogFormatter } from './formatter';
|
|
3
3
|
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
4
4
|
export interface LoggerOptions {
|
|
5
5
|
level?: LogLevel;
|
|
@@ -12,8 +12,8 @@ export interface LoggerOptions {
|
|
|
12
12
|
}
|
|
13
13
|
export declare class Logger {
|
|
14
14
|
private _level;
|
|
15
|
-
private transports;
|
|
16
15
|
private formatter;
|
|
16
|
+
private transports;
|
|
17
17
|
private print?;
|
|
18
18
|
constructor(options?: LoggerOptions);
|
|
19
19
|
private defaultPrint;
|
|
@@ -48,7 +48,7 @@ export declare class Logger {
|
|
|
48
48
|
/**
|
|
49
49
|
* 关闭所有 LogTransports
|
|
50
50
|
*/
|
|
51
|
-
|
|
51
|
+
closeTransports(): Promise<void>;
|
|
52
52
|
/**
|
|
53
53
|
* Pino 兼容的 child 方法
|
|
54
54
|
* 创建一个共享配置的子 logger
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../source/logger/logger.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../source/logger/logger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAa,MAAM,aAAa,CAAA;AACrD,OAAO,EAAuB,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/D,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAA;AAE9E,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,EAAE,QAAQ,CAAA;IAChB,oCAAoC;IACpC,UAAU,CAAC,EAAE,YAAY,EAAE,CAAA;IAC3B,aAAa;IACb,SAAS,CAAC,EAAE,YAAY,CAAA;IACxB,0BAA0B;IAC1B,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;CAClD;AAED,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,KAAK,CAAC,CAA2C;gBAE7C,OAAO,GAAE,aAAkB;IAYvC,OAAO,CAAC,YAAY;IA0BpB,OAAO,CAAC,SAAS;IAKjB;;OAEG;IACH,OAAO,CAAC,eAAe;IA2BvB;;OAEG;IACH,OAAO,CAAC,UAAU;IAUlB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOpB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOpB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOnB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOnB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOpB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAOpB,MAAM;IAIN;;OAEG;IACH,YAAY,CAAC,SAAS,EAAE,YAAY,GAAG,IAAI;IAI3C;;OAEG;IACH,eAAe,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI;IAI5C;;OAEG;IACH,aAAa,IAAI,SAAS,YAAY,EAAE;IAIxC;;OAEG;IACG,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAQtC;;;OAGG;IACH,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG,MAAM;IAkB/E;;OAEG;IACH,IAAI,KAAK,IAAI,QAAQ,CAEpB;IAED,IAAI,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAE3B;CACF;AAED,eAAO,MAAM,MAAM,QAAe,CAAA"}
|
package/output/logger/logger.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
// 基础 Logger 实现,支持简单的日志输出和扩展能力
|
|
2
1
|
import { defaultLogFormatter } from './formatter';
|
|
3
2
|
export class Logger {
|
|
4
3
|
_level;
|
|
5
|
-
transports;
|
|
6
4
|
formatter;
|
|
5
|
+
transports;
|
|
7
6
|
print;
|
|
8
7
|
constructor(options = {}) {
|
|
9
8
|
this._level = options.level || 'info';
|
|
@@ -143,7 +142,7 @@ export class Logger {
|
|
|
143
142
|
/**
|
|
144
143
|
* 关闭所有 LogTransports
|
|
145
144
|
*/
|
|
146
|
-
async
|
|
145
|
+
async closeTransports() {
|
|
147
146
|
const closePromises = this.transports
|
|
148
147
|
.map(transport => transport.close?.())
|
|
149
148
|
.filter(Boolean);
|
|
@@ -229,7 +229,7 @@ describe('Logger Transport 集成', () => {
|
|
|
229
229
|
close: mockClose2
|
|
230
230
|
};
|
|
231
231
|
const logger = new Logger({ transports: [transport1, transport2] });
|
|
232
|
-
await logger.
|
|
232
|
+
await logger.closeTransports();
|
|
233
233
|
expect(mockClose1).toHaveBeenCalled();
|
|
234
234
|
expect(mockClose2).toHaveBeenCalled();
|
|
235
235
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/ttl-cache/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TtlCache } from './ttl-cache';
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TTL 缓存配置选项
|
|
3
|
+
*/
|
|
4
|
+
export interface TtlCacheOptions {
|
|
5
|
+
/** 默认 TTL 时间(毫秒),默认为 5 分钟 */
|
|
6
|
+
defaultTtl?: number;
|
|
7
|
+
/** 清理间隔(毫秒),默认为 1 分钟 */
|
|
8
|
+
cleanupInterval?: number;
|
|
9
|
+
/** 最大缓存条目数量,默认无限制 */
|
|
10
|
+
maxSize?: number;
|
|
11
|
+
/** 是否启用自动清理,默认为 true */
|
|
12
|
+
enableCleanup?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* TTL 缓存统计信息
|
|
16
|
+
*/
|
|
17
|
+
export interface TtlCacheStats {
|
|
18
|
+
/** 当前条目数量 */
|
|
19
|
+
size: number;
|
|
20
|
+
/** 最大条目数量限制 */
|
|
21
|
+
maxSize?: number;
|
|
22
|
+
/** 命中次数 */
|
|
23
|
+
hits: number;
|
|
24
|
+
/** 未命中次数 */
|
|
25
|
+
misses: number;
|
|
26
|
+
/** 过期次数 */
|
|
27
|
+
expiredCount: number;
|
|
28
|
+
/** 命中率 */
|
|
29
|
+
hitRate: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* TTL(Time To Live)缓存实现
|
|
33
|
+
* 支持过期时间、自动清理、统计信息等功能
|
|
34
|
+
*
|
|
35
|
+
* 特点:
|
|
36
|
+
* - 支持为每个条目设置独立的 TTL
|
|
37
|
+
* - 自动定期清理过期条目
|
|
38
|
+
* - 支持最大容量限制
|
|
39
|
+
* - 提供详细的统计信息
|
|
40
|
+
* - 优雅的资源清理机制
|
|
41
|
+
*/
|
|
42
|
+
export declare class TtlCache<T> {
|
|
43
|
+
private cache;
|
|
44
|
+
private cleanupTimer?;
|
|
45
|
+
private readonly options;
|
|
46
|
+
private stats;
|
|
47
|
+
constructor(options?: TtlCacheOptions);
|
|
48
|
+
/**
|
|
49
|
+
* 设置缓存项
|
|
50
|
+
* @param key 缓存键
|
|
51
|
+
* @param value 缓存值
|
|
52
|
+
* @param ttl 过期时间(毫秒),默认使用构造函数中的值
|
|
53
|
+
*/
|
|
54
|
+
set(key: string, value: T, ttl?: number): void;
|
|
55
|
+
/**
|
|
56
|
+
* 获取缓存项
|
|
57
|
+
* @param key 缓存键
|
|
58
|
+
* @returns 缓存值或 undefined
|
|
59
|
+
*/
|
|
60
|
+
get(key: string): T | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* 检查缓存项是否存在且未过期
|
|
63
|
+
* @param key 缓存键
|
|
64
|
+
*/
|
|
65
|
+
has(key: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* 删除缓存项
|
|
68
|
+
* @param key 缓存键
|
|
69
|
+
* @returns 是否成功删除
|
|
70
|
+
*/
|
|
71
|
+
delete(key: string): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* 清空所有缓存
|
|
74
|
+
*/
|
|
75
|
+
clear(): void;
|
|
76
|
+
/**
|
|
77
|
+
* 获取当前缓存大小
|
|
78
|
+
*/
|
|
79
|
+
get size(): number;
|
|
80
|
+
/**
|
|
81
|
+
* 获取缓存容量
|
|
82
|
+
*/
|
|
83
|
+
get capacity(): number;
|
|
84
|
+
/**
|
|
85
|
+
* 获取所有未过期的键
|
|
86
|
+
*/
|
|
87
|
+
keys(): string[];
|
|
88
|
+
/**
|
|
89
|
+
* 获取所有未过期的值
|
|
90
|
+
*/
|
|
91
|
+
values(): T[];
|
|
92
|
+
/**
|
|
93
|
+
* 获取所有未过期的键值对
|
|
94
|
+
*/
|
|
95
|
+
entries(): Array<[string, T]>;
|
|
96
|
+
/**
|
|
97
|
+
* 获取或设置缓存(如果不存在)
|
|
98
|
+
* @param key 缓存键
|
|
99
|
+
* @param factory 工厂函数,用于生成缓存值
|
|
100
|
+
* @param ttl 过期时间(毫秒)
|
|
101
|
+
*/
|
|
102
|
+
getOrSet(key: string, factory: () => Promise<T>, ttl?: number): Promise<T>;
|
|
103
|
+
getOrSet(key: string, factory: () => T, ttl?: number): T;
|
|
104
|
+
/**
|
|
105
|
+
* 获取缓存统计信息
|
|
106
|
+
*/
|
|
107
|
+
getStats(): TtlCacheStats;
|
|
108
|
+
/**
|
|
109
|
+
* 重置统计信息
|
|
110
|
+
*/
|
|
111
|
+
resetStats(): void;
|
|
112
|
+
/**
|
|
113
|
+
* 手动清理过期缓存
|
|
114
|
+
* @returns 清理的条目数量
|
|
115
|
+
*/
|
|
116
|
+
cleanup(): number;
|
|
117
|
+
/**
|
|
118
|
+
* 获取指定键的剩余 TTL 时间
|
|
119
|
+
* @param key 缓存键
|
|
120
|
+
* @returns 剩余时间(毫秒),如果不存在或已过期返回 -1
|
|
121
|
+
*/
|
|
122
|
+
getTtl(key: string): number;
|
|
123
|
+
/**
|
|
124
|
+
* 销毁缓存实例,清理所有资源
|
|
125
|
+
*/
|
|
126
|
+
destroy(): void;
|
|
127
|
+
/**
|
|
128
|
+
* 启动定期清理过期缓存
|
|
129
|
+
*/
|
|
130
|
+
private startCleanup;
|
|
131
|
+
/**
|
|
132
|
+
* 停止定期清理
|
|
133
|
+
*/
|
|
134
|
+
private stopCleanup;
|
|
135
|
+
/**
|
|
136
|
+
* 驱逐最旧的缓存条目
|
|
137
|
+
*/
|
|
138
|
+
private evictOldest;
|
|
139
|
+
/**
|
|
140
|
+
* 更新统计信息
|
|
141
|
+
*/
|
|
142
|
+
private updateStats;
|
|
143
|
+
/**
|
|
144
|
+
* 更新命中率
|
|
145
|
+
*/
|
|
146
|
+
private updateHitRate;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=ttl-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ttl-cache.d.ts","sourceRoot":"","sources":["../../source/ttl-cache/ttl-cache.ts"],"names":[],"mappings":"AAYA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,6BAA6B;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,wBAAwB;IACxB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,qBAAqB;IACrB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,wBAAwB;IACxB,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,aAAa;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,eAAe;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW;IACX,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,WAAW;IACX,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU;IACV,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;;;GAUG;AACH,qBAAa,QAAQ,CAAC,CAAC;IACrB,OAAO,CAAC,KAAK,CAA+B;IAC5C,OAAO,CAAC,YAAY,CAAC,CAAK;IAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,KAAK,CAAe;gBAEhB,OAAO,GAAE,eAAoB;IAiCzC;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IA0B9C;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAuB/B;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAiBzB;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAQ5B;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED;;OAEG;IACH,IAAI,IAAI,MAAM,EAAE;IAahB;;OAEG;IACH,MAAM,IAAI,CAAC,EAAE;IAab;;OAEG;IACH,OAAO,IAAI,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAa7B;;;;;OAKG;IACG,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAChF,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC;IAoBxD;;OAEG;IACH,QAAQ,IAAI,aAAa;IAKzB;;OAEG;IACH,UAAU,IAAI,IAAI;IAWlB;;;OAGG;IACH,OAAO,IAAI,MAAM;IAmBjB;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAc3B;;OAEG;IACH,OAAO,IAAI,IAAI;IAKf;;OAEG;IACH,OAAO,CAAC,YAAY;IAUpB;;OAEG;IACH,OAAO,CAAC,WAAW;IAOnB;;OAEG;IACH,OAAO,CAAC,WAAW;IAgBnB;;OAEG;IACH,OAAO,CAAC,WAAW;IAKnB;;OAEG;IACH,OAAO,CAAC,aAAa;CAItB"}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TTL(Time To Live)缓存实现
|
|
3
|
+
* 支持过期时间、自动清理、统计信息等功能
|
|
4
|
+
*
|
|
5
|
+
* 特点:
|
|
6
|
+
* - 支持为每个条目设置独立的 TTL
|
|
7
|
+
* - 自动定期清理过期条目
|
|
8
|
+
* - 支持最大容量限制
|
|
9
|
+
* - 提供详细的统计信息
|
|
10
|
+
* - 优雅的资源清理机制
|
|
11
|
+
*/
|
|
12
|
+
export class TtlCache {
|
|
13
|
+
cache;
|
|
14
|
+
cleanupTimer;
|
|
15
|
+
options;
|
|
16
|
+
stats;
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.options = {
|
|
19
|
+
defaultTtl: options.defaultTtl ?? 5 * 60 * 1000, // 默认5分钟
|
|
20
|
+
cleanupInterval: options.cleanupInterval ?? 60 * 1000, // 默认1分钟清理一次
|
|
21
|
+
maxSize: options.maxSize ?? Infinity,
|
|
22
|
+
enableCleanup: options.enableCleanup ?? true
|
|
23
|
+
};
|
|
24
|
+
if (this.options.defaultTtl <= 0) {
|
|
25
|
+
throw new Error('defaultTtl must be greater than 0');
|
|
26
|
+
}
|
|
27
|
+
if (this.options.cleanupInterval <= 0) {
|
|
28
|
+
throw new Error('cleanupInterval must be greater than 0');
|
|
29
|
+
}
|
|
30
|
+
if (this.options.maxSize <= 0) {
|
|
31
|
+
throw new Error('maxSize must be greater than 0');
|
|
32
|
+
}
|
|
33
|
+
this.cache = new Map();
|
|
34
|
+
this.stats = {
|
|
35
|
+
size: 0,
|
|
36
|
+
maxSize: this.options.maxSize === Infinity ? undefined : this.options.maxSize,
|
|
37
|
+
hits: 0,
|
|
38
|
+
misses: 0,
|
|
39
|
+
expiredCount: 0,
|
|
40
|
+
hitRate: 0
|
|
41
|
+
};
|
|
42
|
+
if (this.options.enableCleanup) {
|
|
43
|
+
this.startCleanup();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 设置缓存项
|
|
48
|
+
* @param key 缓存键
|
|
49
|
+
* @param value 缓存值
|
|
50
|
+
* @param ttl 过期时间(毫秒),默认使用构造函数中的值
|
|
51
|
+
*/
|
|
52
|
+
set(key, value, ttl) {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
const actualTtl = ttl ?? this.options.defaultTtl;
|
|
55
|
+
const expireAt = now + actualTtl;
|
|
56
|
+
// 检查容量限制
|
|
57
|
+
if (!this.cache.has(key) && this.cache.size >= this.options.maxSize) {
|
|
58
|
+
// 尝试清理过期条目
|
|
59
|
+
this.cleanup();
|
|
60
|
+
// 如果清理后仍然超过容量,删除最旧的条目
|
|
61
|
+
if (this.cache.size >= this.options.maxSize) {
|
|
62
|
+
this.evictOldest();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const entry = {
|
|
66
|
+
value,
|
|
67
|
+
expireAt,
|
|
68
|
+
createdAt: now
|
|
69
|
+
};
|
|
70
|
+
this.cache.set(key, entry);
|
|
71
|
+
this.updateStats();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 获取缓存项
|
|
75
|
+
* @param key 缓存键
|
|
76
|
+
* @returns 缓存值或 undefined
|
|
77
|
+
*/
|
|
78
|
+
get(key) {
|
|
79
|
+
const entry = this.cache.get(key);
|
|
80
|
+
if (!entry) {
|
|
81
|
+
this.stats.misses++;
|
|
82
|
+
this.updateHitRate();
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const now = Date.now();
|
|
86
|
+
if (now > entry.expireAt) {
|
|
87
|
+
this.cache.delete(key);
|
|
88
|
+
this.stats.expiredCount++;
|
|
89
|
+
this.stats.misses++;
|
|
90
|
+
this.updateStats();
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
this.stats.hits++;
|
|
94
|
+
this.updateHitRate();
|
|
95
|
+
return entry.value;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 检查缓存项是否存在且未过期
|
|
99
|
+
* @param key 缓存键
|
|
100
|
+
*/
|
|
101
|
+
has(key) {
|
|
102
|
+
const entry = this.cache.get(key);
|
|
103
|
+
if (!entry) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
if (now > entry.expireAt) {
|
|
108
|
+
this.cache.delete(key);
|
|
109
|
+
this.stats.expiredCount++;
|
|
110
|
+
this.updateStats();
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 删除缓存项
|
|
117
|
+
* @param key 缓存键
|
|
118
|
+
* @returns 是否成功删除
|
|
119
|
+
*/
|
|
120
|
+
delete(key) {
|
|
121
|
+
const result = this.cache.delete(key);
|
|
122
|
+
if (result) {
|
|
123
|
+
this.updateStats();
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 清空所有缓存
|
|
129
|
+
*/
|
|
130
|
+
clear() {
|
|
131
|
+
this.cache.clear();
|
|
132
|
+
this.stats.expiredCount = 0;
|
|
133
|
+
this.updateStats();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 获取当前缓存大小
|
|
137
|
+
*/
|
|
138
|
+
get size() {
|
|
139
|
+
return this.cache.size;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 获取缓存容量
|
|
143
|
+
*/
|
|
144
|
+
get capacity() {
|
|
145
|
+
return this.options.maxSize;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 获取所有未过期的键
|
|
149
|
+
*/
|
|
150
|
+
keys() {
|
|
151
|
+
const keys = [];
|
|
152
|
+
const now = Date.now();
|
|
153
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
154
|
+
if (now <= entry.expireAt) {
|
|
155
|
+
keys.push(key);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return keys;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 获取所有未过期的值
|
|
162
|
+
*/
|
|
163
|
+
values() {
|
|
164
|
+
const values = [];
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
for (const entry of this.cache.values()) {
|
|
167
|
+
if (now <= entry.expireAt) {
|
|
168
|
+
values.push(entry.value);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return values;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 获取所有未过期的键值对
|
|
175
|
+
*/
|
|
176
|
+
entries() {
|
|
177
|
+
const entries = [];
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
180
|
+
if (now <= entry.expireAt) {
|
|
181
|
+
entries.push([key, entry.value]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return entries;
|
|
185
|
+
}
|
|
186
|
+
getOrSet(key, factory, ttl) {
|
|
187
|
+
const cached = this.get(key);
|
|
188
|
+
if (cached !== undefined) {
|
|
189
|
+
return cached;
|
|
190
|
+
}
|
|
191
|
+
const result = factory();
|
|
192
|
+
if (result instanceof Promise) {
|
|
193
|
+
return result.then(value => {
|
|
194
|
+
this.set(key, value, ttl);
|
|
195
|
+
return value;
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
this.set(key, result, ttl);
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 获取缓存统计信息
|
|
203
|
+
*/
|
|
204
|
+
getStats() {
|
|
205
|
+
this.updateStats();
|
|
206
|
+
return { ...this.stats };
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 重置统计信息
|
|
210
|
+
*/
|
|
211
|
+
resetStats() {
|
|
212
|
+
this.stats = {
|
|
213
|
+
size: this.cache.size,
|
|
214
|
+
maxSize: this.options.maxSize === Infinity ? undefined : this.options.maxSize,
|
|
215
|
+
hits: 0,
|
|
216
|
+
misses: 0,
|
|
217
|
+
expiredCount: 0,
|
|
218
|
+
hitRate: 0
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 手动清理过期缓存
|
|
223
|
+
* @returns 清理的条目数量
|
|
224
|
+
*/
|
|
225
|
+
cleanup() {
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
let expiredCount = 0;
|
|
228
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
229
|
+
if (now > entry.expireAt) {
|
|
230
|
+
this.cache.delete(key);
|
|
231
|
+
expiredCount++;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (expiredCount > 0) {
|
|
235
|
+
this.stats.expiredCount += expiredCount;
|
|
236
|
+
this.updateStats();
|
|
237
|
+
}
|
|
238
|
+
return expiredCount;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* 获取指定键的剩余 TTL 时间
|
|
242
|
+
* @param key 缓存键
|
|
243
|
+
* @returns 剩余时间(毫秒),如果不存在或已过期返回 -1
|
|
244
|
+
*/
|
|
245
|
+
getTtl(key) {
|
|
246
|
+
const entry = this.cache.get(key);
|
|
247
|
+
if (!entry) {
|
|
248
|
+
return -1;
|
|
249
|
+
}
|
|
250
|
+
const now = Date.now();
|
|
251
|
+
if (now > entry.expireAt) {
|
|
252
|
+
return -1;
|
|
253
|
+
}
|
|
254
|
+
return entry.expireAt - now;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 销毁缓存实例,清理所有资源
|
|
258
|
+
*/
|
|
259
|
+
destroy() {
|
|
260
|
+
this.clear();
|
|
261
|
+
this.stopCleanup();
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* 启动定期清理过期缓存
|
|
265
|
+
*/
|
|
266
|
+
startCleanup() {
|
|
267
|
+
if (this.cleanupTimer) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
this.cleanupTimer = setInterval(() => {
|
|
271
|
+
this.cleanup();
|
|
272
|
+
}, this.options.cleanupInterval);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* 停止定期清理
|
|
276
|
+
*/
|
|
277
|
+
stopCleanup() {
|
|
278
|
+
if (this.cleanupTimer) {
|
|
279
|
+
clearInterval(this.cleanupTimer);
|
|
280
|
+
this.cleanupTimer = undefined;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* 驱逐最旧的缓存条目
|
|
285
|
+
*/
|
|
286
|
+
evictOldest() {
|
|
287
|
+
let oldestKey;
|
|
288
|
+
let oldestTime = Infinity;
|
|
289
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
290
|
+
if (entry.createdAt < oldestTime) {
|
|
291
|
+
oldestTime = entry.createdAt;
|
|
292
|
+
oldestKey = key;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (oldestKey) {
|
|
296
|
+
this.cache.delete(oldestKey);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* 更新统计信息
|
|
301
|
+
*/
|
|
302
|
+
updateStats() {
|
|
303
|
+
this.stats.size = this.cache.size;
|
|
304
|
+
this.updateHitRate();
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* 更新命中率
|
|
308
|
+
*/
|
|
309
|
+
updateHitRate() {
|
|
310
|
+
const total = this.stats.hits + this.stats.misses;
|
|
311
|
+
this.stats.hitRate = total > 0 ? this.stats.hits / total : 0;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ttl-cache.test.d.ts","sourceRoot":"","sources":["../../source/ttl-cache/ttl-cache.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { TtlCache } from './ttl-cache';
|
|
3
|
+
describe('TtlCache', () => {
|
|
4
|
+
let cache;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
vi.useFakeTimers();
|
|
7
|
+
cache = new TtlCache({ defaultTtl: 1000, enableCleanup: false });
|
|
8
|
+
});
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
cache.destroy();
|
|
11
|
+
vi.useRealTimers();
|
|
12
|
+
});
|
|
13
|
+
describe('基础功能', () => {
|
|
14
|
+
it('应该能够设置和获取缓存项', () => {
|
|
15
|
+
cache.set('key1', 'value1');
|
|
16
|
+
expect(cache.get('key1')).toBe('value1');
|
|
17
|
+
});
|
|
18
|
+
it('应该能够检查缓存项是否存在', () => {
|
|
19
|
+
cache.set('key1', 'value1');
|
|
20
|
+
expect(cache.has('key1')).toBe(true);
|
|
21
|
+
expect(cache.has('nonexistent')).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
it('应该能够删除缓存项', () => {
|
|
24
|
+
cache.set('key1', 'value1');
|
|
25
|
+
expect(cache.delete('key1')).toBe(true);
|
|
26
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
27
|
+
expect(cache.delete('nonexistent')).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
it('应该能够清空所有缓存', () => {
|
|
30
|
+
cache.set('key1', 'value1');
|
|
31
|
+
cache.set('key2', 'value2');
|
|
32
|
+
cache.clear();
|
|
33
|
+
expect(cache.size).toBe(0);
|
|
34
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
35
|
+
expect(cache.get('key2')).toBeUndefined();
|
|
36
|
+
});
|
|
37
|
+
it('应该能够获取缓存大小', () => {
|
|
38
|
+
expect(cache.size).toBe(0);
|
|
39
|
+
cache.set('key1', 'value1');
|
|
40
|
+
expect(cache.size).toBe(1);
|
|
41
|
+
cache.set('key2', 'value2');
|
|
42
|
+
expect(cache.size).toBe(2);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('TTL 功能', () => {
|
|
46
|
+
it('应该在 TTL 过期后删除缓存项', () => {
|
|
47
|
+
cache.set('key1', 'value1', 500);
|
|
48
|
+
expect(cache.get('key1')).toBe('value1');
|
|
49
|
+
// 前进 600ms,超过 TTL
|
|
50
|
+
vi.advanceTimersByTime(600);
|
|
51
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
52
|
+
});
|
|
53
|
+
it('应该使用默认 TTL', () => {
|
|
54
|
+
cache.set('key1', 'value1');
|
|
55
|
+
expect(cache.get('key1')).toBe('value1');
|
|
56
|
+
// 前进 1100ms,超过默认 TTL (1000ms)
|
|
57
|
+
vi.advanceTimersByTime(1100);
|
|
58
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
it('应该能够获取剩余 TTL 时间', () => {
|
|
61
|
+
cache.set('key1', 'value1', 1000);
|
|
62
|
+
expect(cache.getTtl('key1')).toBe(1000);
|
|
63
|
+
vi.advanceTimersByTime(500);
|
|
64
|
+
expect(cache.getTtl('key1')).toBe(500);
|
|
65
|
+
vi.advanceTimersByTime(600);
|
|
66
|
+
expect(cache.getTtl('key1')).toBe(-1); // 已过期
|
|
67
|
+
});
|
|
68
|
+
it('不存在的键应该返回 -1 的 TTL', () => {
|
|
69
|
+
expect(cache.getTtl('nonexistent')).toBe(-1);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
describe('容量限制', () => {
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
cache.destroy();
|
|
75
|
+
cache = new TtlCache({ maxSize: 2, enableCleanup: false });
|
|
76
|
+
});
|
|
77
|
+
it('应该遵守最大容量限制', () => {
|
|
78
|
+
cache.set('key1', 'value1');
|
|
79
|
+
cache.set('key2', 'value2');
|
|
80
|
+
cache.set('key3', 'value3'); // 应该驱逐最旧的条目
|
|
81
|
+
expect(cache.size).toBe(2);
|
|
82
|
+
expect(cache.get('key1')).toBeUndefined(); // 最旧的条目被驱逐
|
|
83
|
+
expect(cache.get('key2')).toBe('value2');
|
|
84
|
+
expect(cache.get('key3')).toBe('value3');
|
|
85
|
+
});
|
|
86
|
+
it('应该能够获取容量信息', () => {
|
|
87
|
+
expect(cache.capacity).toBe(2);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe('getOrSet 功能', () => {
|
|
91
|
+
it('应该返回已存在的缓存值', async () => {
|
|
92
|
+
cache.set('key1', 'existing');
|
|
93
|
+
const factory = vi.fn(() => Promise.resolve('new'));
|
|
94
|
+
const result = await cache.getOrSet('key1', factory);
|
|
95
|
+
expect(result).toBe('existing');
|
|
96
|
+
expect(factory).not.toHaveBeenCalled();
|
|
97
|
+
});
|
|
98
|
+
it('应该在缓存不存在时调用工厂函数并缓存结果', async () => {
|
|
99
|
+
const factory = vi.fn(() => Promise.resolve('new'));
|
|
100
|
+
const result = await cache.getOrSet('key1', factory);
|
|
101
|
+
expect(result).toBe('new');
|
|
102
|
+
expect(factory).toHaveBeenCalledOnce();
|
|
103
|
+
expect(cache.get('key1')).toBe('new');
|
|
104
|
+
});
|
|
105
|
+
it('应该支持同步工厂函数', () => {
|
|
106
|
+
const factory = vi.fn(() => 'sync-value');
|
|
107
|
+
const result = cache.getOrSet('key1', factory);
|
|
108
|
+
expect(result).toBe('sync-value');
|
|
109
|
+
expect(factory).toHaveBeenCalledOnce();
|
|
110
|
+
expect(cache.get('key1')).toBe('sync-value');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
describe('集合操作', () => {
|
|
114
|
+
beforeEach(() => {
|
|
115
|
+
cache.set('key1', 'value1');
|
|
116
|
+
cache.set('key2', 'value2');
|
|
117
|
+
cache.set('expired', 'expired-value', 100);
|
|
118
|
+
vi.advanceTimersByTime(200); // 使 'expired' 过期
|
|
119
|
+
});
|
|
120
|
+
it('应该返回所有未过期的键', () => {
|
|
121
|
+
const keys = cache.keys();
|
|
122
|
+
expect(keys).toContain('key1');
|
|
123
|
+
expect(keys).toContain('key2');
|
|
124
|
+
expect(keys).not.toContain('expired');
|
|
125
|
+
});
|
|
126
|
+
it('应该返回所有未过期的值', () => {
|
|
127
|
+
const values = cache.values();
|
|
128
|
+
expect(values).toContain('value1');
|
|
129
|
+
expect(values).toContain('value2');
|
|
130
|
+
expect(values).not.toContain('expired-value');
|
|
131
|
+
});
|
|
132
|
+
it('应该返回所有未过期的键值对', () => {
|
|
133
|
+
const entries = cache.entries();
|
|
134
|
+
expect(entries).toContainEqual(['key1', 'value1']);
|
|
135
|
+
expect(entries).toContainEqual(['key2', 'value2']);
|
|
136
|
+
expect(entries).not.toContainEqual(['expired', 'expired-value']);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe('统计功能', () => {
|
|
140
|
+
it('应该跟踪命中和未命中', () => {
|
|
141
|
+
cache.set('key1', 'value1');
|
|
142
|
+
// 命中
|
|
143
|
+
cache.get('key1');
|
|
144
|
+
cache.get('key1');
|
|
145
|
+
// 未命中
|
|
146
|
+
cache.get('nonexistent');
|
|
147
|
+
const stats = cache.getStats();
|
|
148
|
+
expect(stats.hits).toBe(2);
|
|
149
|
+
expect(stats.misses).toBe(1);
|
|
150
|
+
expect(stats.hitRate).toBeCloseTo(2 / 3);
|
|
151
|
+
});
|
|
152
|
+
it('应该跟踪过期次数', () => {
|
|
153
|
+
cache.set('key1', 'value1', 100);
|
|
154
|
+
vi.advanceTimersByTime(200);
|
|
155
|
+
cache.get('key1'); // 触发过期
|
|
156
|
+
const stats = cache.getStats();
|
|
157
|
+
expect(stats.expiredCount).toBe(1);
|
|
158
|
+
});
|
|
159
|
+
it('应该能够重置统计信息', () => {
|
|
160
|
+
cache.get('nonexistent'); // 创建一些统计数据
|
|
161
|
+
cache.resetStats();
|
|
162
|
+
const stats = cache.getStats();
|
|
163
|
+
expect(stats.hits).toBe(0);
|
|
164
|
+
expect(stats.misses).toBe(0);
|
|
165
|
+
expect(stats.expiredCount).toBe(0);
|
|
166
|
+
expect(stats.hitRate).toBe(0);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
describe('清理功能', () => {
|
|
170
|
+
it('应该手动清理过期条目', () => {
|
|
171
|
+
cache.set('key1', 'value1', 100);
|
|
172
|
+
cache.set('key2', 'value2', 1000);
|
|
173
|
+
vi.advanceTimersByTime(200);
|
|
174
|
+
const cleaned = cache.cleanup();
|
|
175
|
+
expect(cleaned).toBe(1);
|
|
176
|
+
expect(cache.size).toBe(1);
|
|
177
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
178
|
+
expect(cache.get('key2')).toBe('value2');
|
|
179
|
+
});
|
|
180
|
+
it('应该启用自动清理', () => {
|
|
181
|
+
cache.destroy();
|
|
182
|
+
cache = new TtlCache({
|
|
183
|
+
defaultTtl: 1000,
|
|
184
|
+
cleanupInterval: 500,
|
|
185
|
+
enableCleanup: true
|
|
186
|
+
});
|
|
187
|
+
cache.set('key1', 'value1', 100);
|
|
188
|
+
vi.advanceTimersByTime(600); // 触发清理
|
|
189
|
+
expect(cache.size).toBe(0);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
describe('配置验证', () => {
|
|
193
|
+
it('应该拒绝无效的默认 TTL', () => {
|
|
194
|
+
expect(() => new TtlCache({ defaultTtl: 0 })).toThrow('defaultTtl must be greater than 0');
|
|
195
|
+
expect(() => new TtlCache({ defaultTtl: -1 })).toThrow('defaultTtl must be greater than 0');
|
|
196
|
+
});
|
|
197
|
+
it('应该拒绝无效的清理间隔', () => {
|
|
198
|
+
expect(() => new TtlCache({ cleanupInterval: 0 })).toThrow('cleanupInterval must be greater than 0');
|
|
199
|
+
expect(() => new TtlCache({ cleanupInterval: -1 })).toThrow('cleanupInterval must be greater than 0');
|
|
200
|
+
});
|
|
201
|
+
it('应该拒绝无效的最大容量', () => {
|
|
202
|
+
expect(() => new TtlCache({ maxSize: 0 })).toThrow('maxSize must be greater than 0');
|
|
203
|
+
expect(() => new TtlCache({ maxSize: -1 })).toThrow('maxSize must be greater than 0');
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
describe('边界情况', () => {
|
|
207
|
+
it('应该处理空字符串键', () => {
|
|
208
|
+
cache.set('', 'empty-key-value');
|
|
209
|
+
expect(cache.get('')).toBe('empty-key-value');
|
|
210
|
+
});
|
|
211
|
+
it('应该处理 undefined 值', () => {
|
|
212
|
+
cache.set('key1', undefined);
|
|
213
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
214
|
+
expect(cache.has('key1')).toBe(true); // 值虽然是 undefined,但键存在
|
|
215
|
+
});
|
|
216
|
+
it('应该处理非常短的 TTL', () => {
|
|
217
|
+
cache.set('key1', 'value1', 1);
|
|
218
|
+
vi.advanceTimersByTime(2);
|
|
219
|
+
expect(cache.get('key1')).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|