Skip to content

@egain/ai-agent-sdk API Reference - v0.1.1 / StorageCacheAdapter

Class: StorageCacheAdapter

Browser storage cache adapter using localStorage or sessionStorage.

Automatically serializes values to JSON for storage. Falls back gracefully if storage is unavailable or full.

Example

typescript
// Use sessionStorage (cleared when tab closes)
const sessionCache = new StorageCacheAdapter('session');

// Use localStorage (persistent)
const localCache = new StorageCacheAdapter('local');

// Store data
sessionCache.set('config', { 
  value: { theme: 'dark' }, 
  timestamp: Date.now() 
});

Implements

Table of contents

Constructors

Methods

Constructors

constructor

new StorageCacheAdapter(storageType?): StorageCacheAdapter

Create a new StorageCacheAdapter

Parameters

NameTypeDefault valueDescription
storageType"local" | "session"'session'Use 'local' for localStorage, 'session' for sessionStorage

Returns

StorageCacheAdapter

Throws

Error if not in a browser environment

Defined in

core/api/CacheAdapter.ts:291

Methods

get

get<T>(key): null | CacheEntry<T>

Get a value from the cache

Type parameters

Name
T

Parameters

NameTypeDescription
keystringThe cache key

Returns

null | CacheEntry<T>

The cached entry or null if not found

Implementation of

CacheAdapter.get

Defined in

core/api/CacheAdapter.ts:303


set

set<T>(key, entry): void

Set a value in the cache

Type parameters

Name
T

Parameters

NameTypeDescription
keystringThe cache key
entryCacheEntry<T>The cache entry with value and timestamp

Returns

void

Implementation of

CacheAdapter.set

Defined in

core/api/CacheAdapter.ts:315


delete

delete(key): void

Delete a value from the cache

Parameters

NameTypeDescription
keystringThe cache key

Returns

void

Implementation of

CacheAdapter.delete

Defined in

core/api/CacheAdapter.ts:324


clear

clear(prefix?): void

Clear all values from the cache

Parameters

NameTypeDescription
prefix?stringOptional prefix to only clear keys starting with this prefix

Returns

void

Implementation of

CacheAdapter.clear

Defined in

core/api/CacheAdapter.ts:328


keys

keys(prefix?): string[]

Get all keys in the cache

Parameters

NameTypeDescription
prefix?stringOptional prefix to filter keys

Returns

string[]

Array of matching cache keys

Implementation of

CacheAdapter.keys

Defined in

core/api/CacheAdapter.ts:343

Released under the MIT License.