Jerome Etienne.js Blog

Around JavaScript, node.js, WebGL and cool HTML5 game stuff

MicroCache.js - Cache Micro Library

microcache.js is a micro library to handle in-memory cache. It is less than 20 lines and works in node and browser. It is available on github here under MIT license. If you hit bugs, fill issues on github. Feel free to fork, modify and have fun with it :)

Install it

To install it on node

1
npm install microcache

To install the browser version, download it and include it like that

1
<script src="microcache.js"></script>

API

To instanciate a cache, do the following. You can have as many instances you want.

1
var microcache = new MicroCache();

.get(key)

To get a cached asset which is in the cache. Each cached element has a unique key to identify it.

1
microcache.get('foo');

.set(key, value)

To set a element value in the cache.

1
microcache.set('foo', 'bar');

.contains(key)

To know if a given asset is currently in the cache.

1
microcache.contains('foo');

.values()

To return all the elements currently in the cache

1
microcache.values();

.getSet(key, value)

To get an element from the cache, if it isnt already present, store it then return it

1
microcache.getSet(key, value);

FAQ

  • Q. what about plain var microcache = {} ? isnt this wrapper overengineering ?
  • A. i was thinking so too at first. but the syntax of a .getSet() without wrapper was too ugly for my taste :) var a = cache[key] = (cache[key] || value) from @floriancargoet