NestJS使用CacheManager连接Redis调用缓存方法报错
解决NestJS CacheManager对接Redis时
store.set is not a function错误 问题场景
采用NestJS框架,通过CacheManager对接Redis缓存,已成功建立与Redis的连接,但调用set、get等缓存方法时,出现错误提示:store.set is not a function。
相关代码及报错信息如下:
app.module.ts
import { Module, CacheModule } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; const config = require('./config'); import { Logger } from './logger'; import * as redisStore from 'cache-manager-redis-store'; import type { RedisClientOptions } from "redis" @Module({ imports: [ CacheModule.register({ // @ts-ignore store: async () => await redisStore({ // Store-specific configuration: socket: { host: '******', port: 6380, password: '*****' } }) }), HttpModule ], controllers: [AppController], providers: [AppService, Logger], exports: [Logger], }) export class AppModule {}
app.service.ts
import { Cache } from 'cache-manager'; import { HttpException, HttpStatus, Injectable, OnApplicationBootstrap, OnApplicationShutdown, CACHE_MANAGER, Inject } from '@nestjs/common'; @Injectable() export class DeviceOnboardService implements OnApplicationBootstrap, OnApplicationShutdown { constructor( @Inject(CACHE_MANAGER) private cacheManager: Cache, private logger: Logger ) {} async getData(){ await this.cacheManager.set('test', 'XYZ', 600); } }
package.json(依赖部分)
"dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", "cache-manager": "^5.1.3", "cache-manager-redis-store": "^3.0.1", "redis": "^4.3.1", },
错误信息
[Nest] 25598 - 22/11/2022, 19:32:15 ERROR store.set is not a function
原因分析
核心问题是版本不兼容:
cache-manager-redis-store@3.x仅适配cache-manager@4.x版本,而当前项目使用的是cache-manager@5.x,两者API结构差异导致无法正确识别store实例的方法。cache-manager@5.x重构了存储适配器的接口规范,旧版的cache-manager-redis-store未同步适配。
解决方案
方案一:降级cache-manager兼容旧版redis-store
- 修改
package.json中的cache-manager版本为^4.1.0:
"dependencies": { // ...其他依赖 "cache-manager": "^4.1.0", "cache-manager-redis-store": "^3.0.1", // ...其他依赖 }
- 重新安装依赖:
npm install
- 修改
app.module.ts中的CacheModule注册逻辑,无需async包裹redisStore:
CacheModule.register({ store: redisStore, socket: { host: '******', port: 6380, password: '*****' } }),
方案二:使用适配cache-manager@5.x的Redis存储库
推荐使用cache-manager-redis-yet,它是专门适配cache-manager@5.x的Redis存储适配器:
- 卸载旧版存储库:
npm uninstall cache-manager-redis-store
- 安装新存储库:
npm install cache-manager-redis-yet
- 修改
app.module.ts的导入和配置:
import { Module, CacheModule } from '@nestjs/common'; // ...其他导入 import { redisStore } from 'cache-manager-redis-yet'; import type { RedisClientOptions } from 'redis'; @Module({ imports: [ CacheModule.register<RedisClientOptions>({ store: async () => await redisStore({ socket: { host: '******', port: 6380, password: '*****' } }), }), HttpModule ], // ...其他配置 }) export class AppModule {}
方案三:使用NestJS官方维护的@nestjs/cache-manager(推荐)
NestJS 8+版本推荐使用官方的@nestjs/cache-manager包,它内置了对cache-manager@5.x的支持:
- 安装依赖:
npm install @nestjs/cache-manager cache-manager-redis-yet
- 修改
app.module.ts:
import { Module } from '@nestjs/common'; import { CacheModule } from '@nestjs/cache-manager'; // ...其他导入 import { redisStore } from 'cache-manager-redis-yet'; import type { RedisClientOptions } from 'redis'; @Module({ imports: [ CacheModule.registerAsync<RedisClientOptions>({ useFactory: async () => ({ store: await redisStore({ socket: { host: '******', port: 6380, password: '*****' } }), }), }), HttpModule ], // ...其他配置 }) export class AppModule {}
验证
修改完成后重启服务,调用getData方法,检查Redis中是否成功写入test键值对,同时确认控制台不再出现store.set is not a function错误。
内容的提问来源于stack exchange,提问作者Sumit Balia
相关产品推荐
相关产品推荐

