0.3.14 • Published 6 years ago

promise-cache-decorator v0.3.14

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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);
0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago