Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/decorators/cache-key.decorator.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
4 changes: 3 additions & 1 deletion lib/decorators/cache-ttl.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ExecutionContext, SetMetadata } from '@nestjs/common';
import { CACHE_TTL_METADATA } from '../cache.constants';
export type CacheTTLFactory = (
ctx: ExecutionContext,
) => Promise<number> | number;

/**
* Decorator that sets the cache ttl setting the duration for cache expiration.
Expand All @@ -12,6 +15,5 @@ import { CACHE_TTL_METADATA } from '../cache.constants';
*
* @publicApi
*/
type CacheTTLFactory = (ctx: ExecutionContext) => Promise<number> | number;
export const CacheTTL = (ttl: number | CacheTTLFactory) =>
SetMetadata(CACHE_TTL_METADATA, ttl);
15 changes: 8 additions & 7 deletions lib/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,7 +42,7 @@ export class CacheInterceptor implements NestInterceptor {
next: CallHandler,
): Promise<Observable<any>> {
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;
Expand Down Expand Up @@ -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);
Expand Down