memory-cache2 v3.0.0
Memory Cache
Instantiates a mutable object cache in memory. Uses Map and Symbol primitives, so it will only work on modern browsers (versions >= 2015), and node 4+. See benchmarks here.
Note about previous versions:
If you're using previous versions, You can find v1 documentation here and v2 documentation here and you can install using memory-cache2@1 and memory-cache2@2. I have dropped support for functionality like getIn, setIn, concatIn, mergeIn. Instead, I opted refactoring to the more performant JS maps, only allowing merge and concat, and adding other features like each, has, and size. I also refactored the event emitter to only allow observing and not triggering. Triggering should only occur internally, as the cache does things.
API
Installation
npm install --save memory-cache2Usage
Instantiate
You can either pass an existing object and convert it into a cache, or you can start a brand new cache.
const MemoryCache = require('memory-cache');
// Empty cache
const cache = new MemoryCache;
// or
// Pass obj as cache
const cache = new MemoryCache( myObj || returnObj() );Set
Defines key value. Returns value.
cache.set('testing', { set: true, arr: [1,2,3] });Get
Returns key value.
const val = cache.get('testing');
// > { set: true, arr: [1,2,3] }
const entireCache = cache.get(true);
// > { testing: { set: true, arr: [1,2,3] }}Remove
Remove key from cache.
cache.remove('testing');
// > trueReset
Resets cache object to empty object.
cache.reset();
// > {}Expire
Expire cache after a certain amount of time in milliseconds.
cache.expire('testing', 3000);
// Expires in 3 secondsSet and Expire
Set the cache and expire after a certain amount of time in miliseconds.
cache.set('testing', { value: 1 }, 3000);
// > { value: 1 }
// Expires in 3 secondsMerge
Merge objects stored in cache. Works with arrays or objects only.
const val = cache.merge('testing', { merged: true });
// > { set: true, arr: [1,2,3], merged: true }Concat
Concatenates cached array.
cache.set('array', ['a', 'b', 'c']);
cache.concat('array', [1,2,3]);
// > ['a', 'b', 'c', 1, 2, 3];Has
Checks to see if cache has a key
cache.has('array')
// > true
cache.has('what')
// > falseEach
Iterates through cache.
cache.each(function (value, key) {
console.log(value, key);
});Keys
Gets all cached keys
cache.keys;
// > ['testing', 'array']Size
Get size of cache
cache.size;
// > 2Debug
Turns logging off or on.
// Instantiate cache with debugging
const cache = new MemoryCache({}, {
debug: true
});
// Set debugging after instantiation
cache.debug = true || false;
cache.set('testing', { set: true });
// MemoryCache: set testing { set: true }
cache.get('testing');
// MemoryCache: get testingEvents
MemoryCache events can be observed. All events return an object with at least key and value, except for reset. You can bind or unbind to any event using the following API:
cache.onGet((obj) => {}): Binds a function togetevent.cache.oneGet((obj) => {}): Binds a function 1 time togetevent.cache.offGet(/* instantiated fn */): Remove a function fromgetevent.cache.onSet((obj) => {}): Binds a function tosetevent.cache.oneSet((obj) => {}): Binds a function 1 time tosetevent.cache.offSet(/* instantiated fn */): Remove a function fromsetevent.cache.onRemove((obj) => {}): Binds a function toremoveevent.cache.oneRemove((obj) => {}): Binds a function 1 time toremoveevent.cache.offRemove(/* instantiated fn */): Remove a function fromremoveevent.cache.onExpire((obj) => {}): Binds a function toexpireevent.cache.oneExpire((obj) => {}): Binds a function 1 time toexpireevent.cache.offExpire(/* instantiated fn */): Remove a function fromexpireevent.cache.onMerge((obj) => {}): Binds a function tomergeevent.cache.oneMerge((obj) => {}): Binds a function 1 time tomergeevent.cache.offMerge(/* instantiated fn */): Remove a function frommergeevent.cache.onConcat((obj) => {}): Binds a function toconcatevent.cache.oneConcat((obj) => {}): Binds a function 1 time toconcatevent.cache.offConcat(/* instantiated fn */): Remove a function fromconcatevent.cache.onReset((obj) => {}): Binds a function toresetevent.cache.oneReset((obj) => {}): Binds a function 1 time toresetevent.cache.offReset(/* instantiated fn */): Remove a function fromresetevent.
Example:
// Instantiate cache with event binding
const cache = new MemoryCache({}, {
events: true
});
// Set event binding after instantiation
cache.events = true || false;
// Happens on every get
cache.onGet(function(opts) {
fs.writeFileSync(`${opts.name}.json`, JSON.strigify(opts.value));
});
// Only happens one time
cache.oneReset(function() {
console.warn('Cache was reset!!!');
});Contributing
- Include 100% test coverage.
- Must support older browsers.
- Submit an issue first for significant changes.