npm install smartcache
Not every request you serve needs to be making a database call. For lots of public data, updating once every second is good enough. SmartCache is a little utility that lets you add data sources and decide how often they refresh. Then just read the data from the cache whenever you need to display it.
Add a new data source called name
. The cached value of name
will expire every expire
milliseconds. fn
should be of the form function(arg, cb)
where cb
is the callback used to return the value, and arg
is a (possibly null) argument passed from a get
call. Each value from this source with a different arg
will be cached seperately.
Get a value by name. cb
is called with one argument (the value). argument
is an optional parameter that will be passed to the data source if available for this request.
Set a value that expires after expire
milliseconds.
Stops all the refresh events from firing.
var cache = require('smartcache')(); // Get new cache
cache.add('news', function(arg, cb) {
db.getNewsItems(function(items) {
cb(newsTemplate.render(items));
});
}, 5000); // Every 5 seconds is good enough
function serveNews(req, res) {
cache.get('news', function(news) {
res.send(news);
});
}
An example passing arguments to the data source
cache.add('hi', function(arg, cb) {
cb('Hello ' + arg + '!');
}); // No expire argument; cached values never expire
function serveHello(req, res) {
cache.get('hi', 'Larry', function(m) {
res.send(m);
});
}