Skip to main content

Quik Framework :: Cache

Provides a pluggable caching abstraction built on the framework's feature system. Ships with an in-memory implementation that supports per-entry TTLs. Replace it with any backend (Redis, Memcached, etc.) by registering a custom IQCache implementation through the core feature registry.

Installation

pnpm add @quik/cache

Usage

Basic get/set

import { getCache } from '@quik/cache';

const cache = getCache();

await cache.set('user:1', { name: 'Alice' }, 60_000); // 60 s TTL
const user = await cache.get<{ name: string }>('user:1');

if (await cache.has('user:1')) {
await cache.delete('user:1');
}

await cache.clear(); // wipe all entries

Module setup

Call Module.setup() during bootstrap to register the in-memory cache as the active IQCache feature:

import { Module as CacheModule } from '@quik/cache';

await CacheModule.setup({ /* bootstrap options */ });

Custom backend

import { initFeature } from '@quik/core';
import type { IQCache } from '@quik/cache';

class RedisCache implements IQCache {
async get<T>(key: string): Promise<T | undefined> { /* … */ }
async set<T>(key: string, value: T, ttlMs?: number): Promise<void> { /* … */ }
async delete(key: string): Promise<void> { /* … */ }
async has(key: string): Promise<boolean> { /* … */ }
async clear(): Promise<void> { /* … */ }
}

initFeature('IQCache', new RedisCache());

Configuration

  • cache.defaultTtlMs — default TTL applied when no ttlMs is passed to set(). 0 means no expiry (default).

API Highlights

  • getCache() — returns the active IQCache instance (from @quik/core).
  • QInMemoryCache — Map-backed implementation with lazy TTL eviction.
  • IQCache — interface to implement for custom backends.

Testing & Coverage

See the root instructions for details on running pnpm run test:coverage and accessing coverage artifacts.

API Reference

Generated API documentation is available in the cache API section.