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 localStorageType Parameters
T
T
The type of values stored in the cache
Implements
ICache<T>
Constructors
Constructor
new PersistentCache<T>(config, maxStorageBytes?): PersistentCache<T>;Parameters
config
maxStorageBytes?
number
Returns
PersistentCache<T>
Methods
clear()
clear(): void;Removes all entries from the cache
Returns
void
Implementation of
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
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
getStats()
getStats(): CacheStats;Returns current cache statistics
Returns
CacheStats object with size and configuration info
Implementation of
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
keys()
keys(): string[];Returns all keys currently in the cache
Returns
string[]
Array of cache keys
Implementation of
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