smart-cachex
v1.0.0
Published
A tiny in-memory TTL/LRU cache for Node.js.
Readme
smart-cache
smart-cache is a tiny, dependency-free in-memory cache with TTL expiration and LRU eviction.
Install
npm install smart-cacheUsage
const TinyCache = require('smart-cache');
const cache = new TinyCache({ maxSize: 100, ttl: 60_000 });
cache.set('user:42', { name: 'Ada' });
cache.get('user:42'); // { name: 'Ada' }
cache.has('user:42'); // true
cache.delete('user:42');maxSize limits the number of entries. The least recently used entry is evicted when the limit is exceeded. ttl is measured in milliseconds; 0 disables expiration. Expired entries are removed lazily during cache access, writes, and metadata reads. The optional now function can provide a custom millisecond clock for deterministic tests.
API
new TinyCache(options?)set(key, value)returns the cache for chainingget(key)returns the value orundefinedhas(key)checks whether a non-expired value existsdelete(key)removes one entryclear()removes all entriessizereturns the number of non-expired entrieskeys()returns keys from least to most recently used
