Skip to content
module@ember-data/request-utils/index

Class LifetimesService

Defined in: index.ts:53

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.

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 OpCode of "query" for which 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: index.ts:54

Parameters

config

PolicyConfig

Returns

LifetimesService

Overrides

DefaultCachePolicy.constructor

Released under the MIT License.