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.
ES6 notation
;; @ { return axios; } { console; }
More Examples (ES5)
const cache = ; const p = ; const cached_forever = p; const cached_by_5_mins = p const cached_by_5_mins_showing_loader_on_slow_requests = 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)
Persistance
Implemented (1) browser's localStorage and (2) React-Native AsyncStorage by now.
const cache = ;const storage = ; cache; //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 = ; cachevalidator; const p = ; const cached_never = p;