-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Implement EntityCache as replacement for DepTrackingCache and ObjectCache. #5197
Implement EntityCache as replacement for DepTrackingCache and ObjectCache. #5197
Conversation
This could be a breaking change, since the exports of objectCache.ts were previously re-exported by index.ts, whereas now we re-export the exports of entityCache.ts.
book: { | ||
__typename: 'Book', | ||
isbn: '9781451673319', | ||
title: 'Fahrenheit 451', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extremely trivial fun fact: this completely authentic ISBN number for Fahrenheit 451 contains 451 as adjacent digits. 📚🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @benjamn! 👍
Merging this so that @hwillson can go ahead and work on restructuring the repo starting from the |
The result caching system introduced by #3394 gained the ability to cache optimistic results (rather than just non-optimistic results) in #5197, but since then has suffered from unnecessary cache key diversity during optimistic updates, because every EntityStore.Layer object (corresponding to a single optimistic update) counts as a distinct cache key, which prevents cached results from being reused if they were originally read from a different Layer object. This commit introduces the concept of a CacheGroup, store.group, which manages dependency tracking and also serves as a source of keys for the result caching system. While the Root object has its own CacheGroup, Layer objects share a CacheGroup object, which is the key to limiting diversity of cache keys when more than one optimistic update is pending. This separation allows the InMemoryCache to enjoy the full benefits of result caching for both optimistic (Layer) and non-optimistic (Root) data, separately.
The result caching system introduced by #3394 gained the ability to cache optimistic results (rather than just non-optimistic results) in #5197, but since then has suffered from unnecessary cache key diversity during optimistic updates, because every EntityStore.Layer object (corresponding to a single optimistic update) counts as a distinct cache key, which prevents cached results from being reused if they were originally read from a different Layer object. This commit introduces the concept of a CacheGroup, store.group, which manages dependency tracking and also serves as a source of keys for the result caching system. While the Root object has its own CacheGroup, Layer objects share a CacheGroup object, which is the key to limiting diversity of cache keys when more than one optimistic update is pending. This separation allows the InMemoryCache to enjoy the full benefits of result caching for both optimistic (Layer) and non-optimistic (Root) data, separately.
The new
EntityCache
is like the oldDepTrackingCache
in that it can track dependencies to enable caching repeated reads (#3394). However, theEntityCache
also incorporates theOptimisticCacheLayer
logic (#4319) that was previously implemented ininMemoryCache.ts
, thereby providing the same result caching benefits for optimistic cache reads, while preserving the possibility of reading non-optimistic data while optimistic data are still active.The
EntityCache
also removes the need for both theObjectCache
and theMapCache
, which were alternate cache implementations one could theoretically use instead of the defaultDepTrackingCache
.By moving optimistic caching logic into the same area of the code that handles non-optimistic caching, this PR sets the stage for garbage collection (see also #4681), since automatic garbage collection needs to take into account the full contents of the cache, including optimistic data.