The smallest and fastest TTL cache implementation in JavaScript
# pnpm
pnpm add yocto-ttl-cache
# yarn
yarn add yocto-ttl-cache
# npm
npm i yocto-ttl-cache
import { TTLCache } from 'yocto-ttl-cache' // only named export is supported
const cache = new TTLCache(1000) // the `ttl` option of `constructor` is `1000` by default
cache.get('key') // `undefined`
cache.get('key', () => 'value') // get with `setter`, `'value'`
cache.get('key', () => 'unused') // `'value'` is not stale
cache.set('key', 'newValue')
cache.get('key') // `'newValue'`
setTimeout(() => {
cache.get('key') // `undefined`, stale after `ttl`
}, 1000)
1stG | RxTS | UnTS |
---|---|---|
1stG | RxTS | UnTS |
---|---|---|
Detailed changes for each release are documented in CHANGELOG.md.