Skip to content

LocalStorageCache

@blobs/sdk v0.1.0


@blobs/sdk / cache / LocalStorageCache

Defined in: cache/LocalStorageCache.ts:12

localStorage-backed cache provider for browser environments.

Each entry is JSON-serialized along with its timestamp and TTL so expiry can be detected on read. The provider namespaces keys with a configurable prefix (default blobs_cache_) so multiple SDK instances can share the same localStorage without collision. On QuotaExceededError it tries once to evict expired entries and retry the write before giving up.

Implements

Constructors

Constructor

new LocalStorageCache(options?): LocalStorageCache

Defined in: cache/LocalStorageCache.ts:21

Parameters

options?

CacheOptions = {}

Provider configuration (CacheOptions); prefix defaults to blobs_cache_.

Returns

LocalStorageCache

Methods

get()

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

Defined in: cache/LocalStorageCache.ts:49

Reads and deserializes a cached entry. Expired entries are evicted and null is returned. Parse errors are logged and treated as misses.

Type Parameters

T

T = any

Expected value type.

Parameters

key

string

The cache key to read.

Returns

Promise<T | null>

Implementation of

CacheProvider.get


set()

set<T>(key, value, ttl?): Promise<void>

Defined in: cache/LocalStorageCache.ts:84

Serializes and writes an entry. On QuotaExceededError, attempts to free space by clearing expired entries and retries once.

Type Parameters

T

T = any

Value type being stored.

Parameters

key

string

The cache key to write.

value

T

The value to store (must be JSON-serializable).

ttl?

number

Optional TTL in milliseconds; falls back to CacheOptions.defaultTTL.

Returns

Promise<void>

Implementation of

CacheProvider.set


remove()

remove(key): Promise<void>

Defined in: cache/LocalStorageCache.ts:120

Removes a single entry from localStorage.

Parameters

key

string

The cache key to remove.

Returns

Promise<void>

Implementation of

CacheProvider.remove


clear()

clear(): Promise<void>

Defined in: cache/LocalStorageCache.ts:129

Removes every entry owned by this provider (matching its prefix).

Returns

Promise<void>

Implementation of

CacheProvider.clear


has()

has(key): Promise<boolean>

Defined in: cache/LocalStorageCache.ts:142

Reports whether a non-expired entry exists.

Parameters

key

string

The cache key to probe.

Returns

Promise<boolean>

Implementation of

CacheProvider.has


keys()

keys(pattern?): Promise<string[]>

Defined in: cache/LocalStorageCache.ts:153

Lists keys owned by this provider with the prefix stripped, optionally filtered by substring match.

Parameters

pattern?

string

Optional substring; only matching keys are returned.

Returns

Promise<string[]>

Implementation of

CacheProvider.keys


clearExpired()

clearExpired(): Promise<void>

Defined in: cache/LocalStorageCache.ts:174

Sweeps localStorage for entries owned by this provider that have expired and removes them. Also removes entries that fail to parse.

Returns

Promise<void>


getStats()

getStats(): Promise<{ totalKeys: number; expiredKeys: number; totalSize: number; }>

Defined in: cache/LocalStorageCache.ts:205

Reports cache occupancy diagnostics.

Returns

Promise<{ totalKeys: number; expiredKeys: number; totalSize: number; }>

Aggregate counts and the approximate byte footprint.