Skip to content

TypeScript API / laikacms/storage / AsyncCache

Interface: AsyncCache<K, V> ​

Async key-value cache interface with expiration support

Similar to Map but async and with TTL (time-to-live) support.

Example ​

typescript
const cache: AsyncCache<string, User> = new MemoryCache();

// Set with expiration (5 minutes)
await cache.set('user:123', user, new Date(Date.now() + 5 * 60 * 1000));

// Get value (returns undefined if expired)
const user = await cache.get('user:123');

// Check if key exists and not expired
const exists = await cache.has('user:123');

// Delete entry
await cache.delete('user:123');

// Clear all entries
await cache.clear();

Type Parameters ​

Type Parameter
K
V

Methods ​

clear() ​

ts
clear(): Promise<void>;

Clear all entries from the cache

Returns ​

Promise<void>


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


entries() ​

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

Get all entries in the cache (excluding expired ones)

Returns ​

Promise<[K, V][]>


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>


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>


has() ​

ts
has(key): Promise<boolean>;

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

Parameters ​

ParameterType
keyK

Returns ​

Promise<boolean>


keys() ​

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

Get all keys in the cache (excluding expired ones)

Returns ​

Promise<K[]>


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>


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>


values() ​

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

Get all values in the cache (excluding expired ones)

Returns ​

Promise<V[]>

Released under the MIT License.