cacheAsync

Creates a version of a schema that caches its output.

const Schema = v.cacheAsync<TSchema, TCacheConfig>(schema, config);

Generics

Parameters

  • schema TSchema
  • config TCacheConfig

Explanation

The cacheAsync method creates a version of the given schema that caches its output. This can be useful for performance optimization, for example when validation involves a network request.

Hint: Primitive inputs are cached by value. Object and function inputs are cached by reference identity, so mutating input objects and reusing the same reference can return a stale cached dataset. Returned objects are also reused by reference, so mutating cached output can affect later cache hits. For best results, use cacheAsync with immutable inputs and avoid mutating returned cached objects.

Returns

Examples

The following examples show how cacheAsync can be used.

Cache schema

Schema that caches its output.

const CacheSchema = v.cacheAsync(v.string());

Max size schema

Schema that caches its output for a maximum of 100 items.

const MaxSizeSchema = v.cacheAsync(v.string(), { maxSize: 100 });

Max age schema

Schema that caches its output for a maximum of 10 seconds.

const MaxAgeSchema = v.cacheAsync(v.string(), { maxAge: 10_000 });

The following APIs can be combined with cacheAsync.

Schemas

Methods

Actions

Async

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of @EskiMojo14
  • GitHub profile picture of @fabianhiller

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of @antfu
  • GitHub profile picture of @UpwayShop
  • GitHub profile picture of @vasilii-kovalev
  • GitHub profile picture of @saturnonearth
  • GitHub profile picture of @ruiaraujo012
  • GitHub profile picture of @hyunbinseo
  • GitHub profile picture of @nickytonline
  • GitHub profile picture of @KubaJastrz
  • GitHub profile picture of @kibertoad
  • GitHub profile picture of @Thanaen
  • GitHub profile picture of @caegdeveloper
  • GitHub profile picture of @bmoyroud
  • GitHub profile picture of @dslatkin