---
title: "@vltpkg/cache: Reference — vlt docs"
description: "Documentation for the vlt client and the vlt registry."
url: "https://docs.vlt.io/client/api-reference/cache/reference"
image: "https://docs.vlt.io/og/client/api-reference/cache/reference"
lang: "en"
---

# Reference

## Classes

### Cache

Defined in: index.ts:70

#### Extends

- [`LRUCache`](https://isaacs.github.io/node-lru-cache/)<`string`, `Buffer`, [`CacheFetchContext`](https://docs.vlt.io/client/api-reference/cache/reference.md#cachefetchcontext)>

#### Constructors

##### new Cache()

```ts
new Cache(options): Cache
```

Defined in: index.ts:99

###### Parameters

| Parameter | Type                                                                                       |
| --------- | ------------------------------------------------------------------------------------------ |
| `options` | [`CacheOptions`](https://docs.vlt.io/client/api-reference/cache/reference.md#cacheoptions) |

###### Returns

[`Cache`](https://docs.vlt.io/client/api-reference/cache/reference.md#cache)

###### Overrides

```ts
LRUCache<
  string,
  Buffer,
  CacheFetchContext
>.constructor
```

#### Properties

| Property        | Type                                                               | Default value           | Overrides                |
| --------------- | ------------------------------------------------------------------ | ----------------------- | ------------------------ |
| `[toStringTag]` | `string`                                                           | `'@vltpkg/cache.Cache'` | `LRUCache.[toStringTag]` |
| `onDiskDelete?` | (`path`: `string`, `key`: `string`, `deleted`: `boolean`) => `any` | `undefined`             | -                        |
| `onDiskWrite?`  | (`path`: `string`, `key`: `string`, `data`: `Buffer`) => `any`     | `undefined`             | -                        |
| `store?`        | `string`                                                           | `undefined`             | -                        |

#### Accessors

##### pending

###### Get Signature

```ts
get pending(): Promise<BooleanOrVoid>[]
```

Defined in: index.ts:87

A list of the actions currently happening in the background

###### Returns

`Promise`<[`BooleanOrVoid`](https://docs.vlt.io/client/api-reference/cache/reference.md#booleanorvoid)>\[]

##### defaultMax

###### Get Signature

```ts
get static defaultMax(): number
```

Defined in: index.ts:95

By default, cache up to 1000 items in memory. Disk cache is unbounded.

###### Returns

`number`

#### Methods

##### \[asyncIterator]\()

```ts
asyncIterator: AsyncGenerator<[string, Buffer<ArrayBufferLike>], void, void>
```

Defined in: index.ts:161

###### Returns

`AsyncGenerator`<\[`string`, `Buffer`<`ArrayBufferLike`>], `void`, `void`>

##### \[iterator]\()

```ts
iterator: Generator<[string, Buffer<ArrayBufferLike>], void>
```

Defined in: index.ts:193

###### Returns

`Generator`<\[`string`, `Buffer`<`ArrayBufferLike`>], `void`>

###### Overrides

```ts
LRUCache.[iterator]
```

##### delete()

```ts
delete(
   key, 
   fromDisk, 
   integrity?): boolean
```

Defined in: index.ts:214

Pass `true` as second argument to delete not just from the in-memory cache, but the disk backing as well.

###### Parameters

| Parameter    | Type                     | Default value |
| ------------ | ------------------------ | ------------- |
| `key`        | `string`                 | `undefined`   |
| `fromDisk`   | `boolean`                | `false`       |
| `integrity`? | `` `sha512-${string}` `` | `undefined`   |

###### Returns

`boolean`

###### Overrides

```ts
LRUCache.delete
```

##### fetchSync()

```ts
fetchSync(key, opts?): undefined | Buffer<ArrayBufferLike>
```

Defined in: index.ts:314

Read synchronously from the fs cache storage if not already in memory.

###### Parameters

| Parameter | Type                                                                                                                                                                                                    |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`     | `string`                                                                                                                                                                                                |
| `opts`?   | [`FetchOptions`](https://isaacs.github.io/node-lru-cache/)<`string`, `Buffer`<`ArrayBufferLike`>, [`CacheFetchContext`](https://docs.vlt.io/client/api-reference/cache/reference.md#cachefetchcontext)> |

###### Returns

`undefined` | `Buffer`<`ArrayBufferLike`>

##### integrityPath()

```ts
integrityPath(integrity?): undefined | string
```

Defined in: index.ts:298

given an SRI sha512 integrity string, get the path on disk that is hard-linked to the value.

###### Parameters

| Parameter    | Type                     |
| ------------ | ------------------------ |
| `integrity`? | `` `sha512-${string}` `` |

###### Returns

`undefined` | `string`

##### path()

```ts
path(key?): string
```

Defined in: index.ts:291

given a key, figure out the path on disk where it lives

###### Parameters

| Parameter | Type     |
| --------- | -------- |
| `key`?    | `string` |

###### Returns

`string`

##### promise()

```ts
promise(): Promise<void>
```

Defined in: index.ts:282

Resolves when there are no pending writes to the disk cache

###### Returns

`Promise`<`void`>

##### set()

```ts
set(
   key, 
   val, 
   options?): Cache
```

Defined in: index.ts:244

Sets an item in the memory cache (like `LRUCache.set`), and schedules a background operation to write it to disk.

Use the CacheOptions#onDiskWrite method to know exactly when this happens, or `await cache.promise()` to defer until all pending actions are completed.

The `noDiskWrite` option can be set to prevent it from writing back to the disk cache. This is almost never relevant for consumers, and is used internally to prevent the write at the end of `fetch()` from excessively writing over a file we just read from.

###### Parameters

| Parameter  | Type                                                                                                                                                                                                             |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`      | `string`                                                                                                                                                                                                         |
| `val`      | `Buffer`                                                                                                                                                                                                         |
| `options`? | [`SetOptions`](https://isaacs.github.io/node-lru-cache/)<`string`, `Buffer`<`ArrayBufferLike`>, [`CacheFetchContext`](https://docs.vlt.io/client/api-reference/cache/reference.md#cachefetchcontext)> & `object` |

###### Returns

[`Cache`](https://docs.vlt.io/client/api-reference/cache/reference.md#cache)

###### Overrides

```ts
LRUCache.set
```

##### walk()

```ts
walk(): AsyncGenerator<[string, Buffer<ArrayBufferLike>], void, unknown>
```

Defined in: index.ts:142

Walk over all the items cached to disk (not just in memory). Useful for cleanup, pruning, etc.

Implementation for `for await` to walk over entries.

###### Returns

`AsyncGenerator`<\[`string`, `Buffer`<`ArrayBufferLike`>], `void`, `unknown`>

##### walkSync()

```ts
walkSync(): Generator<[string, Buffer<ArrayBufferLike>], void, unknown>
```

Defined in: index.ts:172

Synchronous form of Cache.walk()

###### Returns

`Generator`<\[`string`, `Buffer`<`ArrayBufferLike`>], `void`, `unknown`>

## Type Aliases

### BooleanOrVoid

```ts
type BooleanOrVoid = boolean | void;
```

Defined in: index.ts:57

---

### CacheFetchContext

```ts
type CacheFetchContext = 
  | {
  integrity: Integrity;
 }
  | undefined;
```

Defined in: index.ts:20

---

### CacheOptions

```ts
type CacheOptions = { [k in keyof LRUCache.Options<string, Buffer, CacheFetchContext>]?: LRUCache.Options<string, Buffer, CacheFetchContext>[k] } & object;
```

Defined in: index.ts:26

#### Type declaration

| Name            | Type                                | Description                                                                                                                                         |
| --------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fetchMethod`?  | `undefined`                         | fetchMethod may not be provided, because this cache forces its own read-from-disk as the fetchMethod                                                |
| `onDiskDelete`? | (`path`, `key`, `deleted`) => `any` | called whenever an item is deleted with `cache.delete(key, true)` Deletes of the in-memory data do not trigger this method.                         |
| `onDiskWrite`?  | (`path`, `key`, `data`) => `any`    | called whenever an item is written to disk.                                                                                                         |
| `path`          | `string`                            | folder where items should be stored to disk                                                                                                         |
| `store`?        | `string`                            | global store root. Deleting an entry from disk with its integrity also removes the store entry, sidecar index and copied marker for that integrity. |
