Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
popular-cache
Advanced tools
An in-memory LRU cache with easy statistics and smart proxy mode.
npm install popular-cache --save
var pcache = require('popular-cache');
cache = pcache({
maxSize: 500,
maxAge: 1000 * 60 * 60 // in millisecond
}),
smallCache = pcache(50); // or simply sets max size
// basic usage
cache.set("key", "value")
cache.get("key")
cache.del("key") // delete key
cache.size() // get size
cache.reset() // resets cache
cache.hits() // get total hits
cache.misses() // get total misses
// print the most popular keys and the number of hits
cache.popular(function(key, value, hits){
console.log(key + ': ' + hits);
}, 5);
// print the most recently used keys and the number of hits
cache.recent(function(key, value, hits){
console.log(key + ': ' + hits);
}, 10);
Proxy mode is as simple as telling popular-cache what you want and then you just get and get. Cache misses and concurrent requests are handled automatically.
// some time consuming process like HTTP request
var httpRequest = function(key, callback){
setTimeout(function(){
if(key=='popular-cache') callback('I am popular-cache');
else callback('others');
}, 1000);
}
// use the proxied cache returned by proxy()
var cache = pcache(50).proxy(httpRequest);
cache.get('popular-cache', function(value){
console.log(value); // I am popular-cache
});
cache.get('another key', function(value){
console.log(value); // others
});
Note that the value is not returned directly in proxy mode. Instead, it's returned via callback.
set(key, value)
get(key) (Normal Mode)
del(key)
size()
reset()
hits()
misses()
recent(function(key, value, hits), [limit])
key
: the key of the entry.value
: the value of the entry.hits
: the number of hits of the entry.limit
: optional, the maximum number of iterations.popular(function(key, value, hits), [limit])
key
: the key of the entry.value
: the value of the entry.hits
: the number of hits of the entry.limit
: optional, the maximum number of iterations.proxy(function(key, callback(value), [context]))
Build and return a proxied cache.
The proxy function function(key, callback(value), [context])
will be called automatically on cache misses.
key
and context
is passed from get(key, function(value), [context])
directly. context
is optional and could be anything that is useful for the proxy function.
var cache = pcache().proxy(function(key, callback, context){
callback(key + ' proxy ' + context);
});
cache.get('hello', function(value){
console.log(value); // hello proxy world
}, 'world');
get(key, function(value), [context]) (only in proxy mode)
get(key)
except how the value is returned. function(value)
is required in proxy mode to receive the value.context
is only useful on cache misses and will be passed to the proxy function directly.FAQs
An in-memory LRU cache with built-in statistics and proxy mode.
The npm package popular-cache receives a total of 1 weekly downloads. As such, popular-cache popularity was classified as not popular.
We found that popular-cache 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
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.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.