Files
hose-use/src/core/cache/memory.ts
T
gitea 3a15b064fe refactor(core): 规范数据库表结构、版本迁移与多级缓存体系加固
- 数据库表规范化与原子事务版本迁移机制 (_schema_migrations)
- 增加 SQLite 复合主键、PRAGMA busy_timeout 与索引覆盖
- 构建统一两级协同缓存 (L1 Memory LRU + L2 SQLite 持久化)
- withCache 增加 In-flight Promise 去重并发防击穿 (单飞机制)
- 收敛 dimension、role、bill 与 token-store 缓存逻辑
- 100% 保持现有 API 契约与零功能删除
2026-09-08 15:49:27 +08:00

144 lines
3.9 KiB
TypeScript

import type { CacheEntry, CacheOptions, ICache } from "./types.js";
const DEFAULT_TTL_MS = 10 * 60 * 1000; // 10 分钟
const DEFAULT_MAX_ENTRIES = 2000;
export class MemoryCache implements ICache {
// Map 保持键的插入顺序,结合重新插入机制实现高效的 O(1) LRU 淘汰
private map = new Map<string, CacheEntry<any>>();
private maxEntries: number;
constructor(maxEntries = DEFAULT_MAX_ENTRIES) {
this.maxEntries = maxEntries;
}
/**
* CRIT-02: 构建命名空间隔离的复合内存键,彻底规避跨域碰撞
*/
private toCompositeKey(key: string, namespace?: string): string {
const ns = namespace || "default";
return `${ns}:${key}`;
}
public get<T>(key: string, namespace = "default"): T | undefined {
const compositeKey = this.toCompositeKey(key, namespace);
const entry = this.map.get(compositeKey);
if (!entry) return undefined;
const now = Date.now();
if (entry.expiresAt <= now) {
this.map.delete(compositeKey);
return undefined;
}
// MAJ-09: 真正 LRU 访问刷新 —— 命中后移至 Map 末尾
this.map.delete(compositeKey);
this.map.set(compositeKey, entry);
return entry.value as T;
}
public getEntry<T>(key: string, namespace = "default"): CacheEntry<T> | undefined {
const compositeKey = this.toCompositeKey(key, namespace);
const entry = this.map.get(compositeKey);
if (!entry) return undefined;
const now = Date.now();
if (entry.expiresAt <= now) {
this.map.delete(compositeKey);
return undefined;
}
// 命中后移至 Map 末尾保持热度
this.map.delete(compositeKey);
this.map.set(compositeKey, entry);
return entry as CacheEntry<T>;
}
public set<T>(key: string, value: T, options?: CacheOptions): void {
const namespace = options?.namespace ?? "default";
const compositeKey = this.toCompositeKey(key, namespace);
const now = Date.now();
const ttl = options?.ttlMs ?? DEFAULT_TTL_MS;
const entry: CacheEntry<T> = {
value,
expiresAt: now + ttl,
updatedAt: now,
namespace,
};
// MAJ-08: 更新已有键时,先删后插调至末尾,不应误触发 LRU 淘汰
if (this.map.has(compositeKey)) {
this.map.delete(compositeKey);
this.map.set(compositeKey, entry);
return;
}
// 若达到容量限制,执行 LRU 淘汰
if (this.map.size >= this.maxEntries) {
this.evict();
}
this.map.set(compositeKey, entry);
}
public setEntry<T>(key: string, entry: CacheEntry<T>): void {
const compositeKey = this.toCompositeKey(key, entry.namespace);
if (this.map.has(compositeKey)) {
this.map.delete(compositeKey);
} else if (this.map.size >= this.maxEntries) {
this.evict();
}
this.map.set(compositeKey, entry);
}
public delete(key: string, namespace = "default"): void {
const compositeKey = this.toCompositeKey(key, namespace);
this.map.delete(compositeKey);
}
public clear(namespace?: string): void {
if (!namespace) {
this.map.clear();
return;
}
const prefix = `${namespace}:`;
for (const k of this.map.keys()) {
if (k.startsWith(prefix)) {
this.map.delete(k);
}
}
}
public has(key: string, namespace = "default"): boolean {
return this.get(key, namespace) !== undefined;
}
public size(): number {
return this.map.size;
}
private evict(): void {
const now = Date.now();
// 1. 优先剔除已过期的死键
for (const [k, v] of this.map.entries()) {
if (v.expiresAt <= now) {
this.map.delete(k);
}
}
// 2. 若仍然超限,按真正的 LRU(头部为最久未访问的条目)淘汰最早的条目
while (this.map.size >= this.maxEntries) {
const oldestKey = this.map.keys().next().value;
if (oldestKey !== undefined) {
this.map.delete(oldestKey);
} else {
break;
}
}
}
}