@@ -17,6 +17,7 @@ import {
17
17
CACHE_MANAGER ,
18
18
CACHE_TTL_METADATA ,
19
19
} from '../cache.constants' ;
20
+ import { CacheKeyFactory , CacheTTLFactory } from '../decorators' ;
20
21
21
22
/**
22
23
* @see [Caching](https://docs.nestjs.com/techniques/caching)
@@ -41,7 +42,7 @@ export class CacheInterceptor implements NestInterceptor {
41
42
next : CallHandler ,
42
43
) : Promise < Observable < any > > {
43
44
const key = this . trackBy ( context ) ;
44
- const ttlValueOrFactory =
45
+ const ttlValueOrFactory : number | CacheTTLFactory =
45
46
this . reflector . get ( CACHE_TTL_METADATA , context . getHandler ( ) ) ??
46
47
this . reflector . get ( CACHE_TTL_METADATA , context . getClass ( ) ) ??
47
48
null ;
@@ -90,13 +91,15 @@ export class CacheInterceptor implements NestInterceptor {
90
91
protected trackBy ( context : ExecutionContext ) : string | undefined {
91
92
const httpAdapter = this . httpAdapterHost . httpAdapter ;
92
93
const isHttpApp = httpAdapter && ! ! httpAdapter . getRequestMethod ;
93
- const cacheMetadata = this . reflector . get (
94
+ const cacheMetadataOrFactory : string | CacheKeyFactory = this . reflector . get (
94
95
CACHE_KEY_METADATA ,
95
96
context . getHandler ( ) ,
96
97
) ;
97
98
98
- if ( ! isHttpApp || cacheMetadata ) {
99
- return cacheMetadata ;
99
+ if ( ! isHttpApp || cacheMetadataOrFactory ) {
100
+ return isFunction ( cacheMetadataOrFactory )
101
+ ? cacheMetadataOrFactory ( context )
102
+ : cacheMetadataOrFactory ;
100
103
}
101
104
102
105
const request = context . getArgByIndex ( 0 ) ;
0 commit comments