skip to Main Content

Node version: v14.15.4
Nest-js version: 9.0.0

app.module.ts
Here is the code.
In the app module, I am registering Redis as a cache manager.

@Module({
  imports: [
    CacheModule.register({
      isGlobal: true,
      store: redisStore,
      url: process.env.REDIS_URL,
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

service.ts
The cache data method is for storing data with a key. -> the problem is the set function doesn’t save anything

And get Data for returning the data by key.

@Injectable()
export class SomeService {
      constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}

      async cacheData(key: string, data): Promise<void> {
        await this.cacheManager.set(key, data);
      }

      async getData(key: string, data): Promise<any> {
        return this.cacheManager.get(key);
      }
}




It doesn’t throw any error in runtime.

6

Answers


  1. I am not sure why your getData function returns Promise<void> have you tried returning Promise<any> or the data type you are expecting e.g. Promise<string>. You could also try adding await.

     async getData(key: string, data): Promise<any> {
        return await this.cacheManager.get(key);
     }

    Are you sure that you are connecting to redis successfully ?. Have you tried adding password and tls (true/false) configuration ?

    @Module({
      imports: [
        CacheModule.register({
          isGlobal: true,
          store: redisStore,
          url: process.env.REDIS_URL,
          password: process.env.REDIS_PASSWORD,
          tls: process.env.REDIS_TLS
        })
      ],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    Login or Signup to reply.
  2. The default expiration time of the cache is 5 seconds.

    To disable expiration of the cache, set the ttl configuration property to 0:

    await this.cacheManager.set('key', 'value', { ttl: 0 });
    
    Login or Signup to reply.
  3. i has met the same problem as you,the way to fix this is using the install cmd to change the version of cache-manager-redis-store to 2.0.0 like ‘npm i [email protected]

    when you finish this step, the use of redisStore can be found,then the database can be linked.

    Login or Signup to reply.
  4. I had the same problem.

    It looks like NestJS v9 incompatible with version 5 of cache-manager. So you need to downgrade to v4 for now, until this issue is resolved: https://github.com/node-cache-manager/node-cache-manager/issues/210

    Change your package.json to have this in the dependencies:

    "cache-manager": "^4.0.0",` 
    

    Another commenter also suggested lowering the redis cache version.

    Login or Signup to reply.
  5. Set the redis store as redisStore.redisStore (You will get an autocomplete)

    CacheModule.register({
      isGlobal: true,
      store: redisStore.redisStore,
      url: process.env.REDIS_URL,
    })
    
    Login or Signup to reply.
  6. My stack:

    redis.service.ts:

    import {
      CACHE_MANAGER,
      Inject,
      Injectable,
      InternalServerErrorException,
    } from '@nestjs/common';
    import { Cache } from 'cache-manager';
    
    @Injectable()
    export class RedisService {
      constructor(
        @Inject(CACHE_MANAGER)
        private cacheManager: Cache,
      ) {}
      async set(key: string, value: unknown): Promise<void> {
        try {
          await this.cacheManager.set(key, value, 0);
        } catch (error) {
          throw new InternalServerErrorException();
        }
      }
    
      async get(key: string): Promise<unknown> {
        try {
          return await this.cacheManager.get(key);
        } catch (error) {
          throw new InternalServerErrorException();
        }
      }
    }
    

    redis.module.ts:

    import { CacheModule, Module } from '@nestjs/common';
    import type { RedisClientOptions } from 'redis';
    import { redisStore } from 'cache-manager-redis-yet';
    
    import {
      REDIS_HOST,
      REDIS_PORT,
    } from 'libs/config';
    import { RedisService } from './redis.service';
    
    @Module({
      imports: [
        CacheModule.register<RedisClientOptions>({
          isGlobal: true,
          store: redisStore,
          url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
        }),
      ],
      providers: [RedisService],
      exports: [RedisService],
    })
    export class RedisModule {}
    

    This code works fine for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search