Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@CacheTTL() decorator not working with cache-manager@5 #11119

Closed
3 of 15 tasks
doojin opened this issue Feb 15, 2023 · 2 comments
Closed
3 of 15 tasks

@CacheTTL() decorator not working with cache-manager@5 #11119

doojin opened this issue Feb 15, 2023 · 2 comments
Labels
needs triage This issue has not been looked into

Comments

@doojin
Copy link

doojin commented Feb 15, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Trying to implement an endpoint which caches it's response for a custom TTL period.

NestJS docs say I need to install the cache-manager package:

npm install cache-manager

Then I am registering the CacheModule:

@Module({
  imports: [CacheModule.register()],
  controllers: [AppController],
  providers: [],
})
export class AppModule {}

and I have a controller with two endpoints:

@Controller()
export class AppController {
  constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {}

  @Get('/number')
  @UseInterceptors(CacheInterceptor)
  // according to NestJS docs, cache-manager@5 is using milliseconds for TTL
  @CacheTTL(2000)
  getHello(): number {
    return Math.random();
  }

  @Get('/cache')
  async getCacheData(): Promise<any> {
    const keys = await this.cacheManager.store.keys();

    const ttls = {};
    for (const key of keys) {
      // Get TTL of every key in cache
      ttls[key] = await this.cacheManager.store.ttl(key);
    }

    return ttls;
  }
}

First endpoint generates a random number and tries to cache the result for 2 seconds.

Second endpoint is for debugging purposes. To obtain TTL of every item currently stored in a cache.

The problem is that when using @CacheTTL(2000) decorator, the result is being cached forever. Second endpoint shows that TTL of /number key becomes null for some reason. If I remove the @CacheTTL decorator, then the TTL becomes equal to default 5 seconds (or a custom TTL if configured via module CacheModule.register method) as expected.

Interesting, that the bug exists only when using cache-manager version 5+. When downgrading to v4, everything works as expected.

Minimum reproduction code

https://github.com/doojin/nestjs-cache-bug

Steps to reproduce

  1. npm install
  2. npm run start:dev
  3. Access http://localhost:3000/number - number will be generated, cached forever
  4. Access http://localhost:3000/cache - response will be:
{"/number":null}

Expected behavior

  1. http://localhost:3000/number endpoint should be cached for configured (via decorator) amount of time
  2. http://localhost:3000/cache endpoint should return a valid TTL number for /number key

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

cache-manager

NestJS version

^9.0.0

Packages versions

{
  "name": "testing-nest-cache",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "cache-manager": "^5.1.6",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@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": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

19.5.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@doojin doojin added the needs triage This issue has not been looked into label Feb 15, 2023
@Flusinerd
Copy link
Contributor

Can confirm it on windows. I'll take a look at it.

@kamilmysliwiec
Copy link
Member

Let's track this here #11131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants