Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoCleanedCache: only schedule batched cache cleanup if the cache is full #11792

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-emus-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

AutoCleanedCache: only schedule batched cache cleanup if the cache is full (fixes #11790)
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 18 additions & 9 deletions src/utilities/caching/caches.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { CommonCache } from "@wry/caches";
import { WeakCache, StrongCache } from "@wry/caches";

const scheduledCleanup = new WeakSet<CommonCache<any, any>>();
function schedule(cache: CommonCache<any, any>) {
interface CleanableCache {
size: number;
max?: number;
clean: () => void;
}
const scheduledCleanup = new WeakSet<CleanableCache>();
function schedule(cache: CleanableCache) {
if (cache.size <= (cache.max || -1)) {
return;
}
if (!scheduledCleanup.has(cache)) {
scheduledCleanup.add(cache);
setTimeout(() => {
Expand All @@ -14,7 +21,7 @@ function schedule(cache: CommonCache<any, any>) {
/**
* @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
Expand All @@ -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;
Expand All @@ -48,7 +56,7 @@ export type AutoCleanedWeakCache<K extends object, V> = WeakCache<K, V>;
/**
* @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
Expand All @@ -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;
Expand Down