Skip to main content

Overview

The Cacher class provides a unified interface for caching data with support for multiple storage backends through the CacheStore interface. It handles serialization, deserialization, TTL (time-to-live), and batch operations.

Class: Cacher

Constructor

new Cacher(options?: CacheOptions)
options
CacheOptions
Configuration options for the cacher instance
options.store
CacheStore
The cache store implementation to use. Defaults to NullCache (no-op cache).
options.defaultTtl
number
Default time-to-live in seconds for cached items. If not specified, items may be cached indefinitely (depending on the store implementation).
Example:
import { Cacher } from '@resolid/cache';
import { MemoryCache } from '@resolid/cache/stores';

const cacher = new Cacher({
  store: new MemoryCache(),
  defaultTtl: 3600 // 1 hour default TTL
});

Methods

get()

Retrieves a value from the cache.
async get<T>(key: string, defaultValue?: T): Promise<T | undefined>
key
string
required
The cache key to retrieve
defaultValue
T
Optional default value to return if the key is not found
return
Promise<T | undefined>
The cached value, default value, or undefined if not found
Example:
const user = await cacher.get('user:123');
const settings = await cacher.get('settings', { theme: 'dark' });

set()

Stores a value in the cache.
async set<T>(key: string, value: T, ttl?: number): Promise<boolean>
key
string
required
The cache key to store the value under
value
T
required
The value to cache. Will be JSON-serialized before storage.
ttl
number
Time-to-live in seconds. Overrides the default TTL if provided.
return
Promise<boolean>
Returns true if the operation was successful, false otherwise
Example:
await cacher.set('user:123', { name: 'Alice', email: '[email protected]' });
await cacher.set('temp:token', 'abc123', 300); // Cache for 5 minutes

del()

Deletes a value from the cache.
async del(key: string): Promise<boolean>
key
string
required
The cache key to delete
return
Promise<boolean>
Returns true if the operation was successful, false otherwise
Example:
await cacher.del('user:123');

clear()

Clears all values from the cache.
async clear(): Promise<boolean>
return
Promise<boolean>
Returns true if the operation was successful, false otherwise
Example:
await cacher.clear();

getMultiple()

Retrieves multiple values from the cache at once.
async getMultiple<T>(keys: string[], defaultValue?: T): Promise<(T | undefined)[]>
keys
string[]
required
Array of cache keys to retrieve
defaultValue
T
Optional default value for keys that are not found
return
Promise<(T | undefined)[]>
Array of values in the same order as the keys. Missing keys return undefined or the default value.
Example:
const [user1, user2, user3] = await cacher.getMultiple([
  'user:1',
  'user:2',
  'user:3'
]);

setMultiple()

Stores multiple values in the cache at once.
async setMultiple<T>(values: Record<string, T>, ttl?: number): Promise<boolean>
values
Record<string, T>
required
Object mapping cache keys to values
ttl
number
Time-to-live in seconds for all values
return
Promise<boolean>
Returns true if all operations were successful, false otherwise
Example:
await cacher.setMultiple({
  'user:1': { name: 'Alice' },
  'user:2': { name: 'Bob' },
  'user:3': { name: 'Charlie' }
}, 3600);

delMultiple()

Deletes multiple values from the cache at once.
async delMultiple(keys: string[]): Promise<boolean>
keys
string[]
required
Array of cache keys to delete
return
Promise<boolean>
Returns true if all operations were successful, false otherwise
Example:
await cacher.delMultiple(['user:1', 'user:2', 'user:3']);

has()

Checks if a key exists in the cache.
async has(key: string): Promise<boolean>
key
string
required
The cache key to check
return
Promise<boolean>
Returns true if the key exists, false otherwise
Example:
if (await cacher.has('user:123')) {
  console.log('User is cached');
}

dispose()

Cleans up resources and closes connections to the cache store.
async dispose(): Promise<void>
Example:
await cacher.dispose();

Type Definitions

CacheOptions

interface CacheOptions {
  store?: CacheStore;
  defaultTtl?: number;
}

Usage Example

import { Cacher } from '@resolid/cache';
import { MemoryCache } from '@resolid/cache/stores';

const cacher = new Cacher({
  store: new MemoryCache(),
  defaultTtl: 3600
});

// Store data
await cacher.set('user:123', {
  id: 123,
  name: 'Alice',
  email: '[email protected]'
});

// Retrieve data
const user = await cacher.get('user:123');
console.log(user); // { id: 123, name: 'Alice', email: '[email protected]' }

// Check if exists
if (await cacher.has('user:123')) {
  console.log('User is cached');
}

// Batch operations
await cacher.setMultiple({
  'user:1': { name: 'Alice' },
  'user:2': { name: 'Bob' }
});

const users = await cacher.getMultiple(['user:1', 'user:2']);

// Cleanup
await cacher.dispose();

Source Code

Location: packages/cache/src/index.ts

Build docs developers (and LLMs) love