MCPcopy Create free account
hub / github.com/KartikLabhshetwar/foliox / createCachedFunction

Function createCachedFunction

lib/utils/cache.ts:123–148  ·  view source on GitHub ↗
(
  fn: () => Promise<T>,
  keyParts: string[],
  options: CacheOptions = {}
)

Source from the content-addressed store, hash-verified

121}
122
123export function createCachedFunction<T>(
124 fn: () => Promise<T>,
125 keyParts: string[],
126 options: CacheOptions = {}
127) {
128 if (keyParts.length === 0) {
129 throw new Error('keyParts must contain at least one element');
130 }
131 const [prefix, ...parts] = keyParts;
132 const cacheKey = getCacheKey(prefix, ...parts);
133
134 return async (): Promise<T> => {
135 if (!Settings.CACHE_ENABLED) {
136 return fn();
137 }
138
139 const cached = await getCache<T>(cacheKey);
140 if (cached !== null) {
141 return cached;
142 }
143
144 const result = await fn();
145 await setCache(cacheKey, result, options);
146 return result;
147 };
148}
149

Callers 3

getCachedProjectsFunction · 0.90
getCachedContributionsFunction · 0.90
getCachedLinkedInProfileFunction · 0.90

Calls 2

getCacheKeyFunction · 0.85
setCacheFunction · 0.85

Tested by

no test coverage detected