尝试使用nestjs中的高速缓存管理器连接redis时出错

e5nqia27  于 2022-12-03  发布在  Redis
关注(0)|答案(1)|浏览(316)

使用nestjs框架和cash manager来连接redis缓存。我可以连接redis,但是当我使用set/get等方法时,会显示set不是函数的错误。添加了app module、service和package json以供参考

应用程序模块.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 {}

应用程序.服务.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);
  }

}

软件包.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",
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/cache-manager": "^4.0.2",
    "@types/express": "^4.17.13",
    "@types/jest": "27.5.0",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.0.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.1",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.0.0",
    "typescript": "^4.3.5"
  },

错误

[Nest] 25598  - 22/11/2022, 19:32:15   ERROR store.set is not a function
8zzbczxx

8zzbczxx1#

异步配置需要使用useFactory进行配置,它允许您使用async await,如果需要动态配置,还可以注入服务
当您使用useFactory时,您可以接着使用imports数组和inject。如下列范例所示,如果需要,您可以在'imports'数组中汇入configModule,并在inject数组中汇入configService。

CacheModule.registerAsync<RedisClientOptions>({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => {
    const store = await redisStore({
      socket: {
        host: configService.get('REDIS_HOST'),
        port: configService.get('REDIS_PORT'),
      },
    });

    return {
      store: {
        create: () => store,
      },
    };
  },
  inject: [ConfigService],
})

你需要返回store引用到create方法,如下面的链接所示,这里有两种类型的store,那么它应该可以工作

相关问题