@@ -6,3 +6,7 @@ ALLOWED_ORIGINS=innei.ren,www.innei.ren
|
||||
# must be 64bit
|
||||
ENCRYPT_KEY=593f62860255feb0a914534a43814b9809cc7534da7f5485cd2e3d3c8609acab
|
||||
ENCRYPT_ENABLE=false
|
||||
|
||||
# CDN Cache header
|
||||
CDN_CACHE_HEADER=true
|
||||
FORCE_CACHE_HEADER=false
|
||||
|
||||
@@ -42,6 +42,17 @@ const commander = program
|
||||
// debug
|
||||
.option('--http_request_verbose', 'enable http request verbose')
|
||||
|
||||
// cache
|
||||
.option('--http_cache_ttl <number>', 'http cache ttl')
|
||||
.option(
|
||||
'--http_cache_enable_cdn_header',
|
||||
'enable http cache cdn header, s-maxage',
|
||||
)
|
||||
.option(
|
||||
'--http_cache_enable_force_cache_header',
|
||||
'enable http cache force cache header, max-age',
|
||||
)
|
||||
|
||||
// security
|
||||
.option('--encrypt_key <string>', 'custom encrypt key, default is machine-id')
|
||||
.option(
|
||||
@@ -114,9 +125,17 @@ export const REDIS = {
|
||||
port: argv.redis_port || 6379,
|
||||
password: argv.redis_password || null,
|
||||
ttl: null,
|
||||
httpCacheTTL: 15,
|
||||
max: 120,
|
||||
disableApiCache: isDev,
|
||||
// disableApiCache: false,
|
||||
}
|
||||
|
||||
export const HTTP_CACHE = {
|
||||
ttl: 15, // s
|
||||
enableCDNHeader:
|
||||
parseBooleanishValue(argv.http_cache_enable_cdn_header) ?? true, // s-maxage
|
||||
enableForceCacheHeader:
|
||||
parseBooleanishValue(argv.http_cache_enable_force_cache_header) ?? false, // cache-control: max-age
|
||||
}
|
||||
|
||||
export const AXIOS_CONFIG: AxiosRequestConfig = {
|
||||
|
||||
@@ -11,12 +11,13 @@ import type {
|
||||
ExecutionContext,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common'
|
||||
import type { FastifyReply } from 'fastify'
|
||||
import type { Observable } from 'rxjs'
|
||||
|
||||
import { Inject, Injectable, Logger, RequestMethod } from '@nestjs/common'
|
||||
import { HttpAdapterHost, Reflector } from '@nestjs/core'
|
||||
|
||||
import { REDIS } from '~/app.config'
|
||||
import { HTTP_CACHE, REDIS } from '~/app.config'
|
||||
import { API_CACHE_PREFIX } from '~/constants/cache.constant'
|
||||
import * as META from '~/constants/meta.constant'
|
||||
import * as SYSTEM from '~/constants/system.constant'
|
||||
@@ -67,6 +68,7 @@ export class HttpCacheInterceptor implements NestInterceptor {
|
||||
}
|
||||
|
||||
const handler = context.getHandler()
|
||||
|
||||
const isDisableCache = this.reflector.get(META.HTTP_CACHE_DISABLE, handler)
|
||||
|
||||
if (isDisableCache) {
|
||||
@@ -76,7 +78,29 @@ export class HttpCacheInterceptor implements NestInterceptor {
|
||||
if (request.user?.id) key = `${key}:logged-${request.user?.id}`
|
||||
|
||||
const metaTTL = this.reflector.get(META.HTTP_CACHE_TTL_METADATA, handler)
|
||||
const ttl = metaTTL || REDIS.httpCacheTTL
|
||||
const ttl = metaTTL || HTTP_CACHE.ttl
|
||||
|
||||
const res = context.switchToHttp().getResponse<FastifyReply>()
|
||||
|
||||
// 如果有则不覆盖
|
||||
if (!res.getHeader('cache-control')) {
|
||||
let cacheHeaderValue = ''
|
||||
|
||||
if (HTTP_CACHE.enableForceCacheHeader) {
|
||||
cacheHeaderValue += `max-age=${ttl}`
|
||||
}
|
||||
|
||||
if (HTTP_CACHE.enableCDNHeader) {
|
||||
if (cacheHeaderValue) cacheHeaderValue += ', '
|
||||
cacheHeaderValue += `s-maxage=${ttl}, stale-while-revalidate=60`
|
||||
res.header(
|
||||
'cdn-cache-control',
|
||||
`max-age=${ttl}, stale-while-revalidate=60`,
|
||||
)
|
||||
}
|
||||
|
||||
if (cacheHeaderValue) res.header('cache-control', cacheHeaderValue)
|
||||
}
|
||||
|
||||
try {
|
||||
const value = await this.cacheManager.get(key)
|
||||
@@ -84,6 +108,7 @@ export class HttpCacheInterceptor implements NestInterceptor {
|
||||
if (value) {
|
||||
this.logger.debug(`hit cache:${key}`)
|
||||
}
|
||||
|
||||
return value
|
||||
? of(value)
|
||||
: call$.pipe(
|
||||
|
||||
@@ -127,6 +127,7 @@ export const parseBooleanishValue = (value: string | boolean | undefined) => {
|
||||
if (value === 'true') return true
|
||||
if (value === 'false') return false
|
||||
}
|
||||
if (typeof value === 'undefined') return undefined
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ location /v2 {
|
||||
#Set Nginx Cache
|
||||
|
||||
proxy_ignore_headers Set-Cookie Cache-Control expires;
|
||||
add_header Cache-Control no-cache;
|
||||
expires 12h;
|
||||
|
||||
error_page 444 = @close_connection;
|
||||
|
||||
@@ -12,6 +12,8 @@ services:
|
||||
- JWT_SECRET
|
||||
- ENCRYPT_KEY
|
||||
- ENCRYPT_ENABLE
|
||||
- FORCE_CACHE_HEADER
|
||||
- CDN_CACHE_HEADER
|
||||
|
||||
volumes:
|
||||
- ./data/mx-space:/root/.mx-space
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
command="node index.js --redis_host=redis --db_host=mongo \
|
||||
--allowed_origins=${ALLOWED_ORIGINS} \
|
||||
--jwt_secret=${JWT_SECRET} \
|
||||
--color"
|
||||
--color \
|
||||
--http_cache_enable_cdn_header=${CDN_CACHE_HEADER} \
|
||||
--http_cache_enable_force_cache_header=${FORCE_CACHE_HEADER} \
|
||||
"
|
||||
|
||||
if [ -n "$ENCRYPT_KEY" ]; then
|
||||
command+=" --encrypt_key=${ENCRYPT_KEY}"
|
||||
|
||||
Reference in New Issue
Block a user