Cache System
ZinTrust provides a unified, multi-driver cache system that allows you to store and retrieve data efficiently across different environments.
Configuration
The cache system is configured via environment variables in your .env file or Env class.
CACHE_DRIVER=memory # memory, kv, redis, mongodb
REDIS_HOST=localhost
REDIS_PORT=6379
MONGO_URI=your_mongo_uriBasic Usage
You can use the cache helper to interact with the configured cache driver.
import { cache } from '@zintrust/core';
// Store an item (TTL in seconds)
await cache.set('user:1', { id: 1, name: 'John' }, 3600);
// Retrieve an item
const user = await cache.get('user:1');
// Check if item exists
if (await cache.has('user:1')) {
// ...
}
// Delete an item
await cache.delete('user:1');
// Clear all items (if supported by driver)
await cache.clear();Supported Drivers
Memory Driver (memory)
The default driver for local development. It stores data in a simple JavaScript Map.
Cloudflare KV Driver (kv)
Designed for Cloudflare Workers.
- Expected Workers binding name:
CACHE - The Workers environment is exposed to framework code via
globalThis.env(set by the Cloudflarefetch()entrypoint)
Redis Driver (redis)
A zero-dependency implementation that communicates with Redis over TCP using the RESP protocol.
If you prefer using the official Redis client in Node.js, install the modular driver:
zin add cache:redisMongoDB Driver (mongodb)
Uses the MongoDB Atlas Data API (HTTPS) for zero-dependency integration, making it ideal for serverless environments where TCP connections are limited.
Install the modular driver package:
zin add cache:mongodbAutomatic Serialization
The cache system automatically handles JSON serialization and deserialization for complex objects, so you can store arrays and objects directly.