@resolid/cache
v1.0.0
Published
Type-safe Async Cache for TypeScript
Readme
Type-safe Async Cache for TypeScript
Documentation | Framework Bundle
A fully-typed, flexible cache system for modern TypeScript projects. Supports single and batch operations, optional TTL, and pluggable storage backends. Designed for libraries, frameworks, and applications needing predictable async caching.
Feature
- Fully typed with TypeScript — no
any. - Supports get/set/del/clear operations.
- Supports batch operations: getMultiple, setMultiple, delMultiple.
- Optional TTL for automatic expiration.
- Pluggable store backend (default is
nullCache). - Detects existence of keys via
has. - Handles disposal of resources via
dispose.
Installation
pnpm add @resolid/cache
# or
npm install @resolid/cache
# or
yarn add @resolid/cache
# or
bun add @resolid/cacheUsage
import { Cacher } from "@resolid/cache";
const cache = new Cacher({ defaultTtl: 1000 });
// Single set/get
await cache.set("foo", { a: 1 });
const value = await cache.get("foo"); // -> { a: 1 }
// Check existence
const exists = await cache.has("foo"); // -> true
// Batch set/get
await cache.setMultiple({ key1: 1, key2: 2 });
const values = await cache.getMultiple(["key1", "key2"]); // -> [1, 2]
// Delete
await cache.del("foo");
await cache.delMultiple(["key1", "key2"]);
// Clear all
await cache.clear();
// Dispose store if necessary
await cache.dispose();Options
export interface CacheOptions {
store?: CacheStore;
defaultTtl?: number;
}Store Interface
Your custom store should implement CacheStore:
export interface CacheStore {
get(key: string): Promise<string | undefined>;
set(key: string, value: string, ttl?: number): Promise<boolean>;
del(key: string): Promise<boolean>;
clear(): Promise<boolean>;
getMultiple?(keys: string[]): Promise<(string | undefined)[]>;
setMultiple?(values: Record<string, string>, ttl?: number): Promise<boolean>;
delMultiple?(keys: string[]): Promise<boolean>;
has?(key: string): Promise<boolean>;
dispose?(): Promise<void> | void;
}License
MIT License (MIT). Please see LICENSE for more information.
