# @sthom/promise-cache

> A cache for Promises

Latest version **1.0.0** (published 2018-12-17) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @sthom/promise-cache
pnpm add @sthom/promise-cache
yarn add @sthom/promise-cache
bun add @sthom/promise-cache
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-12-17 |
| First published | 2018-12-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stuart Thomson |
| Maintainers | sthom |
| Keywords | promise, cache |

## Links

- npm: https://www.npmjs.com/package/@sthom/promise-cache
- Repository: https://github.com/s-thom/promise-cache
- Homepage: https://github.com/s-thom/promise-cache#readme
- Issues: https://github.com/s-thom/promise-cache/issues
- npm.io page: https://npm.io/package/@sthom/promise-cache

## 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.0.0 (latest) — 2018-12-17

## README

# [@sthom/promise-cache](https://www.npmjs.com/package/@sthom/promise-cache)

A simple cache for Promises with a Map-like interface

## Usage

```sh
npm install --save @sthom/promise-cache
```

```js
import PromiseCache from '@sthom/promise-cache';

const myCache = new PromiseCache((key) => fetch(`https://example.com/${key}`));

// ...

// No entry in the cache, so a new one is created (inluding a new Reqeust)
const promiseA = myCache.get('foo');

// Reuses the existing entry, so no new request is made
const promiseB = myCache.get('foo');
console.log(promise1 === promise2); // true

// ...

// Delete a single entry
myCache.delete('foo');

// Clear all values
myCache.clear();
```

### Why is there no `cache.set()`?

It doesn't make sense for this library. The function used to create the Promises is part of the PromiseCache. If you need to put something into the cache, you just need to `.get()` it, the two operations end up being the same.

## Differences to some other Promise cache packages

You know, I really should have looked around before deciding to write this. However, I don't think I would've done much differently. Most of the libraries I found were actual full-on caches, while this is more a way to store and re-use results. I know the two sound the same, but there are differences to the uses.

### [node-cache-promise](https://www.npmjs.com/package/node-cache-promise)

A Promise wrapper around [node-cache](https://www.npmjs.com/package/node-cache), which is meant to be used as a generic cache for anything, and has a completely different use-case this this library.

It's also beholden to node-cache's limitations, such as keys only being strings (and technically numbers, which are then asserted to strings).

### [promise-caching](https://www.npmjs.com/package/promise-caching)

This one is pretty similar, but with two differences.

1. promise-caching supports a timeout, which this library (currently) does not.
2. In promise-caching you pass a function to each call of `cache.get()`. In this one, you pass it to the constructor, where it is re-used when necessary.

I think #2 is the major difference here. This library has you specify the function at the beginning, then re-uses it. This means that if you're using the cache in multiple places in your code, then you don't need to also pass around your Promise-returning function with it. However, this leads to lower flexibility, as you can't change the function for a single item, instead creating a new cache using that new function.

In the end, use whatever's better for your use-case. That's why having different packages is helpful.

### [request-promise-cache](https://www.npmjs.com/package/request-promise-cache)

request-promise-cache is a wrapper around [request](https://www.npmjs.com/package/request), and is only for requests. This library allows you to use other Promises, not just network-related ones.

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