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

promise-cache-decorator

v0.3.14

Published

Two-level cache for functions returning promise (i.e. it memoizes promises), with "wait-a-little" event, adjustable persistence and cache invalidation strategies.

Downloads

94

Readme

promise-cache-decorator

Two-level cache for functions returning promise (i.e. it memoizes promises), with "wait-a-little" event, adjustable persistence and cache invalidation strategies.

Coverage Status Build Status

ES6 notation

import * as cache from 'promise-cache-decorator';
import axios from 'axios';

class API {

    @cache({
      type:"once-a-day",
      time:"14:00",
      tardy:"loader" //will call this.loader() if requests lasts more then 1 second
    })
    getMoscowWeather(){
      return axios.get('http://apidev.accuweather.com/locations/v1/search?q=Moscow,%20RU&apikey=hoArfRosT1215');
    }
    
    loader(){
      console.log("loading...");
    }
}

More Examples (ES5)

const cache = require('promise-cache-decorator');

const p = (url) => require('axios').get(url);

const cached_forever = cache()(p);

const cached_by_5_mins = cache({type:"age", maxAge:5*60*1000})(p)

const cached_by_5_mins_showing_loader_on_slow_requests = cache({
  type:"age", 
  maxAge:5*60*1000,//5 minutes
  tardy : show_loader // will call this handler in 1 second 
                      //(if Promise was not resolved earlier)
})(p);

//will request the weather from openweathermap 
//(with loader if needed) for the first time,
//but will get data from cache for the second time 
//(nevertheless after 5 mins will send request to update data)

cached_by_5_mins_showing_loader_on_slow_requests('http://apidev.accuweather.com/locations/v1/search?q=Moscow,%20RU&apikey=hoArfRosT1215')
.then(res => {
  show_the_weather(res);
})
.catch(err => {
  show_the_error(err);
})

Persistance

Implemented (1) browser's localStorage and (2) React-Native AsyncStorage by now.

const cache = require('promise-cache-decorator');
const storage = require('promise-cache-decorator/lib/storage/asyncStorage');

cache.setSettings({storage});

//1. will try to load() item from storage if cache's get() returns undefined
//2. will call save() if promise resolves
//3. will call remove() if item invalidated (including removing just after load if item is invalid)

You can implement and use your own storage - just look at lib/storage folder for examples.

Cache invalidation

There are three strategies by default: (1) keep forever, (2) invalidate by timeout (see example above) and (3) update 'once-a-day' after given time of day (see tests for example).

But you can add your own strategy:

const cache = require('promise-cache-decorator');

cache.validator.register("always-miss", function invalid(item, opt){
               // 'item' is cache item (like {value:3, ts:1523047229332})
               // 'opt' is first argument at cache() call (i.e. it is parameters of cache)
               // (see example with 'tardy' handler)
  return true; // 'true' mean that cache item is invalid
});

const p = (url) => require('axios').get(url);

const cached_never = cache("always-miss")(p);