# request-cache

> Simple request caching using Redis for request.

Latest version **0.1.3** (published 2017-05-23) · Apache 2.0 license · 0 weekly downloads

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

## Install

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

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2017-05-23 |
| First published | 2014-08-13 |
| Weekly downloads | 0 |
| License | Apache 2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jason Cust |
| Maintainers | jasoncust |
| Keywords | request, redis, node_redis, ioredis, cache |

## Links

- npm: https://www.npmjs.com/package/request-cache
- Repository: https://github.com/CentralPing/request-cache
- Issues: https://github.com/CentralPing/request-cache/issues
- npm.io page: https://npm.io/package/request-cache

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [request](https://npm.io/package/request.md) ^2.81.0

## 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

- 0.1.3 (latest) — 2017-05-23
- 0.0.0-pre1 — 2014-08-13

## README

request-cache
====================

[![Build Status](https://travis-ci.org/CentralPing/request-cache.svg?branch=master)](https://travis-ci.org/CentralPing/request-cache)
[![Code Climate for CentralPing/request-cache](https://codeclimate.com/github/CentralPing/request-cache/badges/gpa.svg)](https://codeclimate.com/github/CentralPing/request-cache)
[![Dependency Status for CentralPing/request-cache](https://david-dm.org/CentralPing/request-cache.svg)](https://david-dm.org/CentralPing/request-cache)
[![Greenkeeper badge](https://badges.greenkeeper.io/CentralPing/request-cache.svg)](https://greenkeeper.io/)

Simple request caching using [Redis](http://redis.io/) for [request](https://github.com/request/request).

Tested with redis clients: [node_redis](https://github.com/mranney/node_redis) [ioredis](https://github.com/luin/ioredis)

## Installation

`npm i --save request-cache`

## API Reference
**Example**  
```js
const requestCache = require('request-cache');
const rCache = requestCache(REDIS_CLIENT);
const cacheKey;

setInterval(function () {
  const key = rCache('http://www.somethingawesome.com', function (err, resp, body) {
    // Do something with the awesomeness
  });

  // First call - cacheKey is undefined; resp and body are cached and returned
  // Second call - cacheKey === key; cached resp and body are returned
  // Third call - cacheKey === key; cached resp and body are returned
  // ...
  // Three-thousand-six-hundredth call - cacheKey === key; new resp and body are cached and returned

  cacheKey = key;
}, 1000);
```
<a name="exp_module_request-cache--module.exports"></a>

### module.exports(redisClient, [options]) ⇒ <code>function</code> ⏏
**Kind**: Exported function  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| redisClient | <code>object</code> |  | an instance of a [redis client](https://github.com/mranney/node_redis) |
| [options] | <code>object</code> |  | Options for cache management |
| [options.algorithm] | <code>string</code> | <code>&quot;md5&quot;</code> | Any available system hashing algorithm to generate cache key ([more info](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm)) |
| [options.encoding] | <code>string</code> | <code>&quot;hex&quot;</code> | Encoding algorithm to use for encoding hashed cache key |
| [options.ttl] | <code>number</code> | <code>3600</code> | Time in seconds for cache time-to-live |
| [options.refresh] | <code>number</code> | <code>0</code> | Time in seconds to refresh time-to-live (this does not initiate a new request) |
| [options.queryCacheKeys] | <code>array</code> |  | Query param keys to use for generating cache key |
| [options.keyPrefix] | <code>string</code> |  | Prefix for generated cache keys |


## Examples

### With Basic Requests
```js
const requestCache = require('request-cache');
const rCache = requestCache(REDIS_CLIENT);

const cacheKey = rCache('http://www.somethingawesome.com', function (err, resp, body) {
  // Do something with the awesomeness
});
```

### With Request Object
```js
const requestCache = require('request-cache');
const rCache = requestCache(REDIS_CLIENT);

const cacheKey = rCache({
  url:'http://www.somethingawesome.com',
}, function (err, resp, body) {
  // Do something with the awesomeness
});
```

### With Cache Key Options
```js
const requestCache = require('request-cache');
const rCache = requestCache(REDIS_CLIENT, {
  queryCacheKeys: ['foo']
});

const cacheKey = rCache({
  url:'http://www.somethingawesome.com',
  qs: {
    foo: 'bar', // value used in cache key generation
    far: 'boo'
  }
}, function (err, resp, body) {
  // Do something with the awesomeness
});
```

# License

Apache 2.0

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