Global-cache

global-cache

Use global object as a share singleton.

From the 🐴's mouth -> Sometimes you have to do horrible things, like use the global object to share a singleton. Abstract that away, with this!

global-cache 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.

Get it: npm install global-cache

Sample usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let value = {}, key='key';
const cache = require('global-cache');

[cache.get(key), cache.has(key)]; //[ undefined, false ]

cache.set(key,value); //true

[cache.get(key), cache.has(key)]; //[ {}, true ]

cache.delete(key); // true

[cache.get(key), cache.has(key)]; // [ undefined, false ]

cache
/*
{ clear: [Function: clear],
  delete: [Function: deleteKey],
  get: [Function: get],
  has: [Function: has],
  set: [Function: set],
  setIfMissingThenGet: [Function: setIfMissingThenGet] }
*/

GIT FTW!

global-cache.gif

Suggest a module

Comments