Typed fast LRU cache for browser.
Implemented by the fast lru algorithm of hashlru. Added ttl and old browser support.
npm i @byungi/lru-cache
import LRUCache from '@byungi/lru-cache'
const cache = new LRUCache({max: 2, ttl: 100})
cache.set('a', 1)
console.log(cache.has('a')) // => true
cache.set('b', 2)
cache.set('c', 3)
cache.set('d', 4)
console.log(cache.has('a')) // => false
console.log(cache.has('d')) // => true
setTimeout(()=> {
// After ttl(100ms).
console.log(cache.has('d')) // => false
}, 100)
Create an instance.
max
- Maximum cache size. (Default:Infinity
)ttl
- Time to live. (Default:Infinity
)renewTTL
- Iftrue
, renewttl
when getting value. (Default:true
)
Set value by key.
Get value by key.
Returns whether a key exists.
Delete value by key
Delete all values.
MIT