Skip to content

Commit

Permalink
AutoCleanedCache: only schedule batched cache cleanup if the cache is…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
phryneas and phryneas authored Apr 24, 2024
1 parent bf9dd17 commit 5876c35
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
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

0 comments on commit 5876c35

Please sign in to comment.