# @tadashi/cache-redis

> Using redis as cache

Latest version **7.0.1** (published 2024-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tadashi/cache-redis
pnpm add @tadashi/cache-redis
yarn add @tadashi/cache-redis
bun add @tadashi/cache-redis
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.0.1 |
| Published | 2024-09-08 |
| First published | 2017-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 1 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Thiago Lagden |
| Maintainers | lagden |
| Keywords | cache, redis, lib |

## Links

- npm: https://www.npmjs.com/package/@tadashi/cache-redis
- Repository: https://github.com/lagden/cache-redis
- Homepage: https://github.com/lagden/cache-redis#readme
- Issues: https://github.com/lagden/cache-redis/issues
- npm.io page: https://npm.io/package/@tadashi/cache-redis

## Dependencies (1)

- [@tadashi/jsonb](https://npm.io/package/@tadashi/jsonb.md) 5.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

- 7.0.1 (latest) — 2024-09-08
- 7.0.0 — 2024-09-08
- 6.4.1 — 2023-12-14
- 6.4.0 — 2023-06-12
- 6.3.1 — 2022-11-28
- 6.3.0 — 2022-04-01
- 6.2.0 — 2021-08-24
- 6.1.0 — 2021-06-24
- 6.0.0 — 2021-06-24
- 5.0.1 — 2021-03-30
- 5.0.0 — 2021-03-28
- 4.3.0 — 2021-01-11
- 4.2.0 — 2020-10-31
- 4.1.5 — 2020-07-17
- 4.1.4 — 2020-05-27
- … 10 more at https://npm.io/package/@tadashi/cache-redis/versions

## README

# cache-redis

[![NPM version][npm-img]][npm]
[![Node.js CI][ci-img]][ci]
[![Coverage Status][coveralls-img]][coveralls]
[![Snyk badge][snyk-img]][snyk]

[npm-img]:         https://img.shields.io/npm/v/@tadashi/cache-redis.svg
[npm]:             https://www.npmjs.com/package/@tadashi/cache-redis
[ci-img]:          https://github.com/lagden/cache-redis/actions/workflows/nodejs.yml/badge.svg
[ci]:              https://github.com/lagden/cache-redis/actions/workflows/nodejs.yml
[coveralls-img]:   https://coveralls.io/repos/github/lagden/cache-redis/badge.svg?branch=main
[coveralls]:       https://coveralls.io/github/lagden/cache-redis?branch=main
[snyk-img]:        https://snyk.io/test/github/lagden/cache-redis/badge.svg
[snyk]:            https://snyk.io/test/github/lagden/cache-redis


Making cache with Redis


## Install

```
$ npm i @tadashi/cache-redis
```


## API

### new Cache( \[opts\])

| parameter | type        | required | default            | description       |
| --------- | ----------- | -------- | ------------------ | ----------------- |
| opts      | Object      | no       | [see below](#opts) | Options for configuring the cache. |


#### opts

| parameter      | type             | required | default        | description                             |
| -------------- | ---------------- | -------- | -------------- | --------------------------------------- |
| address        | String\|String[] | no       | 127.0.0.1:6379 | The address of the Redis server.        |
| namespace      | String           | no       | app            | A namespace for the cache keys.         |
| redisOptions   | Object           | no       | -              | [See configuration options](https://redis.github.io/ioredis/interfaces/CommonRedisOptions.html) |


### Cluster

To use `Cluster`, set addresses separated by commas or an array and set [clusterOptions](https://redis.github.io/ioredis/interfaces/ClusterOptions.html).

```js
import Cache from '@tadashi/cache-redis'

const cache = new Cache({
  address: '127.0.0.1:6379, 127.0.0.1:6380, 127.0.0.1:6381',
  // or
  address: ['127.0.0.1:6379', '127.0.0.1:6380', '127.0.0.1:6381'],
  // and
  redisOptions: {
    clusterOptions: {
      retryDelayOnClusterDown: 500,
      // ...
    }
  }
})
```


## Usage

```js
import Cache from '@tadashi/cache-redis'

const _cache = new Cache({
  redisOptions: {
    keyPrefix: 'api'
  },
  namespace: 'example'
})

async function find(key) {
  try {
    const cache = await _cache.get(key)
    if (cache) {
      return cache
    }
    const result = await getDataFromSomeWhere(key)
    await _cache.set(key, result, 'PX', 3600)
    return result
  } catch (err) {
    throw err
  }
}


await find('foo')
// => data from getDataFromSomeWhere

await find('foo')
// => data from cache
```

---

> [!IMPORTANT]  
> Buy me a coffee!  
> BTC: `bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4`


## License

MIT © [Thiago Lagden](https://github.com/lagden)

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