global-cache ![Version Badge](https://camo.githubusercontent.com/ef0d82040b041044dcd706d078d16bfdc9883a5c92f3205b987555773b27ce3a/687474703a2f2f76657273696f6e626164672e65732f6c6a686172622f676c6f62616c2d63616368652e737667)
Sometimes you have to do horrible things, like use the global object to share a singleton. Abstract that away, with this!
This attaches a cache to the global object. It attempts to make it as undiscoverable as possible:
- uses Symbols if available
- if not, uses a string key that is not a valid identifier, so as not to show up in dot-notation browser autocomplete
- makes it non-enumerable if property descriptors are supported
Keys are required to be strings or symbols.
Example
var cache = require('global-cache');
var assert = require('assert');
var value = {};
assert(cache.get(key) === undefined);
assert(cache.has(key) === false);
cache.set(key, value);
assert(cache.get(key) === value);
assert(cache.has(key) === true);
cache.delete(key);
assert(cache.get(key) === undefined);
assert(cache.has(key) === false);
Tests
Simply clone the repo, npm install
, and run npm test