@taicode/common-base 1.7.0 → 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 +133 -0
- package/output/batching-buffer/batching-buffer.d.ts.map +1 -0
- package/output/batching-buffer/batching-buffer.js +264 -0
- package/output/batching-buffer/batching-buffer.test.d.ts +2 -0
- package/output/batching-buffer/batching-buffer.test.d.ts.map +1 -0
- package/output/batching-buffer/batching-buffer.test.js +358 -0
- package/output/batching-buffer/index.d.ts +3 -0
- package/output/batching-buffer/index.d.ts.map +1 -0
- package/output/batching-buffer/index.js +1 -0
- package/output/index.d.ts +3 -0
- package/output/index.d.ts.map +1 -1
- package/output/index.js +4 -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
|
@@ -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
|
+
});
|