A simple LRUCache with maxAge and allowStale options.
npm i @neumatter/lru-cacheimport 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
})// All Options set to default
const value = cache.get('/key', { allowStale: false }) // returns value or cache.notFoundReturnValueSame 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.notFoundReturnValuecache.set('/key', 99)cache.clear()const isDeleted = cache.delete('/key') // returns boolean