Skip to content

Documentation / @ember-data/request-utils / index / LifetimesService

Class: LifetimesService

Defined in: packages/request-utils/src/index.ts:54

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

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

export default class AppStore extends Store {
  lifetimes = new DefaultCachePolicy({ 
    apiCacheSoftExpires: 30_000,
    apiCacheHardExpires: 60_000,
    // ... Other PolicyConfig Settings ... //
  });
}

💡 TIP

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

See also PolicyConfig for configuration options.

The Mechanics

This policy determines staleness based on various configurable constraints falling back to a simple check of the time elapsed since the request was last received from the API using the date header from the last response.

💡 TIP

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

  • For manual override of reload see CacheOptions.reload | cacheOptions.reload
  • For manual override of background reload see CacheOptions.backgroundReload | cacheOptions.backgroundReload

In order expiration is determined by:

md
Is explicitly invalidated by `cacheOptions.reload`
  ↳ (if falsey) if the request has been explicitly invalidated
     since the last request (see Automatic Invalidation below)
  ↳ (if false) (If Active) isExpired function
  ↳ (if null) (If Active) X-WarpDrive-Expires header
  ↳ (if null) (If Active) Cache-Control header
  ↳ (if null) (If Active) Expires header
  ↳ (if null) Date header + apiCacheHardExpires < current time

  -- <if above is false, a background request is issued if> --

  ↳ is invalidated by `cacheOptions.backgroundReload`
  ↳ (if falsey) Date header + apiCacheSoftExpires < current time

Automatic Invalidation / Entanglement

It also invalidates any request with an RequestInfo.op | OpCode of "query" for which CacheOptions.types | cacheOptions.types was provided when a request with an OpCode of "createRecord" is successful and also includes a matching type in its own cacheOptions.types array.

💡 TIP

Abstracting this behavior via builders is recommended to ensure consistency.

Testing

In Testing environments:

  • apiCacheSoftExpires will always be false
  • 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.

Extends

Constructors

Constructor

ts
new LifetimesService(config): LifetimesService;

Defined in: packages/request-utils/src/index.ts:55

Parameters

config

PolicyConfig

Returns

LifetimesService

Overrides

CachePolicy.constructor

Methods

didRequest()

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

Defined in: warp-drive-packages/core/declarations/store/-private/default-cache-policy.d.ts:356

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 | Response | ResponseInfo

cacheKey

null | RequestKey

store

Store

Returns

void

Inherited from

CachePolicy.didRequest


invalidateRequest()

ts
invalidateRequest(cacheKey, store): void;

Defined in: warp-drive-packages/core/declarations/store/-private/default-cache-policy.d.ts:325

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

Inherited from

CachePolicy.invalidateRequest


invalidateRequestsForType()

ts
invalidateRequestsForType(type, store): void;

Defined in: warp-drive-packages/core/declarations/store/-private/default-cache-policy.d.ts:343

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 NetworkHandler.

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

Parameters

type

string

store

Store

Returns

void

Inherited from

CachePolicy.invalidateRequestsForType


isHardExpired()

ts
isHardExpired(cacheKey, store): boolean;

Defined in: warp-drive-packages/core/declarations/store/-private/default-cache-policy.d.ts:371

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

Inherited from

CachePolicy.isHardExpired


isSoftExpired()

ts
isSoftExpired(cacheKey, store): boolean;

Defined in: warp-drive-packages/core/declarations/store/-private/default-cache-policy.d.ts:385

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

Inherited from

CachePolicy.isSoftExpired

Released under the MIT License.