I’m trying to set a TTL (time-to-live) for caching specific data in my NestJS application. I need to apply dynamic TTL values based on each module, rather than using a common generic setting.
<code>import { Cache, CACHE_MANAGER } from "@nestjs/cache-manager";
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
const serializedValue = JSON.stringify(value);
await this.cacheManager.set(key, serializedValue, {ttl});
}
</code>
<code>import { Cache, CACHE_MANAGER } from "@nestjs/cache-manager";
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
const serializedValue = JSON.stringify(value);
await this.cacheManager.set(key, serializedValue, {ttl});
}
</code>
import { Cache, CACHE_MANAGER } from "@nestjs/cache-manager";
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
const serializedValue = JSON.stringify(value);
await this.cacheManager.set(key, serializedValue, {ttl});
}
Am getting the below error while setting up the ttl like above,
<code>Argument of type '{ ttl: number; }' is not assignable to parameter of type 'number'.ts(2345)
</code>
<code>Argument of type '{ ttl: number; }' is not assignable to parameter of type 'number'.ts(2345)
</code>
Argument of type '{ ttl: number; }' is not assignable to parameter of type 'number'.ts(2345)
However, it keeps defaulting to some value around 85 minutes when executing code as below, and I’m not sure why this is happening.
<code>await this.cacheManager.set(key, serializedValue, ttl);
</code>
<code>await this.cacheManager.set(key, serializedValue, ttl);
</code>
await this.cacheManager.set(key, serializedValue, ttl);
My Module Versions are as below,
<code>"@nestjs/cache-manager": "^2.2.2",
"cache-manager": "^5.7.6",
"cache-manager-redis-store": "^2.0.0",
</code>
<code>"@nestjs/cache-manager": "^2.2.2",
"cache-manager": "^5.7.6",
"cache-manager-redis-store": "^2.0.0",
</code>
"@nestjs/cache-manager": "^2.2.2",
"cache-manager": "^5.7.6",
"cache-manager-redis-store": "^2.0.0",
Add cache like this
<code>await this.cacheManager.set(key, serializedValue, { ttl: ttl ?? 5 } as any);
</code>
<code>await this.cacheManager.set(key, serializedValue, { ttl: ttl ?? 5 } as any);
</code>
await this.cacheManager.set(key, serializedValue, { ttl: ttl ?? 5 } as any);
<code>import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { CacheStoreBase } from './cache.abstract';
@Injectable()
export class CacheStore extends CacheStoreBase {
constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {
super();
}
async deleteValue(key: string): Promise<void> {
await this.cache.del(key);
}
async resetAll(): Promise<void> {
await this.cache.reset();
}
async getValue(key: string): Promise<Object | undefined> {
const value = await this.cache.get(key);
return value || undefined;
}
async setValue(key: string, value: Object, ttl?: number): Promise<void> {
await this.cache.set(key, value, { ttl: ttl ?? 5 } as any);
}
}
</code>
<code>import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { CacheStoreBase } from './cache.abstract';
@Injectable()
export class CacheStore extends CacheStoreBase {
constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {
super();
}
async deleteValue(key: string): Promise<void> {
await this.cache.del(key);
}
async resetAll(): Promise<void> {
await this.cache.reset();
}
async getValue(key: string): Promise<Object | undefined> {
const value = await this.cache.get(key);
return value || undefined;
}
async setValue(key: string, value: Object, ttl?: number): Promise<void> {
await this.cache.set(key, value, { ttl: ttl ?? 5 } as any);
}
}
</code>
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { CacheStoreBase } from './cache.abstract';
@Injectable()
export class CacheStore extends CacheStoreBase {
constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {
super();
}
async deleteValue(key: string): Promise<void> {
await this.cache.del(key);
}
async resetAll(): Promise<void> {
await this.cache.reset();
}
async getValue(key: string): Promise<Object | undefined> {
const value = await this.cache.get(key);
return value || undefined;
}
async setValue(key: string, value: Object, ttl?: number): Promise<void> {
await this.cache.set(key, value, { ttl: ttl ?? 5 } as any);
}
}