You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Controller()exportclassAppController{constructor(@Inject(CACHE_MANAGER)privatereadonlycacheManager: Cache){}
@Get('/number')
@UseInterceptors(CacheInterceptor)// according to NestJS docs, cache-manager@5 is using milliseconds for TTL
@CacheTTL(2000)getHello(): number{returnMath.random();}
@Get('/cache')asyncgetCacheData(): Promise<any>{
const keys=awaitthis.cacheManager.store.keys();constttls={};for(constkeyofkeys){// Get TTL of every key in cachettls[key]=awaitthis.cacheManager.store.ttl(key);}returnttls;}}
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.
Is there an existing issue for this?
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:Then I am registering the
CacheModule
:and I have a controller with two endpoints:
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 becomesnull
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 moduleCacheModule.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
npm install
npm run start:dev
http://localhost:3000/number
- number will be generated, cached foreverhttp://localhost:3000/cache
- response will be:Expected behavior
http://localhost:3000/number
endpoint should be cached for configured (via decorator) amount of timehttp://localhost:3000/cache
endpoint should return a valid TTL number for/number
keyPackage
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
cache-manager
NestJS version
^9.0.0
Packages versions
Node.js version
19.5.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: