Repository navigation
fix(cache): correct cache refresh, invalidation and persistence - #108
Merged
Merged
Conversation
- Enhance caching logic in cache.ts and related specs. - Update caching documentation in caching.md. - Add new tests for IndexedDB and integration scenarios.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes several problems in the response cache, including stale or wrong data being served, crashes with
observe: 'events', and IndexedDB startup races. Adds two cache options, configurable per API and per request. The cache API stays synchronous; custom caches keep working without changes.New options
Both can be set in the API options and overridden on a single request:
ignoreCacheControlfalseCache-Control(no-store,no-cache,max-age). Fetch policies andVarystill apply.cacheInvalidation'entity-set''api'clears all of that API's responses.Persistent caches also gain:
ODataIndexedDBCache.ready()– waits until stored entries are loaded and pending writes are done. Optional on theODataCacheinterface.onErrorconstructor option – if storage fails, the cache keeps working in memory only and reports the error here. Without it, the error is raised.Fixes
Fetch policies
network-onlyandcache-and-networknow replace an existing entry with the fresh response; before, the old entry stayed.observe: 'events', progress events are passed through and only the final successful response is cached.no-cachenever touches the cache.Cache keys
withCredentials,ignoreCacheControl, and theAccept,Accept-LanguageandPreferheaders.|.$querybody share the plain GET's key.Server headers
Cache-Controlparsing handles whitespace, case and quotes. The most restrictive directive wins (no-store, thenno-cache, thenprivate, thenpublic); with repeatedmax-age, the shortest is used.no-storeorno-cache, or with aVaryon headers not in the key, are not reused. A refresh that returns one of these removes the old entry.Invalidation
id.cache-onlymiss fails only its own request; the batch is still sent.Expiry and matching
maxAge: 0is stale right away.size()counts only live entries.maxAgevalues throw aRangeError.forget()matches scopes and names on separator boundaries (Peopleno longer matchesPeopleArchive) and doesn't modify the arrays passed in.Storage backends
{}.flush()only clears this cache's own prefix (odatano longer clearsodata2).forget()andflush()called before loading finishes are applied in order, without bringing back older entries.ready().flush()andforget()still remove stored data.Behavior changes
put()under your own names are unaffected.maxAgevalues now throw instead of creating entries with odd lifetimes.onErrorthey reach the request's error path anderrorHandler.cacheInvalidation: 'api', or clear them yourself.Tests and docs
cache/integration.spec.ts(API, fetch policies, invalidation, batch) andcache/indexeddb.spec.ts. The existing cache and option specs are extended.fake-indexeddbas a dev dependency.npm test: 856 tests pass. The production build (npm run build) passes.handbook/caching.mdandhandbook/configuration.md.