in-memory cache made easy

Usage no npm install needed!

<script type="module">
  import brainz from '';



In-memory cache made easy.


You can manually download it from dist folder or get it through npm / bower.


npm install --save brainz


bower install --save brainz


new Brainz(obj, set, limit, time)

Brainz constructor.


  • obj - Object that contains the cached items.
  • set(obj) - Function that will be executed after the request resolve. The first argument is the updated obj. not required
  • limit - Maximum number of items that will be cached in obj. not required , default: 10000
  • time - Time in milliseconds that items will be considerated valid in obj. not required , default: 36000000

brainz(key, request, [callback])

Caching requests.


  • key - key is a string used to identify stored values in cache.
  • request(callback) - Function that will be executed case key doesn't exists or expired.
  • callback(err, result) - Callback that will be executed after request resolve or found value from obj.


The following identifiers will be used for all examples:

var formula = "..something..";
var cache =  //.. object that contains the cached values
function calculate(callback) {
// 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);

2 Custom set

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