Skip to content

Documentation / @warp-drive/core / store / DefaultCachePolicy

Class: DefaultCachePolicy

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:496

A basic CachePolicy that can be added to the Store service.

Determines staleness based on time since the request was last received from the API using the date header.

Determines expiration based on configured constraints as well as a time based expiration strategy based on the date header.

In order expiration is determined by:

  • Is explicitly invalidated
  • ↳ (if null) isExpired function <IF Constraint Active>
  • ↳ (if null) X-WarpDrive-Expires header <IF Constraint Active>
  • ↳ (if null) Cache-Control header <IF Constraint Active>
  • ↳ (if null) Expires header <IF Constraint Active>
  • ↳ (if null) Date header + apiCacheHardExpires < current time

Invalidates any request for which cacheOptions.types was provided when a createRecord request for that type is successful.

For this to work, the createRecord request must include the cacheOptions.types array with the types that should be invalidated, or its request should specify the ResourceKeys of the records that are being created via records. Providing both is valid.

NOTE

only requests that had specified cacheOptions.types and occurred prior to the createRecord request will be invalidated. This means that a given request should always specify the types that would invalidate it to opt into this behavior. Abstracting this behavior via builders is recommended to ensure consistency.

This allows the Store's CacheHandler to determine if a request is expired and should be refetched upon next request.

The Fetch handler provided by @warp-drive/core will automatically add the date header to responses if it is not present.

NOTE

Date headers do not have millisecond precision, so expiration times should generally be larger than 1000ms.

Usage:

ts
import { Store } from '@warp-drive/core';
import { DefaultCachePolicy } from '@warp-drive/core/store';

export class AppStore extends Store {
  lifetimes = new DefaultCachePolicy({
    apiCacheSoftExpires: 30_000,
    apiCacheHardExpires: 60_000
  });
}

In Testing environments, the apiCacheSoftExpires will always be false and apiCacheHardExpires will use the apiCacheSoftExpires value.

This helps reduce flakiness and produce predictably rendered results in test suites.

Requests that specifically set cacheOptions.backgroundReload = true will still be background reloaded in tests.

This behavior can be opted out of by setting disableTestOptimization = true in the policy config.

Methods

_getStore()

ts
_getStore(store): object;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:500

Parameters

store

Store

Returns

object

invalidated
ts
invalidated: Set<RequestKey>;
types
ts
types: Map<string, Set<RequestKey>>;

didRequest()

ts
didRequest(
   request, 
   response, 
   cacheKey, 
   store): void;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:596

Invoked when a request has been fulfilled from the configured request handlers. This is invoked by the CacheHandler for both foreground and background requests once the cache has been updated.

Note, this is invoked by the CacheHandler regardless of whether the request has a cache-key.

This method should not be invoked directly by consumers.

Parameters

request

ImmutableRequestInfo

response

null | ResponseInfo | Response

cacheKey

null | RequestKey

store

Store

Returns

void


invalidateRequest()

ts
invalidateRequest(cacheKey, store): void;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:548

Invalidate a request by its CacheKey for the given store instance.

While the store argument may seem redundant, the CachePolicy is designed to be shared across multiple stores / forks of the store.

ts
store.lifetimes.invalidateRequest(store, cacheKey);

Parameters

cacheKey

RequestKey

store

Store

Returns

void


invalidateRequestsForType()

ts
invalidateRequestsForType(type, store): void;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:569

Invalidate all requests associated to a specific type for a given store instance.

While the store argument may seem redundant, the CachePolicy is designed to be shared across multiple stores / forks of the store.

This invalidation is done automatically when using this service for both the CacheHandler and the LegacyNetworkHandler.

ts
store.lifetimes.invalidateRequestsForType(store, 'person');

Parameters

type

string

store

Store

Returns

void


isHardExpired()

ts
isHardExpired(cacheKey, store): boolean;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:647

Invoked to determine if the request may be fulfilled from cache if possible.

Note, this is only invoked by the CacheHandler if the request has a cache-key.

If no cache entry is found or the entry is hard expired, the request will be fulfilled from the configured request handlers and the cache will be updated before returning the response.

Parameters

cacheKey

RequestKey

store

Store

Returns

boolean

true if the request is considered hard expired


isSoftExpired()

ts
isSoftExpired(cacheKey, store): boolean;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:680

Invoked if isHardExpired is false to determine if the request should be update behind the scenes if cache data is already available.

Note, this is only invoked by the CacheHandler if the request has a cache-key.

If true, the request will be fulfilled from cache while a backgrounded request is made to update the cache via the configured request handlers.

Parameters

cacheKey

RequestKey

store

Store

Returns

boolean

true if the request is considered soft expired

Properties

_stores

ts
_stores: WeakMap<Store, {
  invalidated: Set<RequestKey>;
  types: Map<string, Set<RequestKey>>;
}>;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:498


config

ts
config: PolicyConfig;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:497

Constructors

Constructor

ts
new DefaultCachePolicy(config): DefaultCachePolicy;

Defined in: warp-drive-packages/core/src/store/-private/default-cache-policy.ts:512

Parameters

config

PolicyConfig

Returns

DefaultCachePolicy

Released under the MIT License.