# @prequest/cache

> 缓存器。

Latest version **1.5.2** (published 2022-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @prequest/cache
pnpm add @prequest/cache
yarn add @prequest/cache
bun add @prequest/cache
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.2 |
| Published | 2022-07-10 |
| First published | 2021-05-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 2 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 89 |
| Author | xdoer |
| Maintainers | thomas_han |
| Keywords | http, request, XMLHttpRequest, fetch, https, middleware, cache |

## Links

- npm: https://www.npmjs.com/package/@prequest/cache
- Repository: https://github.com/xdoer/PreQuest
- Homepage: https://pre-quest.vercel.app
- Issues: https://github.com/xdoer/PreQuest/issues
- npm.io page: https://npm.io/package/@prequest/cache

## Dependencies (2)

- [@xdoer/x](https://npm.io/package/@xdoer/x.md) ^1.0.1
- [@prequest/types](https://npm.io/package/@prequest/types.md) ^1.5.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.5.2 (latest) — 2022-07-10
- 1.5.1 — 2022-07-03
- 1.5.0 — 2022-06-18
- 1.4.1 — 2022-05-02
- 1.4.0 — 2022-05-02
- 1.3.10 — 2022-04-18
- 1.3.9 — 2022-04-18
- 1.3.8 — 2022-04-17
- 1.3.7 — 2022-04-17
- 1.3.6 — 2022-04-03
- 1.3.5 — 2022-03-09
- 1.3.3 — 2022-02-27
- 1.3.3-alpha.0 — 2022-02-26
- 1.3.2 — 2022-02-09
- 1.2.6 — 2021-12-18
- … 27 more at https://npm.io/package/@prequest/cache/versions

## README

# @prequest/cache

缓存器。

## 安装

```bash
npm install @prequest/cache
```

## 使用

下面的示例表示在一分钟内，发起的相同的路径的 GET 请求，都只会调用一次接口，响应相同的数据。

```ts
import { create } from '@prequest/xhr'
import createCacheAdapter from '@prequest/cache'

const cache = createCacheAdapter({
  ttl: 60000,
  getCacheKey: opt => opt.path,
  verifyRequest: opt => !opt.method || opt.method === 'GET',
  verifyResponse: res => {
    if (res.status === 200) return res.data
    throw 'http error'
  },
})

const prequest = create({ useCache: true }, cache)
```

## 配置项

| Option Name    | Type                  | Default                                      | Required | Meaning                      |
| -------------- | --------------------- | -------------------------------------------- | -------- | ---------------------------- |
| ttl            | number                | 60000                                        | false    | 默认一分钟缓存失效           |
| getCacheKey    | (opt: any) => string  | (opt) => opt.path                            | false    | 默认以请求路径作为缓存的 key |
| verifyRequest  | (opt: any) => boolean | opt => !opt.method \|\| opt.method === 'GET' | false    | 默认 GET 请求才会缓存        |
| verifyResponse | (res: any) => any     | res => res                                   | false    | 默认 GET 请求才会缓存        |

同时，发起请求时，设置 `useCache` 为 `true`，且 `verifyRequest` 校验通过，则会缓存请求数据。

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