npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

refresh-cache

v1.0.2

Published

A simple Javascript cache that refreshes by calling the load function at a set refresh time.

Downloads

10

Readme

An Auto Refreshing Cache

Build Status Coverage Status

Do you have a databases query that you want to cache the results for a few minutes? Do you have data in your system that only needs to be reloaded a few times a day? Then this is the caching library for you.

All you have to do is get a new cache and pass it a loader function. It will call the loader right away and keep calling it ever 5 minutes or whatever TTL (Time To Live in milliseconds) you pass it in the options. Here is an example:

var Cache = require('refresh-cache');

var myCache = new Cache({
  loader: function() {
    //... load my data
    return data;
  },
  ttl: 1800000 // 30 minutes in ms
});

// The library uses promises so you will need to call .then to get the data
myCache.get().then(function(myData) {
  console.log(myData); //write out the data
});

// You can force a reload anytime by calling reload

// This will not wait for the data to reload but return after starting it
myCache.reload();

// This will wait for the reload and then fill the promise
myCache.reload().then(function() {
  // data is fully reloaded
});

If you want more complex get logic then getting all the data from load you can pass a getter function on the options and get a single record for an array.

var Cache = require('refresh-cache');
var _ = require('lodash');

var myCache = new Cache({
  loader: function() {
    return {
      { id: 1, name: 'test 1' },
      { id: 2, name: 'test 2' },
      { id: 3, name: 'test 3' },
    };
  },
  ttl: 1800000, // 30 minutes in ms

  // getter gets the data as the first argument and then whatever you pass to get
  getter: function(tests, id) {
    return _.find(tests, function(test) {
      return test.id === id;
    });
  }
});P

// now you can call get passing in an id
myCache.get(2).then(function(test) {
  console.log(test); //write out { id: 2, name: 'test 2' }
});

For the most part the cache swallows errors so it is up to you to handle then. The lastLoadError is a property on the cache but you can also pass an errorCallback in on the options and it will be called for all errors. We use the callback as the data refreshes in the background so you can log it however you do it in your system.

var Cache = require('refresh-cache');

var myCache = new Cache({
  loader: function() {
    //... load my data but sometimes I fail
    return data;
  },
  errorCallback: function(err) {
    //Log my error
  }
});

Also as a side note if you are not using a loader that returns a promise the exception might not always be caught. I would suggest wrapping you loader in a try catch to handle issues there or using Bluebird library Promise.promisify function.