mono/packages/shared/src/server/commons/cache/MemoryCache.ts
2026-01-29 18:14:47 +01:00

43 lines
1.3 KiB
TypeScript

import { LRUCache } from 'lru-cache';
import { CacheAdapter } from './types.js';
export class MemoryCache implements CacheAdapter {
private cache: LRUCache<string, any>;
constructor() {
const defaultTtl = process.env.CACHE_DEFAULT_TTL ? parseInt(process.env.CACHE_DEFAULT_TTL) : 1000 * 60 * 5; // 5 mins default
this.cache = new LRUCache({
max: 500,
ttl: defaultTtl,
updateAgeOnGet: false,
});
}
async get<T>(key: string): Promise<T | null> {
const value = this.cache.get(key);
return (value as T) || null;
}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
this.cache.set(key, value, { ttl: ttl ? ttl * 1000 : undefined });
}
async del(key: string): Promise<void> {
this.cache.delete(key);
}
async flush(pattern?: string): Promise<void> {
if (pattern) {
// Manual iteration for pattern matching (simple startsWith)
// LRUCache doesn't support regex keys easily without walking
for (const key of this.cache.keys()) {
if (typeof key === 'string' && key.startsWith(pattern)) {
this.cache.delete(key);
}
}
} else {
this.cache.clear();
}
}
}