refactor(util.cache): 优化定时清理过期缓存的定时器行为

新增定时器变量保存intervalId,并调用unref方法避免阻塞进程退出
This commit is contained in:
xiaojunnuo
2026-05-16 02:46:28 +08:00
parent c3baaf3ac7
commit 8483ee0d41
+2 -1
View File
@@ -11,9 +11,10 @@ export class LocalCache<V = any> {
cache: Map<string, { value: V; expiresAt: number }>;
constructor(opts: { clearInterval?: number } = {}) {
this.cache = new Map();
setInterval(() => {
const intervalId = setInterval(() => {
this.clearExpires();
}, opts.clearInterval ?? 5 * 60 * 1000);
intervalId.unref?.();
}
get(key: string): V | undefined {