Share via


MemoryStorage class

A simple in-memory storage provider that implements the Storage interface.

Remarks

This class provides a volatile storage solution that keeps data in memory, which means data is lost when the process terminates. It's primarily useful for:

  • Development and testing scenarios
  • Simple applications that don't require data persistence across restarts
  • Stateless environments where external storage isn't available

MemoryStorage supports optimistic concurrency control through eTags and can be used as a singleton through the getSingleInstance() method to share state across different parts of an application.

Constructors

MemoryStorage({[k: string]: string})

Creates a new instance of the MemoryStorage class.

Methods

delete(string[])

Deletes storage items from memory.

getSingleInstance()

Gets a single shared instance of the MemoryStorage class.

Using this method ensures that the same storage instance is used across the application, allowing for shared state without passing references.

read(string[])

Reads storage items from memory.

write(StoreItem)

Writes storage items to memory.

This method supports optimistic concurrency control through eTags. If an item has an eTag, it will only be updated if the existing item has the same eTag. If an item has an eTag of '*' or no eTag, it will always be written regardless of the current state.

Constructor Details

MemoryStorage({[k: string]: string})

Creates a new instance of the MemoryStorage class.

new MemoryStorage(memory?: {[k: string]: string})

Parameters

memory

{[k: string]: string}

An optional initial memory store to seed the storage with data

Method Details

delete(string[])

Deletes storage items from memory.

function delete(keys: string[]): Promise<void>

Parameters

keys

string[]

The keys of the items to delete

Returns

Promise<void>

A promise that resolves when the delete operation is complete

getSingleInstance()

Gets a single shared instance of the MemoryStorage class.

Using this method ensures that the same storage instance is used across the application, allowing for shared state without passing references.

static function getSingleInstance(): MemoryStorage

Returns

The singleton instance of MemoryStorage

read(string[])

Reads storage items from memory.

function read(keys: string[]): Promise<StoreItem>

Parameters

keys

string[]

The keys of the items to read

Returns

Promise<StoreItem>

A promise that resolves to the read items

write(StoreItem)

Writes storage items to memory.

This method supports optimistic concurrency control through eTags. If an item has an eTag, it will only be updated if the existing item has the same eTag. If an item has an eTag of '*' or no eTag, it will always be written regardless of the current state.

function write(changes: StoreItem): Promise<void>

Parameters

changes
StoreItem

The items to write, indexed by key

Returns

Promise<void>

A promise that resolves when the write operation is complete