# cachex

> Cache Hook

Latest version **5.0.0** (published 2025-04-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install cachex
pnpm add cachex
yarn add cachex
bun add cachex
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2025-04-10 |
| First published | 2015-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 1 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jackson Tian |
| Maintainers | jacksontian |
| Keywords | cache |

## Links

- npm: https://www.npmjs.com/package/cachex
- Repository: https://github.com/JacksonTian/cachex
- Homepage: https://github.com/JacksonTian/cachex#readme
- Issues: https://github.com/JacksonTian/cachex/issues
- npm.io page: https://npm.io/package/cachex

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^4.4.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

- 5.0.0 (latest) — 2025-04-10
- 4.0.0 — 2025-03-13
- 3.0.0 — 2018-02-02
- 2.2.0 — 2017-04-26
- 2.1.0 — 2016-06-07
- 2.0.0 — 2015-11-23
- 1.0.1 — 2015-10-15
- 1.0.0 — 2015-10-10

## README

# cachex

A cache hook

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/JacksonTian/cachex/actions/workflows/build.yml/badge.svg)](https://github.com/JacksonTian/cachex/actions/workflows/build.yml)
[![codecov][cov-image]][cov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/cachex.svg?style=flat-square
[npm-url]: https://npmjs.org/package/cachex
[cov-image]: https://codecov.io/gh/JacksonTian/cachex/branch/master/graph/badge.svg
[cov-url]: https://codecov.io/gh/JacksonTian/cachex
[download-image]: https://img.shields.io/npm/dm/cachex.svg?style=flat-square
[download-url]: https://npmjs.org/package/cachex

Features:

- No need to modify any existing code
- Auto generate cache key
- Auto serialize and deserialize data
- Built-in cache penetration solution

## Installation

```sh
npm i cachex --save
```

## Usage

If you have origin SQL query, it is `db.js`:

```js
async function getRows() {
  // mock a slow query
  return await db.query(sql);
}
```

Before use `cachex`, you must provide a cache storage, it can be redis or memcached or memory.

```js
var inMemory = {};

var store = {
  get: async function (key) {
    return inMemory[key];
  },
  setex: async function (key, value, expire) {
    inMemory[key] = value;
    setTimeout(function () {
      delete inMemory[key];
    }, expire);
  }
};
```

The storage object must have get/setex method.

```js
// db_with_cache.js
import cachex from 'cachex';
import db from './db.js';

// cache result 10s
export const getRows = cachex(store, 'db', 'getRows', db.getRows, 10);
```

Running go:

```js
import dbx from './db_with_cache.js';
// from db
await dbx.getRows();

// from cache
await dbx.getRows();
// ..10s..pass..
// from db
await dbx.getRows();
```

### Use with Redis

```js
import { createClient } from 'redis';

const redis = createClient({
  // redis connection options
});
await redis.connect();

// cachex will auto serialize/deserialize
const store = {
  get: async function (key) {
    return await redis.GET(key);
  },
  setex: async function (key, value, expire) {
    await redis.SETEX(key, expire, value);
  }
};

// cache result 10s
export const getRows = cachex(store, 'db', 'getRows', db.getRows, 10);
```

## License

The MIT license

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