Skip to content

Commit 4b8e088

Browse files
committed
feat: add factory to cache-key
Added factory to 'CacheKey' decorator and use it in interceptor
1 parent a404e30 commit 4b8e088

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

lib/decorators/cache-key.decorator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { SetMetadata } from '@nestjs/common';
1+
import { ExecutionContext, SetMetadata } from '@nestjs/common';
22
import { CACHE_KEY_METADATA } from '../cache.constants';
3+
export type CacheKeyFactory = (ctx: ExecutionContext) => string;
34

45
/**
56
* Decorator that sets the caching key used to store/retrieve cached items for
@@ -14,4 +15,5 @@ import { CACHE_KEY_METADATA } from '../cache.constants';
1415
*
1516
* @publicApi
1617
*/
17-
export const CacheKey = (key: string) => SetMetadata(CACHE_KEY_METADATA, key);
18+
export const CacheKey = (key: string | CacheKeyFactory) =>
19+
SetMetadata(CACHE_KEY_METADATA, key);

lib/decorators/cache-ttl.decorator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { ExecutionContext, SetMetadata } from '@nestjs/common';
22
import { CACHE_TTL_METADATA } from '../cache.constants';
3+
export type CacheTTLFactory = (
4+
ctx: ExecutionContext,
5+
) => Promise<number> | number;
36

47
/**
58
* Decorator that sets the cache ttl setting the duration for cache expiration.
@@ -12,6 +15,5 @@ import { CACHE_TTL_METADATA } from '../cache.constants';
1215
*
1316
* @publicApi
1417
*/
15-
type CacheTTLFactory = (ctx: ExecutionContext) => Promise<number> | number;
1618
export const CacheTTL = (ttl: number | CacheTTLFactory) =>
1719
SetMetadata(CACHE_TTL_METADATA, ttl);

lib/interceptors/cache.interceptor.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
CACHE_MANAGER,
1818
CACHE_TTL_METADATA,
1919
} from '../cache.constants';
20+
import { CacheKeyFactory, CacheTTLFactory } from '../decorators';
2021

2122
/**
2223
* @see [Caching](https://docs.nestjs.com/techniques/caching)
@@ -41,7 +42,7 @@ export class CacheInterceptor implements NestInterceptor {
4142
next: CallHandler,
4243
): Promise<Observable<any>> {
4344
const key = this.trackBy(context);
44-
const ttlValueOrFactory =
45+
const ttlValueOrFactory: number | CacheTTLFactory | null =
4546
this.reflector.get(CACHE_TTL_METADATA, context.getHandler()) ??
4647
this.reflector.get(CACHE_TTL_METADATA, context.getClass()) ??
4748
null;
@@ -90,13 +91,13 @@ export class CacheInterceptor implements NestInterceptor {
9091
protected trackBy(context: ExecutionContext): string | undefined {
9192
const httpAdapter = this.httpAdapterHost.httpAdapter;
9293
const isHttpApp = httpAdapter && !!httpAdapter.getRequestMethod;
93-
const cacheMetadata = this.reflector.get(
94-
CACHE_KEY_METADATA,
95-
context.getHandler(),
96-
);
94+
const cacheMetadataOrFactory: string | CacheKeyFactory | null =
95+
this.reflector.get(CACHE_KEY_METADATA, context.getHandler()) ?? null;
9796

98-
if (!isHttpApp || cacheMetadata) {
99-
return cacheMetadata;
97+
if (!isHttpApp || cacheMetadataOrFactory) {
98+
return isFunction(cacheMetadataOrFactory)
99+
? cacheMetadataOrFactory(context)
100+
: cacheMetadataOrFactory;
100101
}
101102

102103
const request = context.getArgByIndex(0);

0 commit comments

Comments
 (0)