@gogoout/cache-manager-redis-store

中文文档
2.1.0发布于 2021/04/12MITCommonJS近 30 天下载 10.8万

Redis store for node-cache-manager

原始使用文档
文档目录7 节
  1. How is this package different from node-cache-manager-redis…
  2. Installation
  3. Usage Examples
  4. Single store
  5. Multi-store
  6. Contribution
  7. License

build status coverage status dependencies status npm version GitHub issues

Redis store for node cache manager

Redis cache store for node-cache-manager.

How is this package different from node-cache-manager-redis?

This is a completely different version than the earlier node-cache-manager-redis. This package does not use redis-pool which is unnecessary and not actively maintained.

This package aims to provide the most simple wrapper possible by just passing the configuration to the underlying node_redis package.

Installation

npm install cache-manager-redis-store --save

or

yarn add cache-manager-redis-store

Usage Examples

See examples below on how to implement the Redis cache store.

Single store

var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');

var redisCache = cacheManager.caching({
  store: redisStore,
  host: 'localhost', // default value
  port: 6379, // default value
  auth_pass: 'XXXXX',
  db: 0,
  ttl: 600
});

// listen for redis connection error event
var redisClient = redisCache.store.getClient();

redisClient.on('error', (error) => {
  // handle error here
  console.log(error);
});

var ttl = 5;

redisCache.set('foo', 'bar', { ttl: ttl }, (err) => {
  if (err) {
    throw err;
  }

  redisCache.get('foo', (err, result) => {
    console.log(result);
    // >> 'bar'
    redisCache.del('foo', (err) => {
    });
  });
});

function getUser(id, cb) {
  setTimeout(() => {
    console.log("Returning user from slow database.");
    cb(null, { id: id, name: 'Bob' });
  }, 100);
}

var userId = 123;
var key = `user_${userId}`;

// Note: ttl is optional in wrap()
redisCache.wrap(key, (cb) => {
  getUser(userId, cb);
}, { ttl: ttl }, (err, user) => {
  console.log(user);

  // Second time fetches user from redisCache
  redisCache
    .wrap(key, () => getUser(userId))
    .then(console.log)
    .catch(err => {
      // handle error
    });
});

Multi-store

var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');

var redisCache = cacheManager.caching({ store: redisStore, db: 0, ttl: 600 });
var memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 60 });

var multiCache = cacheManager.multiCaching([memoryCache, redisCache]);

var userId2 = 456;
var key2 = `user_${userId2}`;

// Set value in all caches
multiCache.set('foo2', 'bar2', { ttl: ttl }, (err) => {
  if (err) {
    throw err;
  }

  // Fetches from highest priority cache that has the key
  multiCache.get('foo2', (err, result) => {
    console.log(result);

    // Delete from all caches
    multiCache.del('foo2');
  });
});

// Note: ttl is optional in wrap
multiCache.wrap(key2, (cb) => {
  getUser(userId2, cb);
}, (err, user) => {
  console.log(user);

  // Second time fetches user from memoryCache, since it's highest priority.
  // If the data expires in the memory cache, the next fetch would pull it from
  // the 'someOtherCache', and set the data in memory again.
  multiCache.wrap(key2, (cb) => {
    getUser(userId2, cb);
  }, (err, user) => {
    console.log(user);
  });
});

Contribution

Want to help improve this package? We take pull requests.

License

The node-cache-manager-redis-store is licensed under the MIT license.

版本与发布记录

@gogoout/cache-manager-redis-store 共发布 1 个版本,首次发布于 2021/04/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2021/04/125 年前

关于 @gogoout/cache-manager-redis-store 的常见问题

@gogoout/cache-manager-redis-store 是什么?

Redis store for node-cache-manager

如何安装 @gogoout/cache-manager-redis-store?

在项目目录运行 npm install @gogoout/cache-manager-redis-store,或使用 pnpm add / yarn add / bun add。

@gogoout/cache-manager-redis-store 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gogoout__cache-manager-redis-store 是否存在。

@gogoout/cache-manager-redis-store 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gogoout/cache-manager-redis-store 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 8.3。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@gogoout/cache-manager-redis-store 可以商用吗?

@gogoout/cache-manager-redis-store 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gogoout/cache-manager-redis-store 的下载量有多少?

根据 npm 官方下载统计,@gogoout/cache-manager-redis-store 在 2026/10/17 – 2026/11/15 这 30 天内被下载 108,362 次,日均约 3,612 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gogoout/cache-manager-redis-store 还在维护吗?

最新版本 2.1.0 发布于 2021/04/12(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。