# cacheAsync

> This document is the Markdown version of [valibot.dev/api/cacheAsync/](https://valibot.dev/api/cacheAsync/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a version of a schema that caches its output.

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

## Generics

- `TSchema` `extends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>`
- `TCacheConfig` `extends CacheConfig | undefined`

## 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

- `Schema` `SchemaWithCacheAsync<TSchema, TCacheConfig>`

## Examples

The following examples show how `cacheAsync` can be used.

### Cache schema

Schema that caches its output.

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

### Max size schema

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

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

### Max age schema

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

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

## Related

The following APIs can be combined with `cacheAsync`.

### Schemas

[`any`](/api/any.md), [`array`](/api/array.md), [`bigint`](/api/bigint.md), [`blob`](/api/blob.md), [`boolean`](/api/boolean.md), [`custom`](/api/custom.md), [`date`](/api/date.md), [`enum`](/api/enum.md), [`exactOptional`](/api/exactOptional.md), [`file`](/api/file.md), [`function`](/api/function.md), [`instance`](/api/instance.md), [`intersect`](/api/intersect.md), [`lazy`](/api/lazy.md), [`literal`](/api/literal.md), [`looseObject`](/api/looseObject.md), [`looseTuple`](/api/looseTuple.md), [`map`](/api/map.md), [`nan`](/api/nan.md), [`never`](/api/never.md), [`nonNullable`](/api/nonNullable.md), [`nonNullish`](/api/nonNullish.md), [`nonOptional`](/api/nonOptional.md), [`null`](/api/null.md), [`nullable`](/api/nullable.md), [`nullish`](/api/nullish.md), [`number`](/api/number.md), [`object`](/api/object.md), [`objectWithRest`](/api/objectWithRest.md), [`optional`](/api/optional.md), [`picklist`](/api/picklist.md), [`promise`](/api/promise.md), [`record`](/api/record.md), [`set`](/api/set.md), [`strictObject`](/api/strictObject.md), [`strictTuple`](/api/strictTuple.md), [`string`](/api/string.md), [`symbol`](/api/symbol.md), [`tuple`](/api/tuple.md), [`tupleWithRest`](/api/tupleWithRest.md), [`undefined`](/api/undefined.md), [`undefinedable`](/api/undefinedable.md), [`union`](/api/union.md), [`unknown`](/api/unknown.md), [`variant`](/api/variant.md), [`void`](/api/void.md)

### Methods

[`config`](/api/config.md), [`getDefault`](/api/getDefault.md), [`getFallback`](/api/getFallback.md)

### Actions

[`message`](/api/message.md), [`unwrap`](/api/unwrap.md)

### Async

[`parseAsync`](/api/parseAsync.md), [`safeParseAsync`](/api/safeParseAsync.md)
