Skip to content

Commit 1217211

Browse files
committed
perf: remove redundant “stale” check for cache reads as it's handled by timer yielded revalidator
1 parent bcd1fda commit 1217211

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

src/cache-manager.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,6 @@ function isCacheExpired(entry: CacheEntry<any>): boolean {
223223
return timeNow() > entry.expiry;
224224
}
225225

226-
/**
227-
* Checks if the cache entry is stale based on its timestamp and the stale time.
228-
*
229-
* @param {CacheEntry<any>} entry - The cache entry to check.
230-
* @returns {boolean} - Returns true if the cache entry is stale, false otherwise.
231-
*/
232-
function isCacheStale(entry: CacheEntry<any>): boolean {
233-
if (!entry.stale) {
234-
return false;
235-
}
236-
237-
return timeNow() > entry.stale;
238-
}
239-
240226
/**
241227
* Retrieves a cached response from the internal cache using the provided key.
242228
*
@@ -453,27 +439,15 @@ export function getCachedResponse<
453439
}
454440

455441
const isExpired = isCacheExpired(entry);
456-
const isStale = isCacheStale(entry);
457442

458443
// If completely expired, delete and return null
459444
if (isExpired) {
460445
deleteCache(cacheKey);
461446
return null;
462447
}
463448

464-
// If fresh (not stale), return immediately
465-
if (!isStale) {
466-
return entry.data;
467-
}
468-
469-
// SWR: Data is stale but not expired
470-
if (isStale && !isExpired) {
471-
// Triggering background revalidation here could cause race conditions
472-
// So we return stale data immediately and leave it up to implementers to handle revalidation
473-
return entry.data;
474-
}
475-
476-
return null;
449+
// Return data whether fresh or stale (SWR: serve stale, revalidation is timer-driven)
450+
return entry.data;
477451
}
478452

479453
/**

0 commit comments

Comments
 (0)