I am using Redis as the cache manager in NestJs project. I was using a code like:
await this.productCacheManager.set('products/time', data, { ttl: 60} )
When I delete the ttl argument or just put 60 there, it doesn’t work and it immediately removes the record from redis, so I was using { ttl: 60} which was working until now.
I do not know what happend but now it throws an error like:
Argument of type ‘{ ttl: number; }’ is not assignable to parameter of type ‘number’.
The parameter I am typing is a number…
Trying to make it work again like before.
2
Answers
You only need to pass the number, not the object
{ ttl: 60}
, just use60
, because here it is only reserved for til and it knows it’s going to be a number.If you use
cache-manager
v4 andcache-manager-redis-store
v2, you need to pass an options object, as in(And that’s because the redis store v2 code expects an object)
If you are using
cache-manager
v5, though, you may pass an integer, although I’ve not yet been able to makecache-manager-redis-store@^3
work well withcache-manager@^5
.⚠️ As of today (2023-02-24), NestJS recommends using cache-manager v4.