Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

freshen-up

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

freshen-up

Cache objects for a specified TTL. After that, freshen them up!

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

freshen-up

Build Status

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; // will be something
config.get().someValue; // 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 = require('freshen-up');

function pingGoogleDotComToCheckWhetherWeHaveInternetAccess() {
  // will return true or false
};

var ping = freshenUp(pingGoogleDotComToCheckWhetherWeHaveInternetAccess);

ping.get(); // true

// disconnect from the network

ping.get(); // false

By default, freshen-up refreshes every 50ms, but you can override this setting by just specifying your custom interval:

// set an interval of 1s
var ping = freshenUp(pingGoogleDotComToCheckWhetherWeHaveInternetAccess, 1000);

ping.get(); // true

// disconnect from the network
// the value of the function hasn't been refreshed yet
ping.get(); // true

// if we check after 1s, the value has changed
setTimeout(function(){
  ping.get(); // false
}, 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.

Keywords

cache

FAQs

Package last updated on 20 Feb 2015

Did you know?

Socket

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.

Install

Related posts