Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
async-lru
Advanced tools
Readme
Also works in the browser with browserify!
npm install async-lru
const AsyncLRU = require('async-lru')
const fs = require('fs')
const lru = new AsyncLRU({
max: 2,
load: (key, cb) => {
fs.readFile(key, cb)
}
})
lru.get('file.txt', (err, value) => { // not in cache, calls load()
lru.get('file.txt', (err, value) => { // cached, will NOT call load()
// ...
})
})
lru
Since values are fetched asynchronously, the get
method takes a callback, rather
than returning the value synchronously.
While there is a set(key, value)
method to manually seed the cache, typically
you'll just call get
and let the load
function fetch the key for you.
Keys must uniquely identify a single object, and must contain all the information required to fetch an object.
lru = AsyncLRU(opts)
Create a new AsyncLRU cache. You must pass an options map with a load
option:
{
load: function (key, callback) {
callback(null, 'value') // get the data from an asyncronous store
}
}
Optional options:
{
max: maxElementsToStore,
maxAge: maxAgeInMilliseconds
}
If you pass max
, items will be evicted if the cache is storing more than max
items.
If you pass maxAge
, items will be evicted if they are older than maxAge
when you access them.
Returns: the newly created AsyncLRU cache
lru.length
The number of keys currently in the cache.
lru.keys
Array of all the keys currently in the cache.
lru.set(key, value)
Set the value of the key and mark the key as most recently used.
Returns: value
lru.get(key, [loadArgs], callback)
Query the value of the key and mark the key as most recently used.
If the key is in the cache, then calls callback(null, cached)
on nextTick
.
Otherwise, calls load(key, callback)
where load
is the function that was
supplied in the options object. If it doesn't return an error, then cache the
result. Multiple get
calls with the same key
will only ever have a single
load
call at the same time.
Optionally, specify loadArgs
if you want a custom array of arguments to be passed
into load
instead of key
, like load.apply(null, loadArgs.concat(callback))
.
lru.peek(key)
Query the value of the key without marking the key as most recently used.
Returns: value of key if found; undefined
otherwise.
lru.remove(key)
Remove the value from the cache.
Returns: value of key if found; undefined
otherwise.
lru.clear()
Clear the cache. This method does NOT emit the evict
event.
lru.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}
.
MIT. Copyright (c) Feross Aboukhadijeh.
FAQs
A simple async LRU cache supporting O(1) set, get and eviction of old keys
The npm package async-lru receives a total of 189 weekly downloads. As such, async-lru popularity was classified as not popular.
We found that async-lru demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.