MCPcopy Create free account
hub / github.com/EBWi11/AgentSmith-HUB / fetchWithCache

Function fetchWithCache

web/src/stores/dataCache.js:277–323  ·  view source on GitHub ↗
(key, fetcher, ttl = 60000, forceRefresh = false)

Source from the content-addressed store, hash-verified

275
276 // Generic cache fetch method
277 async fetchWithCache(key, fetcher, ttl = 60000, forceRefresh = false) {
278 // Initialize event listeners on first use
279 this.initializeEventListeners()
280
281 const cacheKey = key
282 // If there is already an in-flight request for this key, return the same Promise
283 if (ongoingRequests.has(cacheKey)) {
284 return ongoingRequests.get(cacheKey)
285 }
286
287 const cache = this[key]
288
289 // If cache doesn't exist, return error
290 if (!cache) {
291 console.error(`Cache key '${key}' not found in dataCache store`)
292 throw new Error(`Cache key '${key}' not found`)
293 }
294
295 // If data not expired and not forcing refresh, return cached data
296 if (!forceRefresh && cache && !this.isExpired(key, ttl)) {
297 return cache.data
298 }
299
300 // Create a Promise for the fetcher and store it in the map to deduplicate
301 const requestPromise = (async () => {
302 cache.loading = true
303 try {
304 const data = await fetcher()
305 cache.data = data
306 cache.timestamp = Date.now()
307 return data
308 } catch (error) {
309 console.error(`Failed to fetch ${key}:`, error)
310 // If has cached data, return cached data
311 if (cache.data) {
312 return cache.data
313 }
314 throw error
315 } finally {
316 cache.loading = false
317 ongoingRequests.delete(cacheKey)
318 }
319 })()
320
321 ongoingRequests.set(cacheKey, requestPromise)
322 return requestPromise
323 },
324
325 // Get component data
326 async fetchComponents(type, forceRefresh = false, isPriorityRefresh = false) {

Callers

nothing calls this directly

Calls 2

getMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected