-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
cache.interceptor.ts
executable file
·95 lines (85 loc) · 2.94 KB
/
cache.interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file Cache interceptor
* @module interceptor/cache
* @author Surmon <https://github.com/surmon-china>
*/
import { tap } from 'rxjs/operators'
import { Observable, of } from 'rxjs'
import { HttpAdapterHost } from '@nestjs/core'
import {
NestInterceptor,
ExecutionContext,
CallHandler,
Injectable,
RequestMethod,
StreamableFile
} from '@nestjs/common'
import { getCacheKey, getCacheTTL } from '@app/decorators/cache.decorator'
import { CacheService } from '@app/processors/cache/cache.service'
import { UNDEFINED, isNil } from '@app/constants/value.constant'
import { getDecoratorCacheKey } from '@app/constants/cache.constant'
import { createLogger } from '@app/utils/logger'
import { isDevEnv } from '@app/app.environment'
const logger = createLogger({ scope: 'CacheInterceptor', time: isDevEnv })
/**
* @class CacheInterceptor
* @classdesc Cache with ttl
* @ref https://github.com/nestjs/cache-manager/blob/master/lib/interceptors/cache.interceptor.ts
*/
@Injectable()
export class CacheInterceptor implements NestInterceptor {
constructor(
private readonly httpAdapterHost: HttpAdapterHost,
private readonly cacheService: CacheService
) {}
async intercept(context: ExecutionContext, next: CallHandler<any>): Promise<Observable<any>> {
// MARK: force disable cache
// return next.handle()
const key = this.trackBy(context)
if (!key) {
return next.handle()
}
const target = context.getHandler()
const ttl = getCacheTTL(target)
try {
const value = await this.cacheService.get(getDecoratorCacheKey(key))
if (!isNil(value)) {
return of(value)
}
return next.handle().pipe(
tap(async (response) => {
if (response instanceof StreamableFile) {
return
}
try {
await this.cacheService.set(getDecoratorCacheKey(key), response, ttl)
} catch (err) {
logger.warn(`An error has occurred when inserting "key: ${key}", "value: ${response}"`)
}
})
)
} catch (error) {
return next.handle()
}
}
/**
* @function trackBy
* @description
* 1. CacheKey is required
* 2. HTTP GET request only
*/
trackBy(context: ExecutionContext): string | undefined {
const { httpAdapter } = this.httpAdapterHost
const isHttpApp = Boolean(httpAdapter?.getRequestMethod)
const cacheKey = getCacheKey(context.getHandler())
const request = context.switchToHttp().getRequest()
const isGetRequest = isHttpApp && httpAdapter.getRequestMethod(request) === RequestMethod[RequestMethod.GET]
return isHttpApp && isGetRequest && cacheKey ? cacheKey : UNDEFINED
/*
Cache priority strategy: HTTP > GET > Cache Key -> URL -> undefined
const requestUrl = httpAdapter.getRequestUrl(request)
console.debug('isMatchedCache', { isHttpApp, isGetRequest, cacheKey, requestUrl })
return isHttpApp && isGetRequest ? (cacheKey || requestUrl) : undefined;
*/
}
}