UNPKG

toto-agent

Version:

Chatbot agent and reusable components for Toto platform

93 lines (92 loc) 2.03 kB
/** * Cache Service for toto-agent * Provides in-memory and persistent caching for performance optimization */ export interface CacheConfig { maxSize: number; ttl: number; enablePersistentCache: boolean; cacheDirectory?: string; } export interface CacheEntry<T = any> { key: string; value: T; timestamp: number; ttl: number; accessCount: number; lastAccessed: number; } export declare class CacheService { private cache; private config; private stats; constructor(config: CacheConfig); private cleanupInterval; /** * Get value from cache */ get<T>(key: string): T | null; /** * Set value in cache */ set<T>(key: string, value: T, ttl?: number): void; /** * Check if key exists in cache */ has(key: string): boolean; /** * Delete key from cache */ delete(key: string): boolean; /** * Clear all cache entries */ clear(): void; /** * Get cache statistics */ getStats(): { size: number; maxSize: number; hitRate: number; hits: number; misses: number; sets: number; evictions: number; }; /** * Get cache keys */ keys(): string[]; /** * Get cache size */ size(): number; /** * Evict least recently used entry */ private evictLRU; /** * Cleanup expired entries */ private cleanup; /** * Cache with automatic key generation */ cacheFunction<T extends any[], R>(fn: (...args: T) => Promise<R>, keyGenerator?: (...args: T) => string): (...args: T) => Promise<R>; /** * Cache with TTL */ cacheWithTTL<T>(key: string, fn: () => Promise<T>, ttl: number): Promise<T>; /** * Warm up cache with common queries */ warmup(commonQueries: Array<{ key: string; fn: () => Promise<any>; }>): Promise<void>; /** * Destroy cache and cleanup */ destroy(): void; }