# async-data-cache

> Node.js cache for async data

Latest version **1.0.7** (published 2020-07-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-data-cache
pnpm add async-data-cache
yarn add async-data-cache
bun add async-data-cache
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2020-07-19 |
| First published | 2020-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Emilio Spatola |
| Maintainers | emiliosp |
| Keywords | async, cache, performance |

## Links

- npm: https://www.npmjs.com/package/async-data-cache
- Repository: https://github.com/emilioSp/async-data-cache
- Homepage: https://github.com/emilioSp/async-data-cache#readme
- Issues: https://github.com/emilioSp/async-data-cache/issues
- npm.io page: https://npm.io/package/async-data-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.7 (latest) — 2020-07-19
- 1.0.6 — 2020-07-19
- 1.0.5 — 2020-07-19
- 1.0.4 — 2020-07-19
- 1.0.3 — 2020-07-19
- 1.0.2 — 2020-07-19
- 1.0.1 — 2020-07-19
- 1.0.0 — 2020-07-19

## README

# async-data-cache
<img src=https://img.shields.io/npm/l/async-data-cache> <img src=https://img.shields.io/snyk/vulnerabilities/github/emiliosp/async-data-cache> <img src=https://img.shields.io/npm/v/async-data-cache> <img src=https://img.shields.io/badge/keyword-performance-blue> <img src=https://img.shields.io/badge/keyword-cache-blue>

A simple async data cache with __no dependencies__.

This library allows your to cache the result of an async call. 

It's useful when you have to call an api or execute a heavy query and the performance are more important than the real-time.
You can easily choose the cache time to live (ttl).

## Installation
```
yarn add async-data-cache
```

## Usage
```
// Simulate a function that requires 3 secs to complete (e.g. a heavy db query)
const dataFetcher = (): Promise<Array<number>> => {
  console.log('dataFetcher');
  return new Promise((res, rej) => {
    setTimeout(() => res([1, 2, 3, 4, 5]), 3000);
  })
}

(async () => {
  // With the cache the dataFetcher function will be called only 1 time.
  const cache = new DataCache<Array<number>>(dataFetcher);
  let data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
  data = await cache.getData();
  console.log(data);
})();
```

To change the ttl, pass the ttl minutes to the DataCache constructor
```
// 1 hour of ttl
const cache = new DataCache<Array<number>>(dataFetcher, 60);
```

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