{"id":45025,"library":"global-cache","title":"global-cache","description":"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.","status":"active","version":"1.2.1","language":"javascript","source_language":"en","source_url":"git://github.com/ljharb/global-cache","tags":["javascript","global","window","self","cache","global object"],"install":[{"cmd":"npm install global-cache","lang":"bash","label":"npm"},{"cmd":"yarn add global-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add global-cache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package exports a single cache object. CommonJS require works, but ESM import is preferred for modern code.","wrong":"const cache = require('global-cache')","symbol":"default","correct":"import cache from 'global-cache'"},{"note":"Keys must be strings or symbols. Store any value.","symbol":"set","correct":"cache.set(key, value)"},{"symbol":"get","correct":"const val = cache.get(key)"},{"symbol":"has","correct":"if (cache.has(key))"},{"symbol":"delete","correct":"cache.delete(key)"}],"quickstart":{"code":"import cache from 'global-cache';\nimport assert from 'assert';\nconst key = 'myKey';\nconst value = { data: 123 };\nassert.equal(cache.has(key), false);\ncache.set(key, value);\nassert.equal(cache.get(key), value);\ncache.delete(key);\nassert.equal(cache.has(key), false);","lang":"javascript","description":"Shows basic usage of set, get, has, and delete on the global cache."},"warnings":[{"fix":"Ensure keys are strings or Symbols.","message":"Keys must be strings or symbols; passing objects or other types may cause errors or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use cache.delete() to clean up when needed, or rely on process restarts to reset state.","message":"The cache persists across module reloads in Node.js because it's attached to the global object, which is not cleared between runs.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Do not store sensitive data; treat as a shared cache only.","message":"In browsers, the cache is stored on window, making it susceptible to cross-origin attacks if shared origins exist.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use Symbol-polyfilled environments for full privacy.","message":"If 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Persist important data to localStorage or a file.","message":"The cache is not serializable; data is lost on page refresh or Node process restart.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'across':67 'attach':10 'avail':23 'cach':3,12,73 'cjs':59 'code':48 'collid':45 'depend':56 'descriptor':36 'discover':43 'discreet':21 'enumer':34 'environ':68 'esm':57 'global':2,15,39,70,74 'global-cach':1 'ideal':61 'identifi':28 'intern':63 'javascript':69 'key':30,49 'method':22 'minim':42 'modul':64 'must':50 'non':27,33 'non-enumer':32 'non-identifi':26 'object':16,75 'properti':35 'self':72 'share':66 'singleton':40 'stabl':8 'state':65 'string':29,52 'support':60 'symbol':24,54 'tini':5 'typic':38 'unlik':37 'use':18 'util':6 'v1.2.1':7 'window':71 'window/global/self':17","created_at":"2026-06-07T12:53:00.495906+00:00","updated_at":"2026-06-07T12:53:00.495906+00:00","problems":[{"fix":"Ensure you do 'import cache from 'global-cache'' or 'const cache = require('global-cache');'","cause":"Incorrect import: imported a different module or forgot to load the library.","error":"TypeError: cache.set is not a function"},{"fix":"Verify key is a string/symbol and that cache.set was called before cache.get.","cause":"Using a different reference for the same key across modules, or the key was deleted inadvertently.","error":"AssertionError: Expected values to be strictly equal"},{"fix":"Use cache.set(String(key), value) or Symbol(key) as appropriate.","cause":"Passed a non-string/non-symbol key (e.g., object or number).","error":"Error: key must be a string or symbol"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/ljharb/global-cache#readme","github":"git://github.com/ljharb/global-cache","docs":null,"changelog":null,"pypi":null,"npm":"global-cache","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}