Registry / storage / global-cache

global-cache

JSON →
library1.2.1jsnpmunverified

A tiny utility (v1.2.1, stable) that attaches a cache to the global object (window/global/self) using the most discreet method available: Symbols, a non-identifier string key, and non-enumerable property descriptors. Unlike typical global singletons, it minimizes discoverability and collides with other code. Keys must be strings or symbols. No dependencies, ESM and CJS support. Ideal for internal module state sharing across environments.

npm install global-cache
INSTALL
IMPORT
SIG · GLOBAL-CACHE
G
global-cache
storagejavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ import cache from 'global-cache'
✗ const cache = require('global-cache')
The package exports a single cache object. CommonJS require works, but ESM import is preferred for modern code.
set
✓ cache.set(key, value)
Keys must be strings or symbols. Store any value.
get
✓ const val = cache.get(key)
has
✓ if (cache.has(key))
delete
✓ cache.delete(key)

Shows basic usage of set, get, has, and delete on the global cache.

import cache from 'global-cache'; import assert from 'assert'; const key = 'myKey'; const value = { data: 123 }; assert.equal(cache.has(key), false); cache.set(key, value); assert.equal(cache.get(key), value); cache.delete(key); assert.equal(cache.has(key), false);
Debug
Known issues
gotchaKeys must be strings or symbols; passing objects or other types may cause errors or unexpected behavior.
fix
Ensure keys are strings or Symbols.
affects: >=1.0.0
gotchaThe cache persists across module reloads in Node.js because it's attached to the global object, which is not cleared between runs.
fix
Use cache.delete() to clean up when needed, or rely on process restarts to reset state.
affects: >=1.0.0
gotchaIn browsers, the cache is stored on window, making it susceptible to cross-origin attacks if shared origins exist.
fix
Do not store sensitive data; treat as a shared cache only.
affects: >=1.0.0
gotchaIf native Symbols are not supported (e.g., old browsers), the library uses a non-identifier string key, which may be visible via Object.getOwnPropertyNames() or for...in loops.
fix
Use Symbol-polyfilled environments for full privacy.
affects: >=1.0.0
gotchaThe cache is not serializable; data is lost on page refresh or Node process restart.
fix
Persist important data to localStorage or a file.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cache.set is not a function
Incorrect import: imported a different module or forgot to load the library.
fix
Ensure you do 'import cache from 'global-cache'' or 'const cache = require('global-cache');'
AssertionError: Expected values to be strictly equal
Using a different reference for the same key across modules, or the key was deleted inadvertently.
fix
Verify key is a string/symbol and that cache.set was called before cache.get.
Error: key must be a string or symbol
Passed a non-string/non-symbol key (e.g., object or number).
fix
Use cache.set(String(key), value) or Symbol(key) as appropriate.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
Amazon
1
Resources
global-cache — npm install global-cache · libregistry