# request-easy-cache

> Caching proxy for request.

Latest version **1.3.1** (published 2015-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install request-easy-cache
pnpm add request-easy-cache
yarn add request-easy-cache
bun add request-easy-cache
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2015-12-09 |
| First published | 2015-06-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 11 |
| Maintainers | developit |
| Keywords | request, cache, http |

## Links

- npm: https://www.npmjs.com/package/request-easy-cache
- Repository: https://github.com/developit/request-easy-cache
- Homepage: https://github.com/developit/request-easy-cache#readme
- Issues: https://github.com/developit/request-easy-cache/issues
- npm.io page: https://npm.io/package/request-easy-cache

## Dependencies (3)

- [ms](https://npm.io/package/ms.md) ^0.7.1
- [request](https://npm.io/package/request.md) ^2.57.0
- [lru-cache](https://npm.io/package/lru-cache.md) ^2.6.4

## 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.3.1 (latest) — 2015-12-09
- 1.3.0 — 2015-09-01
- 1.2.0 — 2015-06-10
- 1.1.3 — 2015-06-09
- 1.1.2 — 2015-06-09

## README

[![NPM version][npm-version-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![MIT License][license-image]][license-url] [![Build Status][travis-image]][travis-url]

# request-easy-cache

> A caching wrapper around [request].
>
> `npm install --save request-easy-cache`

`request-easy-cache` wraps calls to `request` in a layer that caches HTTP responses automatically. Multiple identical requests produce a cached `response` and `body`.

Caching is done in-memory using [lru-cache], keyed on `{url,options}`. Calls with identical `url` and `options` return the same `response` and a cloned `body`.


---


## Usage

Provided in ES6 for clarity.

```js
import request from 'request-easy-cache';

function get(callback) {
	request.get('http://pokeapi.co/api/v1/pokemon/1', {
		json: true
	}, (err, res, body) => {
		callback(body);
	});
}

// simple test example
get( body => {

	let start = Date.now();
	get( body2 => {
		let time = Date.now() - start;

		assert.ok(time<10, 'Should be a cache hit (<10ms)');

		assert.deepEqual(body, body2, 'Should return the same data for a cached call');
	});
});
```


> _**Note:** In addition to [request]'s options, `request-easy-cache` adds a `{boolean} cache` option that, when set to `false`, disables caching for the request._


---


## Methods


##### `.get(url, callback)`

> Fetch a URL using the default options.
>
> Callback signature is identical to [request]'s: `(err, res, body)`.

##### `.get(url, options, callback)`

> Fetch a URL with the specified options.
>
> Supports all [request] options.

##### `.get(options, callback)`

> Fetch via the specified options.
>
> One of `options.url` or `options.uri` must be set.
>
> Supports all [request] options.


---


## Configuration

The default export is an instance of `RequestEasyCache` with all default options.

* `max: 100`: Maximum number of responses to keep cached. Least-recently-used responses are dropped in favor of new ones.
* `maxAge: 60000`: Maximum time to keep cached responses, in milliseconds. Defaults to 1 minute.

You can override the default settings:

```js
requestEasyCache.enableCache({
	max: 100,		   // 100 cached responses
	maxAge: 60000	   // 60 seconds, in milliseconds
});
```

It is also possible to create customized instances of `RequestEasyCache`:

```js
import { RequestEasyCache } from 'request-easy-cache';

let cachedRequest = new RequestEasyCache({
	cacheHttpErrors: false,		// don't cache 4xx/5xx errors
	cache: {
		max: 200,		// max number of responses to keep
		maxAge: 5*6e4	// 5 minute cache lifetime
	}
});

cachedRequest.get('...', (err, res, body) => {});
```


## How It Works

Caching uses a compound key `{ url, options }`. Calls with identical `url` and `options` return the same data.


---


## Version History

* `1.3.0` - Passthrough support for non-`GET` requests, added `request(opts)` method.
* `1.2.0` - Support time strings for `cache.maxAge` via [ms](https://www.npmjs.com/package/ms).
* `1.1.1` - Added preferred support for `.get()`. `.cached()` is now an alias.
* `1.1.0` - Updated to be simpler and more similar to the [request] API (retains backwards compatibility).
* `1.0.0` - Forked from [request-again], rewritten in ES6.


[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
[license-url]: https://github.com/developit/request-easy-cache/blob/master/LICENSE

[npm-version-image]: http://img.shields.io/npm/v/request-easy-cache.svg?style=flat-square
[npm-downloads-image]: http://img.shields.io/npm/dm/request-easy-cache.svg?style=flat-square
[npm-url]: https://npmjs.org/package/request-easy-cache

[travis-image]: http://img.shields.io/travis/developit/request-easy-cache.svg?style=flat-square
[travis-url]: http://travis-ci.org/developit/request-easy-cache

[request]: https://www.npmjs.com/package/request
[request-again]: https://github.com/hemphillcc/request-again
[lru-cache]: https://www.npmjs.com/package/lru-cache

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