| 288 | * Cache decorator for method-level caching |
| 289 | */ |
| 290 | export function Cacheable( |
| 291 | keyGenerator: (...args: any[]) => string, |
| 292 | ttl: number = 5 * 60 * 1000, |
| 293 | ) { |
| 294 | return function ( |
| 295 | target: any, |
| 296 | propertyName: string, |
| 297 | descriptor: PropertyDescriptor, |
| 298 | ) { |
| 299 | const method = descriptor.value; |
| 300 | const cache = new MemoryCache({ ttl }); |
| 301 | |
| 302 | descriptor.value = async function (...args: any[]) { |
| 303 | const cacheKey = keyGenerator(...args); |
| 304 | |
| 305 | // Try to get from cache |
| 306 | let result = cache.get(cacheKey); |
| 307 | if (result !== null) { |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | // Execute original method |
| 312 | result = await method.apply(this, args); |
| 313 | |
| 314 | // Cache the result |
| 315 | cache.set(cacheKey, result); |
| 316 | |
| 317 | return result; |
| 318 | }; |
| 319 | }; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Cache warming utility |