import { KeyValueStore } from '../types'; /** * In-memory store implementation (for development/testing only) * DO NOT USE IN PRODUCTION */ export class InMemoryStore implements KeyValueStore { private data: Map = new Map(); private cleanupInterval: NodeJS.Timeout; constructor() { // Cleanup expired keys every minute this.cleanupInterval = setInterval(() => this.cleanup(), 60000); } async get(key: string): Promise { const item = this.data.get(key); if (!item) { return null; } // Check expiration if (item.expiresAt && item.expiresAt < Date.now()) { this.data.delete(key); return null; } return item.value; } async set(key: string, value: string, ttl?: number): Promise { const item: { value: string; expiresAt?: number } = { value }; if (ttl) { item.expiresAt = Date.now() + ttl * 1000; } this.data.set(key, item); } async delete(key: string): Promise { this.data.delete(key); } async scan(pattern: string): Promise { // Simple glob pattern matching const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$'); const keys: string[] = []; for (const key of this.data.keys()) { if (regex.test(key)) { keys.push(key); } } return keys; } /** * Cleanup expired keys */ private cleanup(): void { const now = Date.now(); for (const [key, item] of this.data.entries()) { if (item.expiresAt && item.expiresAt < now) { this.data.delete(key); } } } /** * Clear all data */ clear(): void { this.data.clear(); } /** * Destroy store and cleanup interval */ destroy(): void { clearInterval(this.cleanupInterval); this.clear(); } }