A simple LRU cache supporting O(1) set, get and eviction of old keys
Fork of lru
to remove dependency on events
.
Installation
$ npm install nanolru
Example
var LRU = ; var cache = 2 cache;cache; //=> bar cache;cache; //=> bar2 cache; // 'foo' evictedcache; // => 'bar3'cache // => 'bar2'cache // => undefinedcachelength // => 1cachekeys // => ['foo3'] cacheclearcachelength // => 0cachekeys // => []
API
LRU( length )
Create a new LRU cache that stores length
elements before evicting the least recently used.
Optionally you can pass an options map with additional options:
max: maxElementsToStore maxAge: maxAgeInMilliseconds
If you pass maxAge
items will be evicted if they are older than maxAge
when you access them.
Returns: the newly created LRU cache
Properties
.length
The number of keys currently in the cache.
.keys
Array of all the keys currently in the cache.
Methods
.set( key, value )
Set the value of the key and mark the key as most recently used.
Returns: value
.get( key )
Query the value of the key and mark the key as most recently used.
Returns: value of key if found; undefined
otherwise.
.peek( key )
Query the value of the key without marking the key as most recently used.
Returns: value of key if found; undefined
otherwise.
.remove( key )
Remove the value from the cache.
Returns: value of key if found; undefined
otherwise.
.clear()
Clear the cache.
Credits
Original library by Chris O'Hara. A big thanks to Dusty Leary who finished the library.
License
MIT