Skip to content

TypeScript API / laikacms/storage / MemoryCache

Class: MemoryCache<K, V> ​

Simple in-memory implementation of AsyncCache Useful for testing or simple use cases

Type Parameters ​

Type Parameter
K
V

Implements ​

Constructors ​

Constructor ​

ts
new MemoryCache<K, V>(): MemoryCache<K, V>;

Returns ​

MemoryCache<K, V>

Methods ​

clear() ​

ts
clear(): Promise<void>;

Clear all entries from the cache

Returns ​

Promise<void>

Implementation of ​

AsyncCache.clear


delete() ​

ts
delete(key): Promise<boolean>;

Delete a key from the cache

Parameters ​

ParameterType
keyK

Returns ​

Promise<boolean>

true if the key existed and was deleted, false otherwise

Implementation of ​

AsyncCache.delete


entries() ​

ts
entries(): Promise<[K, V][]>;

Get all entries in the cache (excluding expired ones)

Returns ​

Promise<[K, V][]>

Implementation of ​

AsyncCache.entries


forEach() ​

ts
forEach(callback): Promise<void>;

Iterate over all entries in the cache (excluding expired ones)

Parameters ​

ParameterType
callback(value, key) => void | Promise<void>

Returns ​

Promise<void>

Implementation of ​

AsyncCache.forEach


get() ​

ts
get(key): Promise<V | undefined>;

Get a value from the cache Returns undefined if the key doesn't exist or has expired

Parameters ​

ParameterType
keyK

Returns ​

Promise<V | undefined>

Implementation of ​

AsyncCache.get


has() ​

ts
has(key): Promise<boolean>;

Check if a key exists in the cache and has not expired

Parameters ​

ParameterType
keyK

Returns ​

Promise<boolean>

Implementation of ​

AsyncCache.has


keys() ​

ts
keys(): Promise<K[]>;

Get all keys in the cache (excluding expired ones)

Returns ​

Promise<K[]>

Implementation of ​

AsyncCache.keys


set() ​

ts
set(
   key, 
   value, 
   expiresAt?
): Promise<void>;

Set a value in the cache with optional expiration

Parameters ​

ParameterTypeDescription
keyKThe cache key
valueVThe value to cache
expiresAt?DateOptional expiration date. If not provided, the entry never expires

Returns ​

Promise<void>

Implementation of ​

AsyncCache.set


size() ​

ts
size(): Promise<number>;

Get the number of entries in the cache (including expired ones) Note: Some implementations may exclude expired entries

Returns ​

Promise<number>

Implementation of ​

AsyncCache.size


values() ​

ts
values(): Promise<V[]>;

Get all values in the cache (excluding expired ones)

Returns ​

Promise<V[]>

Implementation of ​

AsyncCache.values

Released under the MIT License.