UNPKG

@bililive-tools/manager

Version:
76 lines (75 loc) 1.72 kB
/** * Cache system for RecorderManager * 提供统一的缓存接口用于处理持久化事务 */ /** * 内存缓存实现 */ export class MemoryCacheStore { store = new Map(); async get(key) { const item = this.store.get(key); if (!item) return undefined; if (item.expireAt && Date.now() > item.expireAt) { this.store.delete(key); return undefined; } return item.value; } async set(key, value, ttl) { const item = { value }; if (ttl) { item.expireAt = Date.now() + ttl; } this.store.set(key, item); } async delete(key) { this.store.delete(key); } async clear() { this.store.clear(); } async has(key) { const value = await this.get(key); return value !== undefined; } } /** * RecorderCache 实现 */ export class RecorderCacheImpl { store; constructor(store) { this.store = store; } createNamespace(recorderId) { return new NamespacedCacheImpl(this.store, `recorder:${recorderId}`); } global() { return new NamespacedCacheImpl(this.store, "global"); } } /** * 命名空间缓存实现 */ class NamespacedCacheImpl { store; namespace; constructor(store, namespace) { this.store = store; this.namespace = namespace; } getKey(key) { return `${this.namespace}:${key}`; } async get(key) { return this.store.get(this.getKey(key)); } async set(key, value) { return this.store.set(this.getKey(key), value); } async delete(key) { return this.store.delete(this.getKey(key)); } }