From 4b8e088521a27e06e38ad3ecd956220abb81d08c Mon Sep 17 00:00:00 2001 From: Ivan Fomenko Date: Tue, 15 Apr 2025 11:41:50 +0300 Subject: [PATCH] feat: add factory to cache-key Added factory to 'CacheKey' decorator and use it in interceptor --- lib/decorators/cache-key.decorator.ts | 6 ++++-- lib/decorators/cache-ttl.decorator.ts | 4 +++- lib/interceptors/cache.interceptor.ts | 15 ++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/decorators/cache-key.decorator.ts b/lib/decorators/cache-key.decorator.ts index b03572c..50924b2 100644 --- a/lib/decorators/cache-key.decorator.ts +++ b/lib/decorators/cache-key.decorator.ts @@ -1,5 +1,6 @@ -import { SetMetadata } from '@nestjs/common'; +import { ExecutionContext, SetMetadata } from '@nestjs/common'; import { CACHE_KEY_METADATA } from '../cache.constants'; +export type CacheKeyFactory = (ctx: ExecutionContext) => string; /** * Decorator that sets the caching key used to store/retrieve cached items for @@ -14,4 +15,5 @@ import { CACHE_KEY_METADATA } from '../cache.constants'; * * @publicApi */ -export const CacheKey = (key: string) => SetMetadata(CACHE_KEY_METADATA, key); +export const CacheKey = (key: string | CacheKeyFactory) => + SetMetadata(CACHE_KEY_METADATA, key); diff --git a/lib/decorators/cache-ttl.decorator.ts b/lib/decorators/cache-ttl.decorator.ts index f80adde..202c1d9 100644 --- a/lib/decorators/cache-ttl.decorator.ts +++ b/lib/decorators/cache-ttl.decorator.ts @@ -1,5 +1,8 @@ import { ExecutionContext, SetMetadata } from '@nestjs/common'; import { CACHE_TTL_METADATA } from '../cache.constants'; +export type CacheTTLFactory = ( + ctx: ExecutionContext, +) => Promise | number; /** * Decorator that sets the cache ttl setting the duration for cache expiration. @@ -12,6 +15,5 @@ import { CACHE_TTL_METADATA } from '../cache.constants'; * * @publicApi */ -type CacheTTLFactory = (ctx: ExecutionContext) => Promise | number; export const CacheTTL = (ttl: number | CacheTTLFactory) => SetMetadata(CACHE_TTL_METADATA, ttl); diff --git a/lib/interceptors/cache.interceptor.ts b/lib/interceptors/cache.interceptor.ts index 0efcee3..a9df94a 100644 --- a/lib/interceptors/cache.interceptor.ts +++ b/lib/interceptors/cache.interceptor.ts @@ -17,6 +17,7 @@ import { CACHE_MANAGER, CACHE_TTL_METADATA, } from '../cache.constants'; +import { CacheKeyFactory, CacheTTLFactory } from '../decorators'; /** * @see [Caching](https://docs.nestjs.com/techniques/caching) @@ -41,7 +42,7 @@ export class CacheInterceptor implements NestInterceptor { next: CallHandler, ): Promise> { const key = this.trackBy(context); - const ttlValueOrFactory = + const ttlValueOrFactory: number | CacheTTLFactory | null = this.reflector.get(CACHE_TTL_METADATA, context.getHandler()) ?? this.reflector.get(CACHE_TTL_METADATA, context.getClass()) ?? null; @@ -90,13 +91,13 @@ export class CacheInterceptor implements NestInterceptor { protected trackBy(context: ExecutionContext): string | undefined { const httpAdapter = this.httpAdapterHost.httpAdapter; const isHttpApp = httpAdapter && !!httpAdapter.getRequestMethod; - const cacheMetadata = this.reflector.get( - CACHE_KEY_METADATA, - context.getHandler(), - ); + const cacheMetadataOrFactory: string | CacheKeyFactory | null = + this.reflector.get(CACHE_KEY_METADATA, context.getHandler()) ?? null; - if (!isHttpApp || cacheMetadata) { - return cacheMetadata; + if (!isHttpApp || cacheMetadataOrFactory) { + return isFunction(cacheMetadataOrFactory) + ? cacheMetadataOrFactory(context) + : cacheMetadataOrFactory; } const request = context.getArgByIndex(0);