# redis-controller-adapter

> plug and play redis adapter for controllers

Latest version **1.1.5** (published 2019-11-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install redis-controller-adapter
pnpm add redis-controller-adapter
yarn add redis-controller-adapter
bun add redis-controller-adapter
```

## 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.1.5 |
| Published | 2019-11-09 |
| First published | 2019-09-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | vinoth |
| Maintainers | v3n0me |
| Keywords | redis, redis adapter, redis plug, redis controller |

## Links

- npm: https://www.npmjs.com/package/redis-controller-adapter
- Repository: https://github.com/V3N0ME/redis-controller-adapter
- Issues: https://github.com/V3N0ME/redis-controller-adapter/issues
- npm.io page: https://npm.io/package/redis-controller-adapter

## Dependencies (2)

- [redis](https://npm.io/package/redis.md) ^2.8.0
- [string-template](https://npm.io/package/string-template.md) ^1.0.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

- 1.1.5 (latest) — 2019-11-09
- 1.1.4 — 2019-11-09
- 1.1.3 — 2019-11-09
- 1.1.2 — 2019-09-26
- 1.1.1 — 2019-09-26
- 1.0.1 — 2019-09-25
- 1.0.0 — 2019-09-25

## README

A plug and play redis adapter for controllers. Creates cache keys and verifys them dynamically. All you have to do is configure the format of the cache key for each function

## Installation

```bash
$ npm install redis-controller-adapter
```

## Usage Example

### Initialisation

```js
const redis = require("redis").createClient();
const redisAdapter = require("redis-controller-adapter");
//initialise adapter with the redis client
redisAdapter.init(redis);
//set default expiry date for all keys
//can be overridden by providing induvidual expire time for each cache key in config
redisAdapter.setDefaultExpiry(60);
```

### Usage

```js
//controller/example.js
const redisAdapter = require("redis-controller-adapter");

class Example {
  constructor() {}

  getAll() {
    return new Promise((resolve, reject) => {
      resolve(["a", "b", "c"]);
    });
  }

  get(id) {
    return new Promise((resolve, reject) => {
      resolve({ url: id });
    });
  }
}

module.exports = redisAdapter.use(new Example(), {
  get: { cacheKey: "example:{0}" },
  getAll: { cacheKey: "examples", expiry: 20 }
});
```
```js
//index.js
const controller = require('./controller/example');
const data = await controller.get('test');

//data.isCached() returns true if the data was retrieved from redis and false if retrieved from the controller
console.log(data, data.isCached());
```

### Configuration

The name of the controller's method which requires caching will be the key and an object containing the format of the cache key and it's expiry (optional) will be the value.

```js
const config = {
  get: { cacheKey: "example:{0}" },
  getAll: { cacheKey: "examples", expiry: 20 }
};
```

#### Example

>**Key** - example:{0}:{1}    
>**Function** - get(id, value)                
>calling **controller.get(1, 2)** will produce **example:1:2**

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