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
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()
clear(): Promise<void>;Clear all entries from the cache
Returns
Promise<void>
delete()
delete(key): Promise<boolean>;Delete a key from the cache
Parameters
| Parameter | Type |
|---|---|
key | K |
Returns
Promise<boolean>
true if the key existed and was deleted, false otherwise
entries()
entries(): Promise<[K, V][]>;Get all entries in the cache (excluding expired ones)
Returns
Promise<[K, V][]>
forEach()
forEach(callback): Promise<void>;Iterate over all entries in the cache (excluding expired ones)
Parameters
| Parameter | Type |
|---|---|
callback | (value, key) => void | Promise<void> |
Returns
Promise<void>
get()
get(key): Promise<V | undefined>;Get a value from the cache Returns undefined if the key doesn't exist or has expired
Parameters
| Parameter | Type |
|---|---|
key | K |
Returns
Promise<V | undefined>
has()
has(key): Promise<boolean>;Check if a key exists in the cache and has not expired
Parameters
| Parameter | Type |
|---|---|
key | K |
Returns
Promise<boolean>
keys()
keys(): Promise<K[]>;Get all keys in the cache (excluding expired ones)
Returns
Promise<K[]>
set()
set(
key,
value,
expiresAt?
): Promise<void>;Set a value in the cache with optional expiration
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | The cache key |
value | V | The value to cache |
expiresAt? | Date | Optional expiration date. If not provided, the entry never expires |
Returns
Promise<void>
size()
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()
values(): Promise<V[]>;Get all values in the cache (excluding expired ones)
Returns
Promise<V[]>