1.0.0 • Published 2 years ago
@neumatter/lru-cache v1.0.0
LRUCache
A simple LRUCache with maxAge and allowStale options.
Table of Contents
Install
npm i @neumatter/lru-cacheUsage
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.notFoundReturnValueLRUCache.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.notFoundReturnValueLRUCache.set:
cache.set('/key', 99)LRUCache.clear:
cache.clear()LRUCache.delete:
const isDeleted = cache.delete('/key') // returns boolean1.0.0
2 years ago