MutaCache
A cache for mutable javascript data structures.
Do you want to cache an object and then mutate it without affecting the cached representation? Yes? Then this is the cache for you.
The Problem
Should this happen?
NODE> cache a: 'a' b: 'b' c: 'c' NODE> cache a: 'a' b: 'b' c: 'c' NODE> var theValueNow = cacheNODE> delete theValueNowatrueNODE> delete theValueNowbtrueNODE> delete theValueNowctrueNODE> theValueNowlol = truetrueNODE> cache lol: true
I don't think so. This cache won't do that.
Install
npm install mutacache --save
Usage
The simplest case, storing a primitive:
var cache = ; cache;if cache cache; // returns 'value'
Storing an object:
var cache = ; cache;if cache var x = cache; //returns {a:'a', b:'b', c:'c'} xa = 'lol'; cache; // still returns {a:'a', b:'b', c:'c'}
Options
The mutacache function takes an object of options,
cacheFunctions
A boolean, defines whether functions should be cached. Defaultfalse
.
Caching Functions
Caching functions is useful, but it's use invokes an alternate copying mechanism that has performance penalties.
This is probably applicable for more than Function types.
var nonFnCacher = ;var fnCacher = cacheFunctions:true; { // something important here} nonFnCacher;if nonFnCacher var x = nonFnCacher; //returns {a:'a'} fnCacher;if fnCacher var x = fnCacher; //returns {a:'a', fn:abc}
Limitations
This cache copies values on insertion and retrieval, and Javascript data structures aren't really built for that. Don't use this if you're looking for raw speed.
And, some TODOs:
- Add cache busting
- TTLs
License
Copyright © 2014 Dan Midwood
Distributed under the Apache License, Version 2.0