feat: 缓存拦截器,新增 `cacheExcludeStatusCodeList` 用于排除某个响应码不使用缓存

pull/376/head
王良 3 weeks ago
parent 0840831298
commit 35faea4547

@ -6,10 +6,25 @@ module.exports = {
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) { responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log } = context const { rOptions, log } = context
// 只有GET请求且响应码为 2xx 或 3xx 时才进行缓存 // 只有GET请求
const minStatusCode = interceptOpt.cacheMinStatusCode || 200 if (rOptions.method !== 'GET') {
const maxStatusCode = interceptOpt.cacheMaxStatusCode || 303 return
if (rOptions.method !== 'GET' || proxyRes.statusCode < minStatusCode || proxyRes.statusCode > maxStatusCode) { }
// 判断当前响应码是否不使用缓存
if (interceptOpt.cacheExcludeStatusCodeList && interceptOpt.cacheExcludeStatusCodeList[proxyRes.statusCode + '']) {
return
}
// 响应码为 200~303 时才进行缓存(可通过以下两个参数调整范围)
let minStatusCode = interceptOpt.cacheMinStatusCode || 200
let maxStatusCode = interceptOpt.cacheMaxStatusCode || 303
if (minStatusCode > maxStatusCode) {
const temp = minStatusCode
minStatusCode = maxStatusCode
maxStatusCode = temp
}
if (proxyRes.statusCode < minStatusCode || proxyRes.statusCode > maxStatusCode) {
// res.setHeader('DS-Cache-Response-Interceptor', `skip: 'method' or 'status' not match`) // res.setHeader('DS-Cache-Response-Interceptor', `skip: 'method' or 'status' not match`)
return return
} }

Loading…
Cancel
Save