@dcl/memory-cache-component
v2.4.2
Published
In-memory cache component for core components library
Readme
@dcl/memory-cache-component
In-memory cache component using LRU cache for local caching.
Installation
npm install @dcl/memory-cache-componentUsage
import { createInMemoryCacheComponent } from '@dcl/memory-cache-component'
const cache = createInMemoryCacheComponent()
await cache.set('key', value, 3600)
const value = await cache.get('key')You can override the cap and the default TTL by passing an options bag:
// Larger cap, no implicit expiration (entries live until LRU evicts them).
const cache = createInMemoryCacheComponent({ max: 1_000_000, ttl: 0 })Features
- LRU (Least Recently Used) eviction policy
- TTL support for automatic expiration
- Pattern-based key filtering
- No external dependencies (local only)
- Fast access times
Configuration
createInMemoryCacheComponent(options?) accepts:
max— maximum number of items the cache will hold. Must be a positive integer. Defaults to10_000.ttl— default TTL in milliseconds applied to every entry. Defaults to1000 * 60 * 60(1 hour). Pass0to disable TTL entirely so entries live until evicted by the LRU cap. Must not be negative.
Invalid values (non-integer or non-positive max, negative or non-finite ttl) throw a TypeError at construction time so misuse surfaces immediately.
TTL units — careful
The two ttl parameters in this API use different units:
| Where | Unit | Example |
| --- | --- | --- |
| createInMemoryCacheComponent({ ttl }) (constructor default) | milliseconds | ttl: 60_000 → 60 seconds |
| cache.set(key, value, ttl) (per-call override) | seconds | ttl: 60 → 60 seconds |
| cache.setInHash(key, field, value, ttlInSecondsForHash) | seconds | ttlInSecondsForHash: 60 → 60 seconds |
The per-call values are passed through fromSecondsToMilliseconds internally; only the constructor option is in milliseconds. Watch the unit when switching between defaults and overrides.
License
MIT
