https://github.com/ljharb/global-cache
Sometimes you have to do horrible things, like use the global object to share a singleton. Abstract that away, with this!
https://github.com/ljharb/global-cache
Last synced: 2 months ago
JSON representation
Sometimes you have to do horrible things, like use the global object to share a singleton. Abstract that away, with this!
- Host: GitHub
- URL: https://github.com/ljharb/global-cache
- Owner: ljharb
- License: mit
- Created: 2015-07-02T06:00:18.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-08-14T17:15:55.000Z (about 2 years ago)
- Last Synced: 2025-07-23T14:52:02.548Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 102 KB
- Stars: 20
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# global-cache [![Version Badge][npm-version-svg]][package-url]
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url][![npm badge][npm-badge-png]][package-url]
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 supportedKeys are required to be strings or symbols.
## Example
```js
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`[package-url]: https://npmjs.org/package/global-cache
[npm-version-svg]: https://versionbadg.es/ljharb/global-cache.svg
[deps-svg]: https://david-dm.org/ljharb/global-cache.svg
[deps-url]: https://david-dm.org/ljharb/global-cache
[dev-deps-svg]: https://david-dm.org/ljharb/global-cache/dev-status.svg
[dev-deps-url]: https://david-dm.org/ljharb/global-cache#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/global-cache.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/global-cache.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/global-cache.svg
[downloads-url]: https://npm-stat.com/charts.html?package=global-cache
[codecov-image]: https://codecov.io/gh/ljharb/global-cache/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/ljharb/global-cache/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/global-cache
[actions-url]: https://github.com/ljharb/global-cache/actions