Skip to Content
APIConfigurationClass: PersistentCache<T>

Class: PersistentCache<T>

Persistent Cache implementation (Decorator pattern) Wraps LRUCache with localStorage persistence.

Features:

  • Automatic save to localStorage on modifications
  • Hydration from localStorage on initialization
  • Graceful degradation when localStorage unavailable
  • Storage quota management
  • Expired items filtered during hydration

Example

const cache = new PersistentCache<string>({ maxItems: 100, ttlMs: 60000, persistenceKey: "my_cache", }) cache.set("key1", "value1") // Saved to memory and localStorage

Type Parameters

T

T

The type of values stored in the cache

Implements

Constructors

Constructor

new PersistentCache<T>(config, maxStorageBytes?): PersistentCache<T>;

Parameters

config

CacheConfig

maxStorageBytes?

number

Returns

PersistentCache<T>

Methods

clear()

clear(): void;

Removes all entries from the cache

Returns

void

Implementation of

ICache.clear


delete()

delete(key): boolean;

Removes a specific key from the cache

Parameters

key

string

The cache key to remove

Returns

boolean

true if the key was found and removed

Implementation of

ICache.delete


get()

get(key): undefined | T;

Retrieves a value from the cache

Parameters

key

string

The cache key

Returns

undefined | T

The cached value or undefined if not found/expired

Implementation of

ICache.get


getStats()

getStats(): CacheStats;

Returns current cache statistics

Returns

CacheStats

CacheStats object with size and configuration info

Implementation of

ICache.getStats


has()

has(key): boolean;

Checks if a key exists in the cache

Parameters

key

string

The cache key

Returns

boolean

true if the key exists and is not expired

Implementation of

ICache.has


keys()

keys(): string[];

Returns all keys currently in the cache

Returns

string[]

Array of cache keys

Implementation of

ICache.keys


set()

set(key, value): void;

Stores a value in the cache

Parameters

key

string

The cache key

value

T

The value to store

Returns

void

Implementation of

ICache.set

Last updated on