# transparent-redis-layer

> ## Installation

Latest version **1.0.3** (published 2019-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install transparent-redis-layer
pnpm add transparent-redis-layer
yarn add transparent-redis-layer
bun add transparent-redis-layer
```

## 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.3 |
| Published | 2019-11-15 |
| First published | 2019-09-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | cool-firer |
| Maintainers | cool-firer |
| Keywords | redis, transparent |

## Links

- npm: https://www.npmjs.com/package/transparent-redis-layer
- Repository: https://github.com/cool-firer/transparent-redis-layer
- Homepage: https://github.com/cool-firer/transparent-redis-layer#readme
- Issues: https://github.com/cool-firer/transparent-redis-layer/issues
- npm.io page: https://npm.io/package/transparent-redis-layer

## 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.3 (latest) — 2019-11-15
- 1.0.2 — 2019-09-05
- 1.0.1 — 2019-09-05
- 1.0.0 — 2019-09-05

## README

# transparent-redis-layer

## Installation

```js
$ npm install transparent-redis-layer
```

## Usage


try to get redis data, if key not exists, fill the data to the corresponding key.

## Example

```js

const Redis = require("ioredis");
const CacheLayer = require('./index');
const redis = new Redis({
  port: 6379,         // Redis port
  host: "127.0.0.1",  // Redis host
  family: 4,          // 4 (IPv4) or 6 (IPv6)
  // password: "auth",
  // db: 0
});

const cacheLayer = new CacheLayer();

// if actionForGet not set, then default command is "get". default set command is "set"
// this will "get test:basic", if test:basic not exists, then "set test:basic i am string"
const res = await cacheLayer
  .redisClient(redis)
  .cache('test:basic')
  .from('i am string')
  .exec();

// maintain redis clent
const funcRes = await cacheLayer
  .cache('test:function')
  .actionForGet('lrange', 0, -1)  // lrange test:function 0 -1
  .actionForSet('lpush')          // lpush test:function 0 1 2
  .from(function(){
    return [0, 1, 2];
  })
  .exec();

const promiseRes = await cacheLayer
  .cache('test:promise')
  .actionForGet('hget', 'age')  // hget test:promise age
  // use hmset instead of hset
  .actionForSet('hmset')        // hmset test:promise age 456
  .from(new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({age: 456});
    }, 1000);
  }))
  .exec();

const setParameterRes = await cacheLayer
  .cache('test:parameters')
  .actionForGet('get')        // get test:parameters
  .actionForSet('set', 'NX')  // set test:parameters 123 NX
  .from(async function() {
    return 123;
  })
  .exec();

// change to another redis client
const redis2 = new Redis({
  port: 6379,
  host: "127.0.0.1",
  family: 4,
  db: 1
});

const res = await cacheLayer
  .redisClient(redis2)
  .cache('test:basic')
  .from('i am string')
  .exec();

// customize check function
const cacheLayer = new CacheLayer();
await cacheLayer.redisClient(redis)
  .cache('test:assertFn') // first, put a key
  .from('123')
  .exec();

await cacheLayer
  .cache('test:assertFn')
  .actionForGet('get')        // get test:assertFn
  .assert(async function(res) {
   	return res === '345';   // if test:parameters is not 345, then set test:parameters 345
   })
  .actionForSet('set')  // set test:parameters 123
  .from(async function() {
   	return '345';
   })
  .exec();

```

See `test.js` for more examples.

## Updates

**1.0.3**

- Add assert function，customize check function whether to fill data or not.



## License

  MIT

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