# @endo/cache-map

> bounded-size caches having WeakMap-compatible methods

Latest version **1.1.0** (published 2025-07-12) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @endo/cache-map
pnpm add @endo/cache-map
yarn add @endo/cache-map
bun add @endo/cache-map
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-07-12 |
| First published | 2025-07-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1053 |
| Author | Endo contributors |
| Maintainers | kriskowal, michaelfig, erights, warner, mhofman |
| Keywords | cache, cache-map, cachemap, weakmap |

## Links

- npm: https://www.npmjs.com/package/@endo/cache-map
- Repository: https://github.com/endojs/endo
- Homepage: https://github.com/endojs/endo/tree/master/packages/cache-map#readme
- Issues: https://github.com/endojs/endo/issues
- npm.io page: https://npm.io/package/@endo/cache-map

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2025-07-12

## README

# cache-map

This `@endo/cache-map` package creates bounded-size caches having
WeakMap-compatible `has`/`get`/`set`/`delete` methods.
Key validity, comparison, and referential strength are controlled by a `makeMap`
option, which defaults to `WeakMap` but can be set to any producer of objects
with those methods (e.g., using `Map` allows for arbitrary keys which will be
strongly held).
Cache eviction policy is not currently configurable, but strives for a hit ratio
at least as good as
[LRU](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU) (e.g., it
might be [CLOCK](https://en.wikipedia.org/wiki/Page_replacement_algorithm#Clock)
or [SIEVE](https://sievecache.com/)).

## Usage

### Weak Cache
```js
import { makeCacheMapKit } from '@endo/cache-map';

const { cache: weakCache, getMetrics } = makeCacheMapKit(2);
const entries = [
  { key: Symbol('key 1'), value: Symbol('value 1') },
  { key: Symbol('key 2'), value: Symbol('value 2') },
  { key: Symbol('key 3'), value: Symbol('value 3') },
];
for (const { key, value } of entries) weakCache.set(key, value);

assert(!weakCache.has(entries[0].key));
assert(weakCache.has(entries[1].key));
assert(weakCache.get(entries[2].key) === entries[2].value);

weakCache.delete(entries[2].key);
weakCache.set(entries[1].key, entries[0]);

assert(!weakCache.has(entries[0].key));
assert(!weakCache.has(entries[2].key));
assert(weakCache.get(entries[1].key) === entries[0]);

assert.throws(() => weakCache.set('unweakable key', {}));
```

### Strong Cache
```js
import { makeCacheMapKit } from '@endo/cache-map';

const { cache, getMetrics } = makeCacheMapKit(100, { makeMap: Map });
cache.set('unweakable key', 'ok');
assert(cache.get('unweakable key') === 'ok');
```

## License

[Apache License, Version 2.0](./LICENSE)

---
_Source: https://npm.io/package/@endo/cache-map · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
