Drivers
@mongez/cache — Driver Configuration
How to use
Bootstrapping — setCacheConfigurations
Call this once, early (e.g. in your app entry point), before any cache.* call.
import cache, { PlainLocalStorageDriver, setCacheConfigurations,} from "@mongez/cache";
setCacheConfigurations({ driver: new PlainLocalStorageDriver(), prefix: "myapp-", // prepended to every key on disk expiresAfter: 60 * 60, // 1-hour default TTL (seconds); omit for no expiry});Full CacheConfigurations type:
type CacheConfigurations = { driver: CacheDriverInterface; prefix?: string; expiresAfter?: number; // seconds; default is Infinity (no expiry) valueConverter?: (value: any) => any; // replaces JSON.stringify valueParer?: (value: any) => any; // replaces JSON.parse (note: typo in type name is intentional) encryption?: { encrypt: (value: any) => any; decrypt: (value: any) => any; };};PlainLocalStorageDriver
Reads and writes window.localStorage. Values are wrapped in a {data, expiresAt} JSON envelope before storage.
import { PlainLocalStorageDriver, setCacheConfigurations } from "@mongez/cache";
setCacheConfigurations({ driver: new PlainLocalStorageDriver(), prefix: "shop-", expiresAfter: 60 * 60 * 24, // 24 hours});PlainSessionStorageDriver
Identical contract to PlainLocalStorageDriver but backed by window.sessionStorage. Data is lost when the tab is closed.
import { PlainSessionStorageDriver, setCacheConfigurations } from "@mongez/cache";
setCacheConfigurations({ driver: new PlainSessionStorageDriver(), prefix: "wizard-",});RunTimeDriver
In-memory map. No Web Storage dependency — safe for tests and SSR. Data is gone when the page reloads or the process exits.
import { RunTimeDriver, setCacheConfigurations } from "@mongez/cache";
setCacheConfigurations({ driver: new RunTimeDriver() });Two RunTimeDriver instances are independent: they do not share any global store.
SSR-safe driver selection
localStorage and sessionStorage do not exist in Node. Gate driver selection:
import { PlainLocalStorageDriver, RunTimeDriver, setCacheConfigurations } from "@mongez/cache";
const driver = typeof window === "undefined" ? new RunTimeDriver() : new PlainLocalStorageDriver();
setCacheConfigurations({ driver, prefix: "ssr-" });Custom serialization
The default encoder/decoder is JSON.stringify / JSON.parse. Override globally:
setCacheConfigurations({ driver: new PlainLocalStorageDriver(), valueConverter: (v) => mySerialize(v), valueParer: (v) => myDeserialize(v), // note: key is `valueParer` (single r), not `valueParser`});Or per driver instance (chainable):
const driver = new PlainLocalStorageDriver();driver .setValueConverter((v) => mySerialize(v)) .setValueParser((v) => myDeserialize(v));Multiple CacheManager instances
The default cache export is a singleton. For isolated concerns (e.g. session state vs. long-lived prefs), create a second manager:
import { CacheManager, PlainSessionStorageDriver, PlainLocalStorageDriver } from "@mongez/cache";
const sessionCache = new CacheManager();sessionCache.setDriver(new PlainSessionStorageDriver());sessionCache.setPrefixKey("session-");
const prefsCache = new CacheManager();prefsCache.setDriver(new PlainLocalStorageDriver());prefsCache.setPrefixKey("prefs-");Building a custom driver
Extend BaseCacheEngine and point storage at any object that exposes getItem / setItem / removeItem / clear. The base class handles the expiry envelope, prefix, and corruption recovery.
import { BaseCacheEngine } from "@mongez/cache";
class CookieDriver extends BaseCacheEngine { public storage = { getItem: (k: string) => getCookie(k) ?? null, setItem: (k: string, v: string) => setCookie(k, v), removeItem: (k: string) => deleteCookie(k), clear: () => clearAllCookies(), };}
setCacheConfigurations({ driver: new CookieDriver() });Key details / Pitfalls
setCacheConfigurationsis not idempotent in every case. It merges into an internalconfigurationobject, but theprefixand serializers are applied to the driver directly when the call is made. Calling it a second time with a new driver will push new config to the new driver, not re-apply the old prefix.prefixaffects the raw storage key, not the key you pass toget/set. Pass the bare key to all methods; the prefix is injected automatically.expiresAfter: 0disables expiry (falsy check in the base engine). Useundefinedor omit the key to get the same result.valuePareris misspelled in theCacheConfigurationstype (oner). UsevalueParer— notvalueParser— when passing it tosetCacheConfigurations. The per-driver method is correctly namedsetValueParser.- Encrypted drivers require the
encryptionkey insetCacheConfigurations. Without it,encrypt/decryptareundefinedand the driver will throw. See theencrypted-cacheskill for the full setup.