#Having trouble with NestJS + Redis cache-manager: data not stored, always undefined when get

9 messages · Page 1 of 1 (latest)

dry vortex
#

Hi all 👋

I’m implementing Redis caching in a NestJS application.
I run Redis using Docker (basic image, tested PING works).

I want to use caching for /me endpoint (to store user profile) but plan to extend it for more global usage.

he setup:

  • Using @nestjs/cache-manager with cache-manager-redis-store@2.x.
  • Redis is up, logs from Nest show Cache SET works (my custom logger prints), but nothing gets stored.
  • When I get the same key, it always returns undefined.

What I’ve tested:

  • I manually connected to Redis server from host, created key-value (SET foo bar) and it works.
    But from my NestJS app:

  • calling cacheManager.set("somekey", "someval") prints logs from my service

  • calling cacheManager.get("somekey") immediately after returns undefined

  • Also verified with redis-cli KEYS * or GET somekey — key is not stored at all.

#

Appmodule.ts

  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    CacheModule.register({
      isGlobal: true,
      store: redisStore,
      host: 'localhost',
      port: 6379,
    }),
    // ... other modules
  ],
})
export class AppModule {}
#

⚙️ What I want
A stable caching config that connects to Redis via NestJS CacheModule, stores values reliably, and allows me to get them later.

Any examples for cache-manager-redis-store@2 or recommended alternative (ioredis) would be super helpful.

#
  constructor(
    private readonly userService: UserService,
    private readonly authService: AuthService,
    private readonly cacheService: RedisService,
  ) {}

  @Get('me')
  @ApiBearerAuth('access-token')
  @UseGuards(JwtGuard)
  @ApiExtraModels(SwaggerApiResponseData, AuthResponseDto)
  @ApiOperation({
    summary: 'Get current user',
    description: 'Retrieve details of the currently logged-in user.',
  })
  @DocApiResponseDataSuccess({ dataType: AuthResponseDto })
  @DocApiResponseError({
    messageExample: 'Unauthorized',
    statusCode: 401,
    description: 'Unauthorized. Token is invalid or missing.',
  })
  async me(@User() user: TLoggedUser) {
    const cacheKey = this.cacheService.generateUserProfileCacheKey(user.id);

    const dummyTes = await this.cacheService.get("jeijeie");
    console.log({dummyTes});

    const cachedUser = await this.cacheService.get(cacheKey);
    if (cachedUser) {
      console.log('cache hit');
      return {
        data: cachedUser,
        cached: true,
      };
    }

    const res = await this.userService.findOne(user.id);
    await this.cacheService.set(cacheKey, res, 300 );
    return {
      data: res,
      cached: false,
    };
  }
...
}
#
@Module({
  imports: [DatabaseModule, RedisModule],
  controllers: [AuthController],
  providers: [AuthService, UserService, JwtService, UniversitiesService],
})
export class AuthModule {}```
late venture
#

It seems like cache-manager-redis-store silently falls down to in memory store
https://github.com/dabroek/node-cache-manager-redis-store/issues/53
Try killing your redis instance. Would you get connection errors?
Maybe it's better to use something which is maintained

GitHub

V3.0.1 is incompatible with NestJS . Unable to register CacheModule using the RedisStore. "message": "Type 'typeof import("cache-test/node_modules/cache-manager-redis-store...

lost lagoon
#

Had similar issues. It falls back to memory by default.

late venture
#

Good choice to name package cache-manager-redis-store and don't use redis at all 😄