Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jtwebman/refresh-cache
A simple Javascript cache that refreshes by calling the load function at a set refresh time.
https://github.com/jtwebman/refresh-cache
Last synced: 11 days ago
JSON representation
A simple Javascript cache that refreshes by calling the load function at a set refresh time.
- Host: GitHub
- URL: https://github.com/jtwebman/refresh-cache
- Owner: jtwebman
- License: mit
- Created: 2015-10-02T04:22:43.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-08T16:56:41.000Z (about 9 years ago)
- Last Synced: 2024-09-23T04:09:08.270Z (about 2 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# An Auto Refreshing Cache
[![Build Status](https://travis-ci.org/jtwebman/refresh-cache.svg?branch=master)](https://travis-ci.org/jtwebman/refresh-cache) [![Coverage Status](https://coveralls.io/repos/jtwebman/refresh-cache/badge.svg?branch=master&service=github)](https://coveralls.io/github/jtwebman/refresh-cache?branch=master)
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:```Javascript
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.```Javascript
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.```Javascript
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.