2024-05-31 16:39:58 +08:00
|
|
|
// Package httpclient ...
|
|
|
|
//
|
|
|
|
// Description : httpclient ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-05-31 15:22
|
|
|
|
package httpclient
|
|
|
|
|
|
|
|
import (
|
2024-06-08 14:05:50 +08:00
|
|
|
"fmt"
|
2024-06-03 18:42:46 +08:00
|
|
|
"git.zhangdeman.cn/gateway/httpclient/cache"
|
2024-05-31 16:39:58 +08:00
|
|
|
"git.zhangdeman.cn/gateway/httpclient/define"
|
|
|
|
"git.zhangdeman.cn/gateway/httpclient/validate"
|
2024-06-03 18:42:46 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
2024-05-31 16:39:58 +08:00
|
|
|
"github.com/go-resty/resty/v2"
|
2024-06-05 21:54:06 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2024-06-09 19:56:32 +08:00
|
|
|
"net/http"
|
2024-06-03 18:42:46 +08:00
|
|
|
"strings"
|
2024-06-01 18:41:26 +08:00
|
|
|
"time"
|
2024-05-31 16:39:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHttpClient 获取http client
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:27 2024/5/31
|
2024-06-03 18:42:46 +08:00
|
|
|
func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*HttpClient, error) {
|
2024-05-31 16:39:58 +08:00
|
|
|
// 验证配置正确性以及初始化默认值
|
|
|
|
if err := validate.RequestConfig(reqConfig); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-31 18:00:24 +08:00
|
|
|
restyClient, restyRequest := NewRestyClient(reqConfig)
|
|
|
|
hc := &HttpClient{
|
2024-06-01 18:41:26 +08:00
|
|
|
Client: restyClient,
|
|
|
|
request: restyRequest,
|
|
|
|
reqConfig: reqConfig,
|
|
|
|
http4xxHandler: make([]define.Http4xxHandler, 0),
|
|
|
|
http5xxHandler: make([]define.Http5xxHandler, 0),
|
|
|
|
httpBusinessErrorHandler: make([]define.HttpBusinessErrorHandler, 0),
|
|
|
|
requestFinishHandler: make([]define.RequestFinishHandler, 0),
|
2024-06-03 18:42:46 +08:00
|
|
|
cacheInstance: cacheInstance,
|
2024-05-31 18:00:24 +08:00
|
|
|
}
|
2024-05-31 16:39:58 +08:00
|
|
|
return hc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpClient 请求客户端
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:27 2024/5/31
|
|
|
|
type HttpClient struct {
|
|
|
|
*resty.Client
|
2024-06-01 18:41:26 +08:00
|
|
|
request *resty.Request
|
|
|
|
reqConfig *define.Request
|
|
|
|
http4xxHandler []define.Http4xxHandler
|
|
|
|
http5xxHandler []define.Http5xxHandler
|
|
|
|
httpBusinessErrorHandler []define.HttpBusinessErrorHandler
|
|
|
|
requestSendErrorHandler []define.RequestSendErrorHandler
|
|
|
|
requestFinishHandler []define.RequestFinishHandler
|
2024-06-03 18:42:46 +08:00
|
|
|
cacheInstance cache.ICache
|
2024-06-01 18:41:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnResponse4xx 4xx处理逻辑
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:20 2024/6/1
|
|
|
|
func (hc *HttpClient) OnResponse4xx(handlerList ...define.Http4xxHandler) {
|
|
|
|
hc.http4xxHandler = append(hc.http4xxHandler, handlerList...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnResponse5xx 5xx处理逻辑
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:21 2024/6/1
|
|
|
|
func (hc *HttpClient) OnResponse5xx(handlerList ...define.Http5xxHandler) {
|
|
|
|
hc.http5xxHandler = append(hc.http5xxHandler, handlerList...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnResponseBusinessError 业务错误出路逻辑
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:22 2024/6/1
|
|
|
|
func (hc *HttpClient) OnResponseBusinessError(handlerList ...define.HttpBusinessErrorHandler) {
|
|
|
|
hc.httpBusinessErrorHandler = append(hc.httpBusinessErrorHandler, handlerList...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnRequestFinish 请求完成时间
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:36 2024/6/1
|
|
|
|
func (hc *HttpClient) OnRequestFinish(handlerList ...define.RequestFinishHandler) {
|
|
|
|
hc.requestFinishHandler = append(hc.requestFinishHandler, handlerList...)
|
2024-05-31 16:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getRequestValidateMiddleware 请求验证的Middleware
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:40 2024/5/31
|
|
|
|
func (hc *HttpClient) getRequestValidateMiddleware() resty.RequestMiddleware {
|
|
|
|
return func(client *resty.Client, request *resty.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getResponseValidateMiddleware 获取相应数据验证的middleware
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:42 2024/5/31
|
|
|
|
func (hc *HttpClient) getResponseValidateMiddleware() resty.ResponseMiddleware {
|
|
|
|
return func(client *resty.Client, response *resty.Response) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRestyClient 设置client
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:54 2024/5/31
|
|
|
|
func (hc *HttpClient) SetRestyClient(restyClient *resty.Client) {
|
|
|
|
hc.Client = restyClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRestyClient 获取 resty client
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:57 2024/5/31
|
|
|
|
func (hc *HttpClient) GetRestyClient() *resty.Client {
|
|
|
|
return hc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request 发送请求
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:52 2024/5/31
|
2024-05-31 18:51:31 +08:00
|
|
|
func (hc *HttpClient) Request() *define.Response {
|
2024-06-03 18:42:46 +08:00
|
|
|
var (
|
|
|
|
cacheResult *define.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if cacheResult = hc.getCacheResult(); nil != cacheResult {
|
2024-10-09 19:05:38 +08:00
|
|
|
// 判断是否开启预热
|
|
|
|
inputCachePreHeatConfig := hc.cacheInstance.PreHeatConfig()
|
|
|
|
var cachePreHeatConfig define.CachePreHeatConfig
|
|
|
|
serialize.JSON.TransitionIgnoreError(inputCachePreHeatConfig, &cachePreHeatConfig)
|
|
|
|
if !cachePreHeatConfig.Enable || (cachePreHeatConfig.MinTTL <= 0 && cachePreHeatConfig.MinPercent <= 0 && !cachePreHeatConfig.Force) {
|
|
|
|
// 无预热配置或未启用预热或者未设置预热规则
|
|
|
|
return cacheResult
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// 判断是否触发预热
|
|
|
|
if cachePreHeatConfig.Force {
|
|
|
|
_ = hc.requestBackendApi()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 将百分比的配置归一化成最小剩余时间的配置
|
|
|
|
if cachePreHeatConfig.MinPercent > 0 {
|
|
|
|
expectMinTTL := hc.cacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100
|
|
|
|
if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL {
|
|
|
|
cachePreHeatConfig.MinTTL = expectMinTTL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cachePreHeatConfig.MinTTL <= 0 {
|
|
|
|
// 未配置最小剩余时间
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ttl := hc.cacheInstance.TTL(cacheResult.CacheInfo.CacheKey)
|
|
|
|
if ttl < 0 {
|
|
|
|
// 不存在或者未设置有效期
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ttl <= cachePreHeatConfig.MinTTL {
|
|
|
|
// 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间
|
|
|
|
_ = hc.requestBackendApi()
|
|
|
|
}
|
|
|
|
}()
|
2024-06-03 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2024-10-09 18:48:33 +08:00
|
|
|
return hc.requestBackendApi()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestBackendApi 请求后端接口
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:47 2024/10/9
|
|
|
|
func (hc *HttpClient) requestBackendApi() *define.Response {
|
2024-05-31 18:37:35 +08:00
|
|
|
hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册
|
|
|
|
hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册
|
2024-05-31 18:51:31 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2024-06-05 21:54:06 +08:00
|
|
|
response := hc.newResponse()
|
2024-06-01 18:41:26 +08:00
|
|
|
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
|
2024-06-05 21:54:06 +08:00
|
|
|
response.Seq++
|
|
|
|
response.RequestCount++
|
2024-06-01 18:41:26 +08:00
|
|
|
if response.RestyResponse, err = hc.request.Send(); nil != err {
|
2024-06-05 21:54:06 +08:00
|
|
|
response.FailInfo = &define.ResponseFailInfo{
|
|
|
|
Type: define.RequestFailTypeSend,
|
|
|
|
Message: "response instance is nil",
|
|
|
|
}
|
|
|
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
|
|
|
itemAfterResponse(hc.reqConfig, response)
|
|
|
|
}
|
2024-06-01 18:41:26 +08:00
|
|
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
2024-06-05 21:54:06 +08:00
|
|
|
continue
|
2024-06-01 18:41:26 +08:00
|
|
|
}
|
2024-06-03 18:47:47 +08:00
|
|
|
|
2024-06-05 21:54:06 +08:00
|
|
|
if nil == response.RestyResponse {
|
|
|
|
response.FailInfo = &define.ResponseFailInfo{
|
|
|
|
Type: define.RequestFailTypeSend,
|
|
|
|
Message: "response instance is nil",
|
|
|
|
}
|
|
|
|
for _, itemAfterResponse := range hc.requestFinishHandler {
|
|
|
|
itemAfterResponse(hc.reqConfig, response)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// 解析返回信息
|
2024-06-06 22:54:49 +08:00
|
|
|
hc.fillResponseHeader(response)
|
|
|
|
hc.fillResponseCookie(response)
|
|
|
|
hc.fillResponseBody(response)
|
2024-06-09 19:56:32 +08:00
|
|
|
if response.HttpCode != http.StatusOK {
|
|
|
|
response.FailInfo = &define.ResponseFailInfo{
|
|
|
|
Type: define.RequestFailTypeServerError,
|
|
|
|
Message: "http code is " + response.HttpCodeStatus + ", not success",
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2024-06-06 22:54:49 +08:00
|
|
|
if !hc.isCodeSuccess(response) {
|
2024-06-09 19:56:32 +08:00
|
|
|
response.FailInfo = &define.ResponseFailInfo{
|
|
|
|
Type: define.RequestFailTypeBusinessError,
|
|
|
|
Message: "business code is " + response.Code + ", not success",
|
|
|
|
}
|
2024-06-06 22:54:49 +08:00
|
|
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2024-06-14 22:29:41 +08:00
|
|
|
response.IsSuccess = true //设置成功
|
|
|
|
response.CacheInfo.SetCache, response.CacheInfo.CacheError = hc.setCacheResult(response) // 设置缓存
|
2024-06-06 22:54:49 +08:00
|
|
|
break
|
2024-06-01 18:41:26 +08:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:48:45 +08:00
|
|
|
return response
|
2024-06-01 18:41:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newResponse 默认返回数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:44 2024/6/1
|
2024-06-05 21:54:06 +08:00
|
|
|
func (hc *HttpClient) newResponse() *define.Response {
|
2024-06-01 18:41:26 +08:00
|
|
|
return &define.Response{
|
2024-05-31 18:51:31 +08:00
|
|
|
Header: map[string]string{},
|
|
|
|
Cookie: map[string]string{},
|
2024-06-08 16:02:22 +08:00
|
|
|
Body: map[string]any{},
|
2024-05-31 18:51:31 +08:00
|
|
|
Code: "",
|
|
|
|
Message: "",
|
|
|
|
Data: "",
|
|
|
|
HttpCode: 0,
|
|
|
|
HttpCodeStatus: "",
|
|
|
|
ResponseDataRule: nil,
|
2024-06-05 21:54:06 +08:00
|
|
|
Seq: 0,
|
2024-05-31 18:51:31 +08:00
|
|
|
RequestStartTime: 0,
|
2024-06-03 18:42:46 +08:00
|
|
|
RequestFinishTime: 0,
|
2024-05-31 18:51:31 +08:00
|
|
|
UsedTime: 0,
|
|
|
|
RestyResponse: nil,
|
|
|
|
IsSuccess: false,
|
2024-06-14 22:29:41 +08:00
|
|
|
CacheInfo: &define.ResponseCacheInfo{
|
|
|
|
IsCache: false,
|
|
|
|
SetCache: false,
|
|
|
|
CacheKey: "",
|
|
|
|
CacheValue: "",
|
|
|
|
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
|
|
|
|
CacheError: nil,
|
|
|
|
},
|
|
|
|
RequestCount: 0,
|
|
|
|
FailInfo: nil,
|
2024-05-31 18:51:31 +08:00
|
|
|
}
|
2024-05-31 16:39:58 +08:00
|
|
|
}
|
2024-06-03 18:42:46 +08:00
|
|
|
|
2024-06-05 21:54:06 +08:00
|
|
|
// fillResponseHeader 填充响应header
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:30 2024/6/5
|
|
|
|
func (hc *HttpClient) fillResponseHeader(response *define.Response) {
|
|
|
|
response.Header = map[string]string{} // 清空已有数据
|
|
|
|
response.HttpCode = response.RestyResponse.StatusCode() // http状态码
|
|
|
|
response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述
|
|
|
|
for headerName, headerValue := range response.RestyResponse.Header() {
|
|
|
|
if len(headerValue) > 0 {
|
|
|
|
response.Header[headerName] = headerValue[0]
|
|
|
|
} else {
|
|
|
|
response.Header[headerName] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillResponseCookie 填充cookie
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:32 2024/6/5
|
|
|
|
func (hc *HttpClient) fillResponseCookie(response *define.Response) {
|
|
|
|
response.Cookie = map[string]string{} // 清空已有数据
|
|
|
|
for _, cookieValue := range response.RestyResponse.Cookies() {
|
|
|
|
response.Cookie[cookieValue.Name] = cookieValue.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillResponseBody 填充响应body
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:38 2024/6/5
|
|
|
|
func (hc *HttpClient) fillResponseBody(response *define.Response) {
|
2024-06-08 16:02:22 +08:00
|
|
|
response.Data = string(response.RestyResponse.Body())
|
|
|
|
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
|
|
|
|
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
|
|
|
|
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
|
2024-06-08 14:05:50 +08:00
|
|
|
if businessData.Value() == nil {
|
|
|
|
// data为空指针, 归一化成空对象
|
2024-06-08 16:02:22 +08:00
|
|
|
response.Body = map[string]any{}
|
2024-06-08 14:05:50 +08:00
|
|
|
} else {
|
|
|
|
if businessData.IsArray() {
|
|
|
|
// 数组类型的转换
|
|
|
|
response.Data = fmt.Sprintf(`{"list":` + businessData.String() + "}")
|
|
|
|
} else {
|
|
|
|
if businessData.IsObject() {
|
|
|
|
// 返回的就是对象
|
|
|
|
response.Data = businessData.String()
|
|
|
|
} else {
|
|
|
|
// 返回是普通类型
|
2024-10-24 21:48:01 +08:00
|
|
|
response.Data = serialize.JSON.MarshalForStringIgnoreError(map[string]any{
|
2024-06-08 14:05:50 +08:00
|
|
|
"value": businessData.Value(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 16:02:22 +08:00
|
|
|
_ = serialize.JSON.UnmarshalWithNumber([]byte(response.Data), &response.Body)
|
2024-06-08 14:05:50 +08:00
|
|
|
}
|
|
|
|
|
2024-06-05 21:54:06 +08:00
|
|
|
response.ExtendData = map[string]string{}
|
2024-06-08 16:02:22 +08:00
|
|
|
gjson.Parse(response.Data).ForEach(func(key, value gjson.Result) bool {
|
2024-06-05 21:54:06 +08:00
|
|
|
if key.String() == hc.reqConfig.CodeField ||
|
|
|
|
key.String() == hc.reqConfig.MessageField ||
|
|
|
|
key.String() == hc.reqConfig.DataField {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
response.ExtendData[key.String()] = value.String()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:54:49 +08:00
|
|
|
// isHttpCodeSuccess ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:48 2024/6/6
|
|
|
|
func (hc *HttpClient) isCodeSuccess(response *define.Response) bool {
|
|
|
|
for _, itemSuccessCode := range hc.reqConfig.SuccessCodeList {
|
|
|
|
if itemSuccessCode == response.Code {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-03 18:42:46 +08:00
|
|
|
// getCacheResult 获取缓存结果
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:04 2024/6/3
|
|
|
|
func (hc *HttpClient) getCacheResult() *define.Response {
|
|
|
|
if nil == hc.cacheInstance || !hc.cacheInstance.Enable() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
startTime := time.Now().UnixMilli()
|
|
|
|
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
|
|
|
cacheValue := strings.TrimSpace(hc.cacheInstance.GetValue(cacheKey))
|
|
|
|
if len(cacheValue) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-05 21:54:06 +08:00
|
|
|
response := hc.newResponse()
|
2024-06-03 18:42:46 +08:00
|
|
|
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 22:29:41 +08:00
|
|
|
response.CacheInfo.IsCache = true // 设置缓存标记
|
2024-06-03 18:42:46 +08:00
|
|
|
response.RequestStartTime = startTime // 开始时间
|
|
|
|
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
|
|
|
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
2024-06-14 22:29:41 +08:00
|
|
|
response.CacheInfo.CacheKey = cacheKey // 缓存key
|
|
|
|
response.CacheInfo.CacheValue = cacheValue // 缓存值
|
2024-06-03 18:42:46 +08:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCacheResult ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:24 2024/6/3
|
|
|
|
func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
|
|
|
if nil == response || nil == hc.cacheInstance {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// 全局未开启或者当前请求不支持缓存
|
|
|
|
if !hc.cacheInstance.Enable() || !hc.cacheInstance.IsAllow(hc.reqConfig, response) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
2024-10-24 21:48:01 +08:00
|
|
|
cacheValue := serialize.JSON.MarshalForStringIgnoreError(response)
|
2024-06-14 22:48:32 +08:00
|
|
|
if err := hc.cacheInstance.SetValue(cacheKey, cacheValue); nil != err {
|
2024-06-03 18:42:46 +08:00
|
|
|
return false, err
|
|
|
|
}
|
2024-06-14 22:29:41 +08:00
|
|
|
response.CacheInfo.CacheKey = cacheKey
|
2024-06-14 22:48:32 +08:00
|
|
|
response.CacheInfo.CacheValue = cacheValue
|
2024-06-03 18:42:46 +08:00
|
|
|
return true, nil
|
|
|
|
}
|