From 5876c35530a21473207954d1f0c2b7dd00c0b9ea Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Wed, 24 Apr 2024 20:06:24 +0200 Subject: [PATCH] AutoCleanedCache: only schedule batched cache cleanup if the cache is full (#11792) * AutoCleanedCache: only schedule batched cache cleanup if the cache is full Fixes #11790 This prevents timeouts from being set when they are not necessary. * update size limit * trigger CI * Clean up Prettier, Size-limit, and Api-Extractor --------- Co-authored-by: phryneas --- .changeset/cuddly-emus-fail.md | 5 +++++ .size-limits.json | 4 ++-- src/utilities/caching/caches.ts | 27 ++++++++++++++++++--------- 3 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 .changeset/cuddly-emus-fail.md diff --git a/.changeset/cuddly-emus-fail.md b/.changeset/cuddly-emus-fail.md new file mode 100644 index 00000000000..d3830efd702 --- /dev/null +++ b/.changeset/cuddly-emus-fail.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +AutoCleanedCache: only schedule batched cache cleanup if the cache is full (fixes #11790) diff --git a/.size-limits.json b/.size-limits.json index 86e387b30fb..b18103fdaec 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 39534, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32804 + "dist/apollo-client.min.cjs": 39551, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32826 } diff --git a/src/utilities/caching/caches.ts b/src/utilities/caching/caches.ts index 6acbdb88d34..a42cd24a06d 100644 --- a/src/utilities/caching/caches.ts +++ b/src/utilities/caching/caches.ts @@ -1,8 +1,15 @@ -import type { CommonCache } from "@wry/caches"; import { WeakCache, StrongCache } from "@wry/caches"; -const scheduledCleanup = new WeakSet>(); -function schedule(cache: CommonCache) { +interface CleanableCache { + size: number; + max?: number; + clean: () => void; +} +const scheduledCleanup = new WeakSet(); +function schedule(cache: CleanableCache) { + if (cache.size <= (cache.max || -1)) { + return; + } if (!scheduledCleanup.has(cache)) { scheduledCleanup.add(cache); setTimeout(() => { @@ -14,7 +21,7 @@ function schedule(cache: CommonCache) { /** * @internal * A version of WeakCache that will auto-schedule a cleanup of the cache when - * a new item is added. + * a new item is added and the cache reached maximum size. * Throttled to once per 100ms. * * @privateRemarks @@ -35,8 +42,9 @@ export const AutoCleanedWeakCache = function ( */ const cache = new WeakCache(max, dispose); cache.set = function (key: any, value: any) { - schedule(this); - return WeakCache.prototype.set.call(this, key, value); + const ret = WeakCache.prototype.set.call(this, key, value); + schedule(this as any as CleanableCache); + return ret; }; return cache; } as any as typeof WeakCache; @@ -48,7 +56,7 @@ export type AutoCleanedWeakCache = WeakCache; /** * @internal * A version of StrongCache that will auto-schedule a cleanup of the cache when - * a new item is added. + * a new item is added and the cache reached maximum size. * Throttled to once per 100ms. * * @privateRemarks @@ -69,8 +77,9 @@ export const AutoCleanedStrongCache = function ( */ const cache = new StrongCache(max, dispose); cache.set = function (key: any, value: any) { - schedule(this); - return StrongCache.prototype.set.call(this, key, value); + const ret = StrongCache.prototype.set.call(this, key, value); + schedule(this as any as CleanableCache); + return ret; }; return cache; } as any as typeof StrongCache;