MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / Cacheable

Function Cacheable

backend/api/src/postgres-proxy/utils/cache.util.ts:290–320  ·  view source on GitHub ↗
(
  keyGenerator: (...args: any[]) => string,
  ttl: number = 5 * 60 * 1000,
)

Source from the content-addressed store, hash-verified

288 * Cache decorator for method-level caching
289 */
290export 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

Callers 1

TestClassClass · 0.90

Calls 2

getMethod · 0.95
setMethod · 0.95

Tested by

no test coverage detected