Drivers
Cache Drivers
Multiple cache backends with a unified driver interface
Available Drivers
| Driver | Best For | Persistence | Distributed |
|---|---|---|---|
| MemoryCache | Development, single-instance | No | No |
| CloudflareKVCache | Cloudflare Workers | Yes | Yes |
Choosing a Driver
Development / Testing
Use MemoryCache for local development. It requires no external dependencies and provides instant cache operations.
import { MemoryCache } from "viborm";
const client = createClient({
schema,
driver,
cache: new MemoryCache(),
});Production - Cloudflare Workers
Use CloudflareKVCache when deploying to Cloudflare Workers. It provides distributed caching across Cloudflare's edge network.
import { CloudflareKVCache } from "viborm";
const client = createClient({
schema,
driver,
cache: new CloudflareKVCache(env.MY_KV_NAMESPACE),
});Production - Other Environments
For other production environments (Node.js, Vercel, AWS Lambda), implement a custom driver using Redis, Upstash, or another distributed cache. See Custom Drivers.
Driver Interface
All cache drivers extend the abstract CacheDriver class and implement four methods:
abstract class CacheDriver {
protected abstract get<T>(key: string): Promise<CacheEntry<T> | null>;
protected abstract set<T>(key: string, storageTtl: number, entry: CacheEntry<T>): Promise<void>;
protected abstract delete(keys: string[]): Promise<void>;
protected abstract clear(prefix: string): Promise<void>;
}This interface ensures consistent behavior across all cache backends.