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

add resultCacheMaxSize #8107

Merged

Conversation

sofianhn
Copy link
Contributor

@sofianhn sofianhn commented May 1, 2021

Per the conversation in apollographql/apollo-feature-requests#289, this PR is proposing two changes:

  1. Add resultCachMaxSize config
    This will allow customizing the max size of caches used for resultCaching.
    const cache = new InMemoryCache({ resultCachMaxSize: 5000 });

  2. Add automatic clean up for the caches in executeSelectionSet and in executeSubSelectedArray upon eviction of related entries.

@apollo-cla
Copy link

@sofianhn: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@@ -0,0 +1,47 @@
diff --git a/node_modules/optimism/lib/bundle.cjs.js b/node_modules/optimism/lib/bundle.cjs.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the patched package is included here for reference, here is the upstream PR: benjamn/optimism#189

@sofianhn sofianhn changed the title Clear cache on eviction main add resultCachMaxSize & clear result caches on eviction May 2, 2021
@sofianhn
Copy link
Contributor Author

sofianhn commented May 2, 2021

I added a post install for npx patch-package to get CI to actually do the patching and run the test but it does not seem to have picked that up? Maybe caching the build?

For me running npm test passes locally.

@benjamn
Copy link
Member

benjamn commented May 3, 2021

@sofianhn Thanks for this! I/we will take a deeper look this week, but a couple things jumped out at me right away:

  1. Please rebase this branch against release-3.4, since it represents significant new (internal) functionality that we probably do not want to release in a patch version of Apollo Client, and some related parts of the code have changed in v3.4 that might be relevant to your goals.
  2. The name of the option has a small misspelling: shouldn't it be resultCacheMaxSize rather than resultCachMaxSize? You used the second spelling consistently, but I think most users will expect Cache to be spelled with an e.

@benjamn
Copy link
Member

benjamn commented May 3, 2021

On second thought @sofianhn, would you mind separating the resultCacheMaxSize changes out into a separate PR (ideally targeting release-3.4)? I think I see a much simpler (less code) way to implement the eviction cleanup, but I don't think that discussion needs to block the resultCacheMaxSize stuff. Looking forward to showing you what I have in mind!

@sofianhn
Copy link
Contributor Author

sofianhn commented May 3, 2021

Awesome, looking forward, I will split this to 2 PRs.

@sofianhn sofianhn force-pushed the clear-cache-on-eviction-main branch from 2ecbfb9 to 482f4d8 Compare May 4, 2021 00:47
@sofianhn sofianhn changed the base branch from main to release-3.4 May 4, 2021 00:48
@sofianhn sofianhn changed the title add resultCachMaxSize & clear result caches on eviction add resultCacheMaxSize May 4, 2021
@sofianhn
Copy link
Contributor Author

sofianhn commented May 4, 2021

On second thought @sofianhn, would you mind separating the resultCacheMaxSize changes out into a separate PR (ideally targeting release-3.4)? I think I see a much simpler (less code) way to implement the eviction cleanup, but I don't think that discussion needs to block the resultCacheMaxSize stuff. Looking forward to showing you what I have in mind!

This PR now tracks only adding resultCacheMaxSize. I am seeing the effects of #4141 (comment) and want dig deeper there before I put my second PR.

@@ -285,8 +324,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}

public reset(): Promise<void> {
this.optimisticData = this.optimisticData.prune();
this.data.clear();
this.init();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to this change, resetting the cache now dumps this.storeReader and all other result-caching-related data, and replaces them with empty data structures.

Comment on lines 100 to +103
constructor(private config: StoreReaderConfig) {
this.config = { addTypename: true, ...config };

this.executeSelectionSet = wrap(options => this.execSelectionSetImpl(options), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these initializations make sense to move to an init or reset method (like I did in InMemoryCache), since it's simpler to dump/replace the whole StoreReader object, and let the original one be garbage collected.

Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few tweaks!

Give me a day or two to whip up some optimism functionality that should make the rest of your original PR easier from the perspective of InMemoryCache. In particular, I think we can reuse the existing dep<TKey> concept, if we allow dep.forget(key) as well as dep.dirty(key), where forgetting causes the parent Entry to be forgotten (removed from the LRU cache, not just dirtied).

@benjamn benjamn merged commit d6ee940 into apollographql:release-3.4 May 5, 2021
@benjamn
Copy link
Member

benjamn commented May 5, 2021

These changes have been published in @apollo/client@3.4.0-beta.24 (just now), FYI.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants