README
brainz
In-memory cache made easy.
Instalation
You can manually download it from dist
folder or get it through npm
/ bower
.
Npm
npm install --save brainz
Bower
bower install --save brainz
Documentation
new Brainz(obj, set, limit, time)
Brainz constructor.
arguments
obj
- Object that contains the cached items.set(obj)
- Function that will be executed after therequest
resolve. The first argument is the updatedobj
. not requiredlimit
- Maximum number of items that will be cached inobj
. not required , default: 10000time
- Time in milliseconds that items will be considerated valid inobj
. not required , default: 36000000
brainz(key, request, [callback])
Caching requests.
arguments
key
- key is a string used to identify stored values in cache.request(callback)
- Function that will be executed casekey
doesn't exists or expired.callback(err, result)
- Callback that will be executed afterrequest
resolve or found value fromobj
.
Examples
The following identifiers will be used for all examples:
var formula = "..something..";
var cache = //.. object that contains the cached values
function calculate(callback) {
//..do some calc, then callback(err, result);
}
function calcCallback(err, result) {
//.. do something with the calculated result
}
1 Common usage:
var brainz = new Brainz(cache);
brainz(formula, calculate, calcCallback);
set
2 Custom If you want to store the cached values in a specific place like localstorage, cookies or give to it
a new layer of customization before properly cache it, you can use the set
param.
function myCustomSet(cachedValues) {
//... do something with the cached values
//... every time that the `cache` changes this method will be executed
}
var brainz = new Brainz(cache, myCustomSet);
brainz(formula, calculate, calcCallback);
License
MIT