Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
A simple LRU cache supporting O(1) set, get and eviction of old keys
$ npm install lru
var LRU = require('lru');
var cache = new LRU(2),
evicted
cache.on('evict',function(data) { evicted = data });
cache.set('foo', 'bar');
cache.get('foo'); //=> bar
cache.set('foo2', 'bar2');
cache.get('foo2'); //=> bar2
cache.set('foo3', 'bar3'); // => evicted = { key: 'foo', value: 'bar' }
cache.get('foo3'); // => 'bar3'
cache.remove('foo2') // => {key: 'foo2', value: 'bar2'}
cache.remove('foo4') // => undefined
cache.length // => 1
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
.length
The number of keys currently in the cache.
.set( key, value )
Set the value of the key and mark the key as most recently used.
Returns: value
.set( key, value )
Set the value of the key and mark the key as most recently used.
.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 key/value pair from the cache.
Returns: an associative array containing {key: <key>, value: <value>}
.on( event, callback )
Respond to events. Currently only the evict
event is implemented. When a key is evicted, the callback is executed with an associative array containing the evicted key: {key: key, value: value}
.
A big thanks to Dusty Leary who finished the library.
MIT
FAQs
A simple O(1) LRU cache
The npm package lru receives a total of 24,034 weekly downloads. As such, lru popularity was classified as popular.
We found that lru demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.