Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import NodeCache from 'node-cache'; interface CacheOptions { /** Default TTL in seconds */ ttl?: number; /** Check period for expired keys in seconds */ checkperiod?: number; } interface CacheStats { hits: number; misses: number; keys: number; ksize: number; vsize: number; hitRate: number; } /** * In-memory cache service using node-cache * Provides caching for database queries and computed values */ class CacheService { private cache: NodeCache; private enabled: boolean; constructor(options: CacheOptions = {}) { this.cache = new NodeCache({ stdTTL: options.ttl || 300, // 5 minutes default checkperiod: options.checkperiod || 60, useClones: false, // Better performance, but be careful with mutations }); // Can be disabled via environment variable this.enabled = process.env.DISABLE_CACHE !== 'true'; } /** * Get a value from cache */ get<T>(key: string): T | undefined { if (!this.enabled) return undefined; return this.cache.get<T>(key); } /** * Set a value in cache */ set<T>(key: string, value: T, ttl?: number): boolean { if (!this.enabled) return false; if (ttl !== undefined) { return this.cache.set(key, value, ttl); } return this.cache.set(key, value); } /** * Get a value from cache, or set it using the fetcher function * This is the primary method for caching database queries */ async getOrSet<T>( key: string, fetcher: () => Promise<T>, ttl?: number ): Promise<T> { if (!this.enabled) { return fetcher(); } const cached = this.get<T>(key); if (cached !== undefined) { return cached; } const value = await fetcher(); this.set(key, value, ttl); return value; } /** * Check if a key exists in cache */ has(key: string): boolean { return this.cache.has(key); } /** * Delete a specific key from cache */ delete(key: string): number { return this.cache.del(key); } /** * Delete multiple keys from cache */ deleteMany(keys: string[]): number { return this.cache.del(keys); } /** * Delete all keys matching a pattern (regex) */ deletePattern(pattern: string): number { const keys = this.cache.keys(); const regex = new RegExp(pattern); const matchingKeys = keys.filter(key => regex.test(key)); return this.cache.del(matchingKeys); } /** * Clear all cached data */ flush(): void { this.cache.flushAll(); } /** * Get all cached keys */ keys(): string[] { return this.cache.keys(); } /** * Get cache statistics */ getStats(): CacheStats { const stats = this.cache.getStats(); const total = stats.hits + stats.misses; return { hits: stats.hits, misses: stats.misses, keys: stats.keys, ksize: stats.ksize, vsize: stats.vsize, hitRate: total > 0 ? stats.hits / total : 0, }; } /** * Get remaining TTL for a key in seconds */ getTtl(key: string): number | undefined { return this.cache.getTtl(key); } /** * Enable or disable caching at runtime */ setEnabled(enabled: boolean): void { this.enabled = enabled; } /** * Check if caching is enabled */ isEnabled(): boolean { return this.enabled; } } // Singleton instance const globalForCache = globalThis as unknown as { cacheService: CacheService }; export const cacheService = globalForCache.cacheService || new CacheService(); if (process.env.NODE_ENV !== 'production') { globalForCache.cacheService = cacheService; } export { CacheService }; export type { CacheOptions, CacheStats }; |