MCPcopy
hub / github.com/middyjs/middy / processCache

Function processCache

packages/util/index.js:606–670  ·  view source on GitHub ↗
(
	options,
	middlewareFetch = () => undefined,
	middlewareFetchRequest = {},
)

Source from the content-addressed store, hash-verified

604 : undefined;
605
606export const processCache = (
607 options,
608 middlewareFetch = () => undefined,
609 middlewareFetchRequest = {},
610) => {
611 let { cacheKey, cacheKeyExpiry, cacheExpiry, cacheMaxSize } = options;
612 cacheMaxSize ??= defaultCacheMaxSize;
613 cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry;
614 validateCacheExpiry(cacheExpiry);
615 const now = Date.now();
616 if (cacheExpiry) {
617 const cached = getCache(cacheKey);
618 const effectiveExpiry =
619 cacheExpiry > 86400000 ? cacheExpiry : cached.expiry;
620 const unexpired =
621 // Stryker disable next-line ConditionalExpression,EqualityOperator: the `cacheExpiry < 0` branch is equivalent. cacheExpiry === 0 is unreachable (guarded by `if (cacheExpiry)`), and for the only negative value -1 the stored expiry is Infinity so `effectiveExpiry > now` is already always true. (-1 -> 0 boundary cannot be observed.)
622 cached.expiry && (cacheExpiry < 0 || effectiveExpiry > now);
623
624 if (unexpired) {
625 if (cached.modified) {
626 const value = middlewareFetch(middlewareFetchRequest, cached.value);
627 silenceFetchRejections(value);
628 Object.assign(cached.value, value);
629 const refresh = scheduleRefresh(
630 cached.expiry - now,
631 options,
632 middlewareFetch,
633 middlewareFetchRequest,
634 );
635 const entry = { value: cached.value, expiry: cached.expiry, refresh };
636 cache.set(cacheKey, entry);
637 return entry;
638 }
639 cached.cache = true;
640 return cached;
641 }
642 }
643 const value = middlewareFetch(middlewareFetchRequest);
644 silenceFetchRejections(value);
645 // cacheExpiry semantics:
646 // >86400000 (24h): treated as unix timestamp (ms)
647 // >0 && <=86400000: treated as duration (ms) from now
648 // -1: infinite cache (never expires)
649 // 0/undefined/null: no caching
650 const expiry =
651 cacheExpiry < 0
652 ? Number.POSITIVE_INFINITY
653 : cacheExpiry > 86400000
654 ? cacheExpiry
655 : now + cacheExpiry;
656 // Stryker disable next-line EqualityOperator: the `> 86400000` -> `>= 86400000` change is equivalent for the refresh duration. It only differs at cacheExpiry === 86400000, where it shifts the auto-refresh timer by `now` ms; the refresh callback re-validates the (separately-set) expiry, so the same number of fetches occur and no observable difference results.
657 const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
658 if (cacheExpiry) {
659 clearTimeout(cache.get(cacheKey)?.refresh);
660 const refresh = scheduleRefresh(
661 duration,
662 options,
663 middlewareFetch,

Callers 15

appConfigMiddlewareFunction · 0.90
secretsManagerMiddlewareFunction · 0.90
resolveSchemaVersionFunction · 0.90
kmsMiddlewareFunction · 0.90
kmsMiddlewareBeforeFunction · 0.90
dynamodbMiddlewareFunction · 0.90

Calls 5

validateCacheExpiryFunction · 0.85
getCacheFunction · 0.85
silenceFetchRejectionsFunction · 0.85
scheduleRefreshFunction · 0.85
evictCacheFunction · 0.85

Tested by

no test coverage detected