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 = ;{// ...// ...};var config = ;configsomeValue; // will be somethingconfigsomeValue; // will be something else
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 = ;{// will return true or false};var ping = ;ping; // true// disconnect from the networkping; // false
By default, freshen-up
refreshes every 50ms, but
you can override this setting by just specifying your
custom interval:
// set an interval of 1svar ping = ;ping; // true// disconnect from the network// the value of the function hasn't been refreshed yetping; // true// if we check after 1s, the value has changed;
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.