缓存实例挂载到reqConfig上, 无需单独传入
This commit is contained in:
parent
1ca3c2d094
commit
f1a65c1ed8
@ -14,7 +14,6 @@ import (
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/log"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/validate"
|
||||
@ -28,7 +27,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:27 2024/5/31
|
||||
func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*HttpClient, error) {
|
||||
func NewHttpClient(reqConfig *define.Request) (*HttpClient, error) {
|
||||
if nil == reqConfig.Logger {
|
||||
reqConfig.Logger = log.Get() // 未单独指定日志实例, 则使用全局日志实例
|
||||
}
|
||||
@ -101,7 +100,6 @@ func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*Http
|
||||
http5xxHandler: make([]define.Http5xxHandler, 0),
|
||||
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
|
||||
requestFinishHandler: make([]define.RequestFinishHandler, 0),
|
||||
cacheInstance: cacheInstance,
|
||||
}
|
||||
hc.OnRequestFinish(func(req *define.Request, rep *define.Response) {
|
||||
if rep.IsSuccess {
|
||||
@ -129,7 +127,6 @@ type HttpClient struct {
|
||||
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
|
||||
requestSendErrorHandler []define.RequestSendErrorHandler
|
||||
requestFinishHandler []define.RequestFinishHandler
|
||||
cacheInstance cache.ICache
|
||||
}
|
||||
|
||||
// OnResponse4xx 4xx处理逻辑
|
||||
@ -219,14 +216,13 @@ func (hc *HttpClient) Request() *define.Response {
|
||||
)
|
||||
|
||||
if cacheResult = hc.getCacheResult(); nil == cacheResult {
|
||||
// 未命中缓存, 直接请求后端接口, 无需
|
||||
// 未命中缓存, 直接请求后端接口, 无需检测预热等逻辑
|
||||
return hc.requestBackendApi()
|
||||
}
|
||||
// 上面若命中缓存, 则后续缓存实例不可能为nil, 无需判断
|
||||
// TODO : 预热加锁, 并发请求触发预热, 仅触发一个即可
|
||||
// 判断是否开启预热
|
||||
inputCachePreHeatConfig := hc.cacheInstance.PreHeatConfig()
|
||||
var cachePreHeatConfig define.CachePreHeatConfig
|
||||
serialize.JSON.TransitionIgnoreError(inputCachePreHeatConfig, &cachePreHeatConfig)
|
||||
cachePreHeatConfig := hc.reqConfig.CacheInstance.PreHeatConfig()
|
||||
log.RecordDebug("接口请求命中缓存", map[string]any{
|
||||
"cache_info": cacheResult.CacheInfo,
|
||||
}, hc.reqConfig)
|
||||
@ -250,7 +246,7 @@ func (hc *HttpClient) Request() *define.Response {
|
||||
}
|
||||
// 将百分比的配置归一化成最小剩余时间的配置
|
||||
if cachePreHeatConfig.MinPercent > 0 {
|
||||
expectMinTTL := hc.cacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
|
||||
expectMinTTL := hc.reqConfig.CacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
|
||||
log.RecordDebug("接口请求命中缓存, 配置预热策略:有效时长剩余百分比", map[string]any{
|
||||
"cache_pre_heat_config": cachePreHeatConfig,
|
||||
"percent_min_ttl": expectMinTTL,
|
||||
@ -267,7 +263,7 @@ func (hc *HttpClient) Request() *define.Response {
|
||||
}, hc.reqConfig)
|
||||
return
|
||||
}
|
||||
ttl := hc.cacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
||||
ttl := hc.reqConfig.CacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
||||
if ttl < 0 {
|
||||
// 不存在或者未设置有效期
|
||||
log.RecordDebug("接口请求命中缓存, 当前缓存结果不存在或未设置有效期, 不执行预热", map[string]any{
|
||||
@ -480,7 +476,7 @@ func (hc *HttpClient) newResponse() *define.Response {
|
||||
SetCache: false,
|
||||
CacheKey: "",
|
||||
CacheValue: "",
|
||||
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
|
||||
CacheEnable: nil != hc.reqConfig.CacheInstance && hc.reqConfig.CacheInstance.Enable(),
|
||||
CacheError: nil,
|
||||
},
|
||||
RequestCount: 0,
|
||||
@ -581,17 +577,17 @@ func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
||||
//
|
||||
// Date : 16:04 2024/6/3
|
||||
func (hc *HttpClient) getCacheResult() *define.Response {
|
||||
if nil == hc.cacheInstance {
|
||||
if nil == hc.reqConfig.CacheInstance {
|
||||
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqConfig)
|
||||
return nil
|
||||
}
|
||||
if !hc.cacheInstance.Enable() {
|
||||
if !hc.reqConfig.CacheInstance.Enable() {
|
||||
log.RecordDebug("接口请求前缓存检测, 设置缓存实例, 但未启用缓存功能", map[string]any{}, hc.reqConfig)
|
||||
return nil
|
||||
}
|
||||
startTime := time.Now().UnixMilli()
|
||||
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
||||
cacheValue := strings.TrimSpace(hc.cacheInstance.GetValue(cacheKey))
|
||||
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
||||
cacheValue := strings.TrimSpace(hc.reqConfig.CacheInstance.GetValue(cacheKey))
|
||||
if len(cacheValue) == 0 {
|
||||
log.RecordDebug("接口请求前缓存检测, 未读取到缓存数据", map[string]any{}, hc.reqConfig)
|
||||
return nil
|
||||
@ -619,16 +615,16 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
||||
//
|
||||
// Date : 16:24 2024/6/3
|
||||
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
||||
if nil == response || nil == hc.cacheInstance {
|
||||
if nil == response || nil == hc.reqConfig.CacheInstance {
|
||||
log.RecordDebug("接口请求成功后, 缓存设置失败", map[string]any{
|
||||
"response_is_nil": response == nil,
|
||||
"cache_instance_is_nil": hc.cacheInstance == nil,
|
||||
"cache_instance_is_nil": hc.reqConfig.CacheInstance == nil,
|
||||
}, hc.reqConfig)
|
||||
return false, nil
|
||||
}
|
||||
// 全局未开启或者当前请求不支持缓存
|
||||
globalCacheEnable := hc.cacheInstance.Enable()
|
||||
currentRequestAllowCache := hc.cacheInstance.IsAllow(hc.reqConfig, response)
|
||||
globalCacheEnable := hc.reqConfig.CacheInstance.Enable()
|
||||
currentRequestAllowCache := hc.reqConfig.CacheInstance.IsAllow(hc.reqConfig, response)
|
||||
log.RecordDebug("检测缓存是否允许执行", map[string]any{
|
||||
"current_cache_enable": currentRequestAllowCache,
|
||||
"global_cache_enable": globalCacheEnable,
|
||||
@ -636,9 +632,9 @@ func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
||||
if !globalCacheEnable || !currentRequestAllowCache {
|
||||
return false, nil
|
||||
}
|
||||
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
||||
cacheKey := hc.reqConfig.CacheInstance.GetKey(hc.reqConfig)
|
||||
cacheValue := serialize.JSON.MarshalForStringIgnoreError(response)
|
||||
if err := hc.cacheInstance.SetValue(cacheKey, cacheValue); nil != err {
|
||||
if err := hc.reqConfig.CacheInstance.SetValue(cacheKey, cacheValue); nil != err {
|
||||
log.RecordWarn("开启结果缓存, 缓存设置失败", map[string]any{
|
||||
"current_cache_enable": currentRequestAllowCache,
|
||||
"global_cache_enable": globalCacheEnable,
|
||||
|
@ -9,6 +9,7 @@ package define
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/cache"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@ -40,6 +41,7 @@ type Request struct {
|
||||
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
|
||||
Logger *zap.Logger `json:"-"` // 日志记录器
|
||||
RateLimiter resty.RateLimiter `json:"-"` // 流控实例
|
||||
CacheInstance cache.ICache `json:"-"` // 数据结果缓存实例
|
||||
}
|
||||
|
||||
// RequestRetryRule 重试规则
|
||||
|
Loading…
x
Reference in New Issue
Block a user