Cache
In-memory caching for performance
OGX includes Cache v2, a high-performance LRU cache that stores rendered images by content hash.
How It Works
When cache: true (default), OGX:
- Generates a fast FNV-1a hash of your config
- Checks the LRU cache for an existing image
- Returns cached image or renders a new one
- Evicts least-recently-used entries when memory limit is reached
Usage
Caching is enabled by default:
const png = await ogx({
preset: 'docs',
title: 'Cached Image',
cache: true, // default
});Disable for dynamic content:
const png = await ogx({
preset: 'social',
title: 'Always Fresh',
cache: false,
});Direct Access
For advanced use cases, access the cache directly:
import { snapshotCache, hashObject } from '@ogxjs/core';
// Generate hash
const hash = hashObject({ preset: 'docs', title: 'Test' });
// Check if cached
const cached = snapshotCache.get(hash);
// Store manually
snapshotCache.set(hash, pngBuffer);
// Get statistics
const stats = snapshotCache.stats;
console.log(stats.hitRate); // Cache hit rate
// Clear all
snapshotCache.clear();Configuration
Configure cache behavior globally:
import { configureSnapshotCache } from '@ogxjs/core';
configureSnapshotCache({
maxSize: 500, // Max entries (default: 500)
ttl: 3600000, // Time to live in ms (0 = no expiry)
});Performance
Cache v2 provides sub-millisecond retrieval (~0.03ms average). First renders take 40-55ms depending on preset complexity.