- 数据库表规范化与原子事务版本迁移机制 (_schema_migrations) - 增加 SQLite 复合主键、PRAGMA busy_timeout 与索引覆盖 - 构建统一两级协同缓存 (L1 Memory LRU + L2 SQLite 持久化) - withCache 增加 In-flight Promise 去重并发防击穿 (单飞机制) - 收敛 dimension、role、bill 与 token-store 缓存逻辑 - 100% 保持现有 API 契约与零功能删除
114 lines
2.9 KiB
TypeScript
114 lines
2.9 KiB
TypeScript
import { MemoryCache } from "./memory.js";
|
|
import { SqliteCache } from "./sqlite.js";
|
|
import type { CacheOptions, ICache, WithCacheOptions } from "./types.js";
|
|
|
|
export interface MultiLevelCacheOptions {
|
|
l1?: MemoryCache;
|
|
l2?: SqliteCache | null;
|
|
}
|
|
|
|
export class MultiLevelCache implements ICache {
|
|
private l1: MemoryCache;
|
|
private l2: SqliteCache | null;
|
|
// MAJ-01: 防击穿 / 单飞并发请求去重表 (In-flight Promise Deduplication)
|
|
private inFlight = new Map<string, Promise<any>>();
|
|
|
|
constructor(options?: MultiLevelCacheOptions) {
|
|
this.l1 = options?.l1 ?? new MemoryCache();
|
|
this.l2 = options?.l2 !== undefined ? options.l2 : new SqliteCache();
|
|
}
|
|
|
|
public get<T>(key: string, namespace = "default"): T | undefined {
|
|
// 1. 优先从 L1 (Memory) 获取
|
|
const l1Value = this.l1.get<T>(key, namespace);
|
|
if (l1Value !== undefined) {
|
|
return l1Value;
|
|
}
|
|
|
|
// 2. L1 穿透,查询 L2 (SQLite 持久化)
|
|
if (this.l2) {
|
|
const l2Entry = this.l2.getEntry<T>(key, namespace);
|
|
if (l2Entry !== undefined) {
|
|
// 回填至 L1,保持相同的到期时间与命名空间
|
|
this.l1.setEntry(key, l2Entry);
|
|
return l2Entry.value;
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
public set<T>(key: string, value: T, options?: CacheOptions): void {
|
|
// 同时写入 L1 与 L2
|
|
this.l1.set(key, value, options);
|
|
if (this.l2) {
|
|
this.l2.set(key, value, options);
|
|
}
|
|
}
|
|
|
|
public delete(key: string, namespace = "default"): void {
|
|
this.l1.delete(key, namespace);
|
|
if (this.l2) {
|
|
this.l2.delete(key, namespace);
|
|
}
|
|
}
|
|
|
|
public clear(namespace?: string): void {
|
|
this.l1.clear(namespace);
|
|
if (this.l2) {
|
|
this.l2.clear(namespace);
|
|
}
|
|
}
|
|
|
|
public has(key: string, namespace = "default"): boolean {
|
|
return this.get(key, namespace) !== undefined;
|
|
}
|
|
|
|
/**
|
|
* MAJ-01: 业务透明包装器,自动多级缓存查验 + In-flight 请求合并防击穿
|
|
*/
|
|
public async withCache<T>(
|
|
key: string,
|
|
fetcher: () => Promise<T>,
|
|
options?: WithCacheOptions
|
|
): Promise<T> {
|
|
const namespace = options?.namespace ?? "default";
|
|
const lockKey = `${namespace}:${key}`;
|
|
|
|
if (!options?.forceRefresh) {
|
|
const cached = this.get<T>(key, namespace);
|
|
if (cached !== undefined) {
|
|
return cached;
|
|
}
|
|
}
|
|
|
|
// 防击穿:检查是否已有相同命名空间与键的并发请求在执行中
|
|
const ongoing = this.inFlight.get(lockKey);
|
|
if (ongoing) {
|
|
return ongoing as Promise<T>;
|
|
}
|
|
|
|
const promise = (async () => {
|
|
try {
|
|
const fresh = await fetcher();
|
|
this.set(key, fresh, options);
|
|
return fresh;
|
|
} finally {
|
|
this.inFlight.delete(lockKey);
|
|
}
|
|
})();
|
|
|
|
this.inFlight.set(lockKey, promise);
|
|
return promise;
|
|
}
|
|
|
|
/**
|
|
* 清理过期条目(MIN-11: L1 与 L2 同步清理)
|
|
*/
|
|
public pruneExpired(): void {
|
|
if (this.l2) {
|
|
this.l2.pruneExpired();
|
|
}
|
|
}
|
|
}
|