node-dummy-cache
A simple in memory cache to use with javascript.
Installation
node.js
npm install node-dummy-cache
Import inside your code:
var cacheFactory = ;
HTML
Download lib/dummy-cache.js.
Import inside HTML:
For old browsers you will also need json2.js.
Usage
Simple key/value
var users = cacheFactory; users; var user = users;
Function style
Before:
{ // Do complex stuff here ;}; ;
Adding cache:
var dummy = cacheFactory; ;
Promise style
Before:
{ return { // Do complex stuff here ; };}; ;
Adding cache:
var dummy = cacheFactory; ;
Fetch when needed (callback style)
var users = cacheFactory; users;
Fetch when needed (promise style)
var users = cacheFactory; users;
Mixed
var users = cacheFactory; ; users; users; var user = users; // Returns only if cached user = ; // Returns only if cached
Complex
var users = cacheFactory; users; var user = users; // Returns the user only if cached users; // Adds the user, but no date
API
All arguments passed to the get / put must be JSON serializable.
cacheFactory.create({ type: string, maxAliveTimeMS: number, maxNotAccessedTimeMs: number, onErrorMaxAliveTimeMs: number }, fetcher: function)
Creates a new cache.
This is the base constructor, all the other ones are shortcuts to this.
Params:
- An object with the cache configuration. The available fields are:
- type : one of cacheFactory.CALLBACK or cacheFactory.PROMISE. The default is CALLBACK.
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation. The default is forever.
- maxNotAccessedTimeMs : Max time a value will stay in cache after its last access. The default is to only consider alive time.
- onErrorMaxAliveTimeMs : Max time a value will stay in cache when an error is returned. The default is the same as maxAliveTimeMS.
- fetcher : callback to fetch the data
cacheFactory.create(type: string, maxAliveTimeMS: number, maxNotAccessedTimeMs: number, fetcher: function)
Creates a new cache.
Params:
- type : one of cacheFactory.CALLBACK or cacheFactory.PROMISE
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
- maxNotAccessedTimeMs : Max time a value will stay in cache after its last access
- fetcher : callback to fetch the data
cacheFactory.create(maxAliveTimeMS: number, maxNotAccessedTimeMs: number, fetcher: function)
Creates a new cache. Fetcher expects a callback.
Params:
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
- maxNotAccessedTimeMs : Max time a value will stay in cache after its last access
- fetcher : callback to fetch the data
cacheFactory.create(type: string, maxAliveTimeMS: number, fetcher: function)
Creates a new cache.
Params:
- type : one of cacheFactory.CALLBACK or cacheFactory.PROMISE
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
- fetcher : callback to fetch the data
cacheFactory.create(maxAliveTimeMS: number, fetcher: function)
Creates a new cache. Fetcher expects a callback.
Params:
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
- fetcher : callback to fetch the data
cacheFactory.create(maxAliveTimeMS: number, maxNotAccessedTimeMs: number)
Creates a new cache. All values must be added to cache using put.
Params:
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
- maxNotAccessedTimeMs : Max time a value will stay in cache after its last access
cacheFactory.create(fetcher: function)
Creates a new cache. Fetcher expects a callback. Values never expires.
Params:
- fetcher : callback to fetch the data
cacheFactory.create(maxAliveTimeMS: number)
Creates a new cache. All values must be added to cache using put.
Params:
- maxAliveTimeMS : Max time a value will stay in cache starting with its creation
cacheFactory.create()
Creates a new cache. Values never expires. All values must be added to cache using put.
License
MIT. Check LICENSE file.