# redis-json-memoize

> Caches the returned value of promises in the provided redis cache

Latest version **1.0.1** (published 2018-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install redis-json-memoize
pnpm add redis-json-memoize
yarn add redis-json-memoize
bun add redis-json-memoize
```

## 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.0.1 |
| Published | 2018-11-02 |
| First published | 2018-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tom von Geldern |
| Maintainers | tvongeldern |
| Keywords | node, redis, cache, memoize, promise, speed, performance |

## Links

- npm: https://www.npmjs.com/package/redis-json-memoize
- Repository: https://github.com/tvongeldern/redis-json-memoize
- Homepage: https://github.com/tvongeldern/redis-json-memoize#readme
- Issues: https://github.com/tvongeldern/redis-json-memoize/issues
- npm.io page: https://npm.io/package/redis-json-memoize

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [json-stable-stringify](https://npm.io/package/json-stable-stringify.md) ^1.0.1

## 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.1 (latest) — 2018-11-02
- 1.0.0 — 2018-11-02

## README

# redis-json-memoize

Caches the JSON response of a provided promise into the provided Redis. Ideal for caching responses from database calls in your API.

If you are looking to reap the benefits of Redis caching in your API, this library provides a simple and easy-to-use solution.

Compatible with both [ioredis](https://www.npmjs.com/package/ioredis) and [node redis](https://www.npmjs.com/package/redis).

For more info on Redis: https://redis.io/topics/introduction

## Install

`npm install --save redis-json-memoize`

## Usage Examples

### Providing Redis instance directly

```
const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

module.exports = redisMemoize.memoize(getUserByName, { redis: redisInstance });
```

### Initializing module with Redis instance

```
const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

redisMemoize.initialize({
	redis: redisInstance,
});

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

module.exports = redisMemoize.memoize(getUserByName);
```

### Clearing cached responses for a specific function

```
const Redis = require('ioredis');
const redisMemoize = require('redis-json-memoize');
const config = require('../../config');
const UserModel = require('../models/user'); // Could be from Mongoose, Sequelize etc

const redisInstance = new Redis(config.redisUrl);

redisMemoize.initialize({
	redis: redisInstance,
});

function getUserByName(userName) {
	return UserModel.find({ name: userName });
}

const cachedGetUserByName = redisMemoize.memoize(getUserByName);

module.exports = {
	getUser: cachedGetUserByName,
	clearCachedUsers: cachedGetUserByName.clearCache, // Will clear cached responses for this function only. Can accept a locator string.
}
```

## API

### memoize (fn : Function, options: Object)
Returns a memoized version of the provided function.

- **fn** Provided function
	- Must return a promise
	- Should be named as specifically as possible (`fn.name` will be used to write the redis key for the cached response)
	- NOTE: it is not recommended that you use this module to memoize Express/Hapi request handlers. Redis keys are written using a combination of `fn.name` and the function inputs stringified. HTTP requests are large circular objects, so the memoization will not be effective
	- **fn.clearCache** `memoizedFunction.clearCache` will clear Redis keys that were written `memoizedFunction` only. Similar to global `clearCache` method, can accept a locator string.

- **options**
	- **redis** This is where the Redis instance is provided. Compatible with both ioredis and node-redis.
	- **ttl** Sets the TTL (time to loss) for responses memoized from this function. NOTE: Redis TTL's are in seconds, not milliseconds.

### initialize (options: Object)
Initializes implementation of redis-json-memoize.
- **redis** Object. Required. A redis instance.
- **ttl** Number. TTL for cached data in your Redis. If not provided, a default is used.
- **disableLogs** Boolean. Defaults to false. If true, all logs from redis-json-memoize will be muted.
- **prefetchRatio** Number. Defaults to 0.7. Determines the time, as a ratio of TTL, at which cached data is marked as "stale". When Redis returns a "stale" object, it triggers a background refresh of that data.
- **startupTime** Number. Default 5 seconds. Determines how much time is allowed at startup for Redis to boot before the module begins memoizing provided functions.
- **promiseLibrary** Object. Default is native Promise. This gives you an option to provide Bluebird or another promise library if you feel the desire to do so.

### clearCache (locator: String, redis: Object)
Clears cached responses from Redis.
- **locator** String. Not required. If provided, only Redis keys that contain this string will be cleared
- **redis** Object. Not Required if initialize has been used. A redis instance.


## TODOS
 - Test coverage
 - Improve documentation surrounding clearing of cache

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