相关文章推荐
坐怀不乱的大葱  ·  ❤️Android Google ...·  1 年前    · 
面冷心慈的树叶  ·  生信技能树 - 知乎·  1 年前    · 
稳重的企鹅  ·  c# - Using ...·  1 年前    · 
狂野的台灯  ·  ios - Swift: Make Int ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm implementing caching following the NestJS docs .

Create new project for this so there's no difference between docs example and this.

  • imports CacheModule.. just like docs
  • import { CacheModule, Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    @Module({
      imports: [CacheModule.register()],
      controllers: [AppController],
      providers: [AppService],
    export class AppModule {}
    
  • Inject CacheManager..
  • import { CACHE_MANAGER, Controller, Get, Inject } from '@nestjs/common';
    import { Cache } from 'cache-manager';
    @Controller()
    export class AppController {
      constructor(
        @Inject(CACHE_MANAGER) private cacheManager: Cache,
    
  • And use it
  •   @Get()
      async getHello(): Promise<string> {
        const value: string = await this.cacheManager.get('hello');
        if (value === null) {
          await this.cacheManager.set('hello', 'cache item', 60);
          return 'no cache exists';
        return value;
    

    but I get this error, I don't know why:

    TypeError: store.get is not a function
        at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
        at AppController.getHello
    

    This is a simple job, I think.

    So there's no one seems to get this error (I looked it up).

    I came across the same error and could not figure it out either.

    node-cache-manager was only updated to version 5 within the last day. https://github.com/node-cache-manager/node-cache-manager/tags

    So I updated the package.json to use version 4.x

    "cache-manager": "^4.0.0",
    

    And now the caching works as expected.

    Keep an eye on the package issue queue for further updates.

    To add onto this, the upgrade to v5 was a breaking change, and discussion about this specifically can be seen in this issue – Jay McDoniel Oct 3, 2022 at 2:38

    Follow these steps to get it working on v5

  • Upgrade @nestjs/core, @nestjs/common and @nestjs/cli to use > 9.4.0

  • install @nesjs/cache-manager package

  • in your app.ts

    import { CacheModule } from '@nestjs/cache-manager'; // not from "cache-manager"
    
  • add this
  •         CacheModule.register({
                isGlobal: true,
    // if you use redis
                useFactory: async () => ({
                    store: redisStore as any,
                    host: config.REDIS_HOST,
                    port: config.REDIS_PORT,
                    // ttl: 1000,
    

    NOTE: Don't use CacheModule.resgisterAsync

    Run Your app and it should work fine

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •