@neumatter/lru-cache
v1.0.0
Published
A simple LRUCache with maxAge and allowStale options.
Downloads
9
Maintainers
Readme
LRUCache
A simple LRUCache with maxAge and allowStale options.
Table of Contents
Install
npm i @neumatter/lru-cache
Usage
LRUCache
:
import LRUCache from '@neumatter/lru-cache'
type LRUCacheOptions = {
maxSize?: number,
maxAge?: number,
allowStale?: boolean,
sizeCalculation?: (value: any, key: any) => number,
notFoundReturnValue?: any
}
// All Options set to default
const cache = new LRUCache({
maxSize: 1e4,
maxAge: Infinity,
allowStale: false
sizeCalculation: (value, key) => 1,
notFoundReturnValue: undefined
})
LRUCache.get
:
// All Options set to default
const value = cache.get('/key', { allowStale: false }) // returns value or cache.notFoundReturnValue
LRUCache.peek
:
Same as LRUCache.get
but it doesn't change the order of the entries.
// All Options set to default
const value = cache.peek('/key', { allowStale: false }) // returns value or cache.notFoundReturnValue
LRUCache.set
:
cache.set('/key', 99)
LRUCache.clear
:
cache.clear()
LRUCache.delete
:
const isDeleted = cache.delete('/key') // returns boolean