freshen-up

The stupidest JS utility you can ever think of:
freshen-up
lets you cache the product of a
function and refresh its value at a given interval.
It might be useful when you need to cache some
values (reading files from the filesystem, API result)
and want to refresh those values every now and then.
Installlation
npm install freshen-up
Usage
The library is pretty straightforward:
var freshenUp = require('freshen-up');
function loadConfigurationFromTheDatabase() {
};
var config = freshenUp(loadConfigurationFromTheDatabase);
config.get().someValue;
config.get().someValue;
As you might understand, freshen-up
accepts your
function and exposes the get
method to access the
value of that function.
Another example:
var freshenUp = require('freshen-up');
function pingGoogleDotComToCheckWhetherWeHaveInternetAccess() {
};
var ping = freshenUp(pingGoogleDotComToCheckWhetherWeHaveInternetAccess);
ping.get();
ping.get();
By default, freshen-up
refreshes every 50ms, but
you can override this setting by just specifying your
custom interval:
var ping = freshenUp(pingGoogleDotComToCheckWhetherWeHaveInternetAccess, 1000);
ping.get();
ping.get();
setTimeout(function(){
ping.get();
}, 1000);
Tests
You can simply run mocha
and meet greenland.
Alternatives
Cache invalidation. But that's too much of a problem.
But yeah, seriously, you can think of a 100 ways to
do this more efficiently.