@byungi/lru-cache
v0.1.1
Published
Typed fast LRU cache for browser.
Downloads
6
Maintainers
Readme
@byungi/lru-cache
Typed fast LRU cache for browser.
Implemented by the fast lru algorithm of hashlru. Added ttl and old browser support.
Install
npm i @byungi/lru-cache
Example
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)
API
new LRUCache(options?)
Create an instance.
options
max
- Maximum cache size. (Default:Infinity
)ttl
- Time to live. (Default:Infinity
)renewTTL
- Iftrue
, renewttl
when getting value. (Default:true
)
cache.set(key, value)
Set value by key.
cache.get(key)
Get value by key.
cache.has(key)
Returns whether a key exists.
cache.delete(key)
Delete value by key
cache.clear()
Delete all values.
License
MIT