> For the complete documentation index, see [llms.txt](https://docs.twine.world/twine-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.twine.world/twine-js/twine-protocol/twine-core/classes/memorystore.md).

# MemoryStore

[**@twine-protocol/twine-core v0.1.0**](/twine-js/twine-protocol/twine-core/index.md) • **Docs**

***

[twine-js](/twine-js/index.md) / [@twine-protocol/twine-core](/twine-js/twine-protocol/twine-core/index.md) / MemoryStore

## Class: MemoryStore

A store that keeps twines in memory

This store is the base for the [TwineCache](/twine-js/twine-protocol/twine-core/classes/twinecache.md).

### Extended by

* [`TwineCache`](/twine-js/twine-protocol/twine-core/classes/twinecache.md)

### Implements

* [`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md)

### Constructors

#### new MemoryStore()

> **new MemoryStore**(`maxSize`): [`MemoryStore`](/twine-js/twine-protocol/twine-core/classes/memorystore.md)

Create a new memory store

**Parameters**

| Parameter | Type     | Default value | Description                                                       |
| --------- | -------- | ------------- | ----------------------------------------------------------------- |
| `maxSize` | `number` | `0`           | The maximum number of twines to keep in memory (default infinite) |

**Returns**

[`MemoryStore`](/twine-js/twine-protocol/twine-core/classes/memorystore.md)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:60](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L60)

### Properties

| Property           | Modifier    | Type                                                                                                                                                  | Description                                        | Defined in                                                                                                                                                                                     |
| ------------------ | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `chainStore`       | `protected` | `Map`<`string`, [`Chain`](/twine-js/twine-protocol/twine-core/type-aliases/chain.md)>                                                                 | Chains are stored in a map with the CID as the key | [packages/twine-core/src/store/memory-store.ts:37](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L37) |
| `chainStorageMeta` | `protected` | `Map`<`string`, [`ChainStorageMeta`](/twine-js/twine-protocol/twine-core/type-aliases/chainstoragemeta.md)>                                           | Chain storage metadata                             | [packages/twine-core/src/store/memory-store.ts:41](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L41) |
| `pulseStore`       | `protected` | [`CacheMap`](/twine-js/twine-protocol/twine-core/classes/cachemap.md)<`string`, [`Pulse`](/twine-js/twine-protocol/twine-core/type-aliases/pulse.md)> | Pulses are stored in a map with the CID as the key | [packages/twine-core/src/store/memory-store.ts:45](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L45) |
| `maxSize`          | `protected` | `number`                                                                                                                                              | The maximum number of twines to keep in memory     | [packages/twine-core/src/store/memory-store.ts:49](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L49) |

### Methods

#### setMaxSize()

> **setMaxSize**(`maxSize`): `void`

Set the maximum number of twines to keep in memory

If the store is already larger than the new max size, the oldest twines will be removed.

**Parameters**

| Parameter | Type     |
| --------- | -------- |
| `maxSize` | `number` |

**Returns**

`void`

**Defined in**

[packages/twine-core/src/store/memory-store.ts:70](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L70)

***

#### fetch()

> **fetch**(`cid`): `null` | [`Chain`](/twine-js/twine-protocol/twine-core/type-aliases/chain.md) | [`Pulse`](/twine-js/twine-protocol/twine-core/type-aliases/pulse.md)

Fetch a twine from storage, returning null if it is not found

it is NOT expected that the twine signature is checked, that is for the [Resolver](/twine-js/twine-protocol/twine-core/interfaces/resolver.md) to do.

**Parameters**

| Parameter | Type                                                                     | Description                   |
| --------- | ------------------------------------------------------------------------ | ----------------------------- |
| `cid`     | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md) | The CID of the twine to fetch |

**Returns**

`null` | [`Chain`](/twine-js/twine-protocol/twine-core/type-aliases/chain.md) | [`Pulse`](/twine-js/twine-protocol/twine-core/type-aliases/pulse.md)

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`fetch`](/twine-js/twine-protocol/twine-core/interfaces/store.md#fetch)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:81](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L81)

***

#### has()

> **has**(`cid`): `boolean`

Check if a cid can be resolved

**Parameters**

| Parameter | Type                                                                     | Description      |
| --------- | ------------------------------------------------------------------------ | ---------------- |
| `cid`     | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md) | The CID to check |

**Returns**

`boolean`

True if the CID can be resolved, false otherwise

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`has`](/twine-js/twine-protocol/twine-core/interfaces/store.md#has)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:97](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L97)

***

#### delete()

> **delete**(`cid`): `void`

Delete a twine from storage

**Parameters**

| Parameter | Type                                                                     | Description                    |
| --------- | ------------------------------------------------------------------------ | ------------------------------ |
| `cid`     | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md) | The CID of the twine to delete |

**Returns**

`void`

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`delete`](/twine-js/twine-protocol/twine-core/interfaces/store.md#delete)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:105](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L105)

***

#### save()

> **save**(`twine`): `void`

Save a twine to storage

**Parameters**

| Parameter | Type                                                                   | Description       |
| --------- | ---------------------------------------------------------------------- | ----------------- |
| `twine`   | [`Twine`](/twine-js/twine-protocol/twine-core/classes/twine.md)<`any`> | The twine to save |

**Returns**

`void`

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`save`](/twine-js/twine-protocol/twine-core/interfaces/store.md#save)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:147](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L147)

***

#### saveMany()

Save many twines to storage

**saveMany(twines)**

> **saveMany**(`twines`): `Promise`<`void`>

Save many twines to storage

**Parameters**

| Parameter | Type                                                                                                                                                                           | Description        |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ |
| `twines`  | `AsyncIterable`<[`Twine`](/twine-js/twine-protocol/twine-core/classes/twine.md)<[`TwineValue`](/twine-js/twine-protocol/twine-core/type-aliases/twinevalue.md)>, `any`, `any`> | The twines to save |

**Returns**

`Promise`<`void`>

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`saveMany`](/twine-js/twine-protocol/twine-core/interfaces/store.md#savemany)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:159](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L159)

**saveMany(twines)**

> **saveMany**(`twines`): `void`

Save many twines to storage

**Parameters**

| Parameter | Type                                                                                                                                                                      |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `twines`  | `Iterable`<[`Twine`](/twine-js/twine-protocol/twine-core/classes/twine.md)<[`TwineValue`](/twine-js/twine-protocol/twine-core/type-aliases/twinevalue.md)>, `any`, `any`> |

**Returns**

`void`

**Implementation of**

`Store.saveMany`

**Defined in**

[packages/twine-core/src/store/memory-store.ts:160](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L160)

***

#### chains()

> **chains**(): `MapIterator`<[`Chain`](/twine-js/twine-protocol/twine-core/type-aliases/chain.md)>

Get an iterator of all the chains in the store

**Returns**

`MapIterator`<[`Chain`](/twine-js/twine-protocol/twine-core/type-aliases/chain.md)>

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`chains`](/twine-js/twine-protocol/twine-core/interfaces/store.md#chains)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:181](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L181)

***

#### pulses()

> **pulses**(`chainCid`, `start`?, `options`?): `AsyncGenerator`<[`Pulse`](/twine-js/twine-protocol/twine-core/type-aliases/pulse.md), `any`, `any`>

Get an async iterator of all the pulses in a chain

**Parameters**

| Parameter  | Type                                                                                   |
| ---------- | -------------------------------------------------------------------------------------- |
| `chainCid` | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md)               |
| `start`?   | `number` \| [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md)   |
| `options`? | [`ResolveOptions`](/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions.md) |

**Returns**

`AsyncGenerator`<[`Pulse`](/twine-js/twine-protocol/twine-core/type-aliases/pulse.md), `any`, `any`>

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`pulses`](/twine-js/twine-protocol/twine-core/interfaces/store.md#pulses)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:188](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L188)

***

#### resolve()

Resolve a chain from a query

This is the main way to get a pulse or chain from somewhere and have it automatically verified.

If the input is already a successful resolution, it will be returned as is.

If the input is a chain or pulse, it will be resolved as a chain or pulse resolution.

**resolve(query, options)**

> **resolve**(`query`, `options`?): `Promise`<[`ChainResolution`](/twine-js/twine-protocol/twine-core/type-aliases/chainresolution.md)>

Resolve a chain from a query

This is the main way to get a pulse or chain from somewhere and have it automatically verified.

If the input is already a successful resolution, it will be returned as is.

If the input is a chain or pulse, it will be resolved as a chain or pulse resolution.

**Parameters**

| Parameter  | Type                                                                                                 | Description                |
| ---------- | ---------------------------------------------------------------------------------------------------- | -------------------------- |
| `query`    | [`IntoResolveChainQuery`](/twine-js/twine-protocol/twine-core/type-aliases/intoresolvechainquery.md) | The query to resolve       |
| `options`? | [`ResolveOptions`](/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions.md)               | Options for the resolution |

**Returns**

`Promise`<[`ChainResolution`](/twine-js/twine-protocol/twine-core/type-aliases/chainresolution.md)>

A chain or pulse resolution

A chain or pulse resolution

**Examples**

```js
const { chain } = await resolver.resolve({ chain: 'bafybeib3...' })
if (chain) {
  console.log('chain', chain)
} else {
  console.log('no chain')
}
```

```js
const { chain, pulse } = await resolver.resolve({
  chain: 'bafybeib3...',
  pulse: 'bafybeib3...'
})

if (pulse) {
  console.log('pulse', pulse)
} else {
  console.log('no pulse')
}
```

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`resolve`](/twine-js/twine-protocol/twine-core/interfaces/store.md#resolve)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:215](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L215)

**resolve(query, options)**

> **resolve**(`query`, `options`?): `Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

Resolve a pulse (with its chain) from a query

**Parameters**

| Parameter  | Type                                                                                                 |
| ---------- | ---------------------------------------------------------------------------------------------------- |
| `query`    | [`IntoResolvePulseQuery`](/twine-js/twine-protocol/twine-core/type-aliases/intoresolvepulsequery.md) |
| `options`? | [`ResolveOptions`](/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions.md)               |

**Returns**

`Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

A chain or pulse resolution

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`resolve`](/twine-js/twine-protocol/twine-core/interfaces/store.md#resolve)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:216](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L216)

***

#### resolveLatest()

> **resolveLatest**(`chainCid`, `options`?): `Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

Resolve the latest pulse of a chain

**Parameters**

| Parameter  | Type                                                                                   | Description                                                    |
| ---------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| `chainCid` | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md)               | The chain CID or chain itself to resolve the latest pulse from |
| `options`? | [`ResolveOptions`](/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions.md) | Options for the resolution                                     |

**Returns**

`Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

A pulse resolution

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`resolveLatest`](/twine-js/twine-protocol/twine-core/interfaces/store.md#resolvelatest)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:230](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L230)

***

#### resolveIndex()

> **resolveIndex**(`chain`, `index`, `options`?): `Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

Resolve a pulse by index

**Parameters**

| Parameter  | Type                                                                                   | Description                                             |
| ---------- | -------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `chain`    | [`IntoCid`](/twine-js/twine-protocol/twine-core/type-aliases/intocid.md)               | The chain CID or chain itself to resolve the pulse from |
| `index`    | `number`                                                                               | The index of the pulse to resolve                       |
| `options`? | [`ResolveOptions`](/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions.md) | Options for the resolution                              |

**Returns**

`Promise`<[`PulseResolution`](/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution.md)>

A pulse resolution

**Implementation of**

[`Store`](/twine-js/twine-protocol/twine-core/interfaces/store.md).[`resolveIndex`](/twine-js/twine-protocol/twine-core/interfaces/store.md#resolveindex)

**Defined in**

[packages/twine-core/src/store/memory-store.ts:246](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/store/memory-store.ts#L246)
