MCPcopy Create free account
hub / github.com/aree6/LiftShift / getOrCompute

Method getOrCompute

frontend/utils/storage/computationCache.ts:63–96  ·  view source on GitHub ↗

* Get or compute a cached value

(
    key: string,
    data: unknown,
    computeFn: () => T,
    options?: { ttl?: number; forceRecompute?: boolean }
  )

Source from the content-addressed store, hash-verified

61 * Get or compute a cached value
62 */
63 getOrCompute<T>(
64 key: string,
65 data: unknown,
66 computeFn: () => T,
67 options?: { ttl?: number; forceRecompute?: boolean }
68 ): T {
69 const dataHash = this.hashData(data);
70 const cacheKey = `${key}:${dataHash}`;
71 const now = Date.now();
72 const ttl = options?.ttl ?? this.maxAge;
73
74 // Check if we have a valid cached entry
75 if (!options?.forceRecompute) {
76 const entry = this.cache.get(cacheKey);
77 if (entry && (now - entry.timestamp) < ttl) {
78 return entry.value as T;
79 }
80 }
81
82 // Compute new value
83 const value = computeFn();
84
85 // Store in cache
86 this.cache.set(cacheKey, {
87 value,
88 timestamp: now,
89 dataHash,
90 });
91
92 // Evict old entries if cache is too large
93 this.evictIfNeeded();
94
95 return value;
96 }
97
98 /**
99 * Get a cached value without computing (returns undefined if not cached)

Callers 15

useMuscleHeatmapDataFunction · 0.80
useMuscleTrendDataFunction · 0.80
DashboardFunction · 0.80
useDashboardPlateausFunction · 0.80
useDashboardTopExercisesFunction · 0.80
useDashboardMuscleTrendFunction · 0.80
useDashboardPrTrendFunction · 0.80
getCachedValueFunction · 0.80

Calls 4

hashDataMethod · 0.95
evictIfNeededMethod · 0.95
getMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected