优化cache
This commit is contained in:
parent
533b948787
commit
a21eb17f5f
26
client.go
26
client.go
@ -204,8 +204,8 @@ func (hc *HttpClient) Request() *define.Response {
|
|||||||
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
response.IsSuccess = true //设置成功
|
response.IsSuccess = true //设置成功
|
||||||
response.IsCache, response.CacheError = hc.setCacheResult(response) // 设置缓存
|
response.CacheInfo.SetCache, response.CacheInfo.CacheError = hc.setCacheResult(response) // 设置缓存
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,9 +234,16 @@ func (hc *HttpClient) newResponse() *define.Response {
|
|||||||
UsedTime: 0,
|
UsedTime: 0,
|
||||||
RestyResponse: nil,
|
RestyResponse: nil,
|
||||||
IsSuccess: false,
|
IsSuccess: false,
|
||||||
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
|
CacheInfo: &define.ResponseCacheInfo{
|
||||||
RequestCount: 0,
|
IsCache: false,
|
||||||
FailInfo: nil,
|
SetCache: false,
|
||||||
|
CacheKey: "",
|
||||||
|
CacheValue: "",
|
||||||
|
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
|
||||||
|
CacheError: nil,
|
||||||
|
},
|
||||||
|
RequestCount: 0,
|
||||||
|
FailInfo: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,12 +353,12 @@ func (hc *HttpClient) getCacheResult() *define.Response {
|
|||||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
response.IsCache = true // 设置缓存标记
|
response.CacheInfo.IsCache = true // 设置缓存标记
|
||||||
response.RequestStartTime = startTime // 开始时间
|
response.RequestStartTime = startTime // 开始时间
|
||||||
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
|
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
|
||||||
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
|
||||||
response.CacheKey = cacheKey // 缓存key
|
response.CacheInfo.CacheKey = cacheKey // 缓存key
|
||||||
response.CacheValue = cacheValue // 缓存值
|
response.CacheInfo.CacheValue = cacheValue // 缓存值
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,8 +376,9 @@ func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
cacheKey := hc.cacheInstance.GetKey(hc.reqConfig)
|
||||||
if err := hc.cacheInstance.SetValue(cacheKey, serialize.JSON.MarshalForString(response)); nil != err {
|
if err := hc.cacheInstance.SetValue(response.CacheInfo.CacheKey, serialize.JSON.MarshalForString(response)); nil != err {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
response.CacheInfo.CacheKey = cacheKey
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
@ -17,29 +17,25 @@ import (
|
|||||||
//
|
//
|
||||||
// Date : 12:34 2024/5/31
|
// Date : 12:34 2024/5/31
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Header map[string]string `json:"header"` // 响应header
|
Header map[string]string `json:"header"` // 响应header
|
||||||
Cookie map[string]string `json:"cookie"` // 响应cookie
|
Cookie map[string]string `json:"cookie"` // 响应cookie
|
||||||
Data string `json:"data"` // 响应body
|
Data string `json:"data"` // 响应body
|
||||||
Code string `json:"code"` // 业务状态码
|
Code string `json:"code"` // 业务状态码
|
||||||
Message string `json:"message"` // 业务状态码描述
|
Message string `json:"message"` // 业务状态码描述
|
||||||
Body map[string]any `json:"body"` // 响应数据
|
Body map[string]any `json:"body"` // 响应数据
|
||||||
ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据
|
ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据
|
||||||
HttpCode int `json:"http_code"` // http状态码
|
HttpCode int `json:"http_code"` // http状态码
|
||||||
HttpCodeStatus string `json:"http_code_status"` // http状态码描述
|
HttpCodeStatus string `json:"http_code_status"` // http状态码描述
|
||||||
ResponseDataRule map[string]any `json:"response_data_rule"` // 返回数据的验证规则
|
ResponseDataRule map[string]any `json:"response_data_rule"` // 返回数据的验证规则
|
||||||
Seq int `json:"seq"` // 第几次请求
|
Seq int `json:"seq"` // 第几次请求
|
||||||
RequestStartTime int64 `json:"request_start_time"` // 请求开始时间 : ms
|
RequestStartTime int64 `json:"request_start_time"` // 请求开始时间 : ms
|
||||||
RequestFinishTime int64 `json:"request_finish_time"` // 请求完成时间 : ms
|
RequestFinishTime int64 `json:"request_finish_time"` // 请求完成时间 : ms
|
||||||
UsedTime int64 `json:"used_time"` // 请求耗时 : ms
|
UsedTime int64 `json:"used_time"` // 请求耗时 : ms
|
||||||
RestyResponse *resty.Response `json:"-"` // 请求返回
|
RestyResponse *resty.Response `json:"-"` // 请求返回
|
||||||
IsSuccess bool `json:"is_success"` // 是否请求成功
|
IsSuccess bool `json:"is_success"` // 是否请求成功
|
||||||
RequestCount int `json:"request_count"` // 请求次数
|
RequestCount int `json:"request_count"` // 请求次数
|
||||||
FailInfo *ResponseFailInfo `json:"fail_info"` // 请求失败信息记录
|
FailInfo *ResponseFailInfo `json:"fail_info"` // 请求失败信息记录
|
||||||
IsCache bool `json:"-"` // 是否命中缓存
|
CacheInfo *ResponseCacheInfo `json:"cache_info"` // 缓存信息
|
||||||
CacheKey string `json:"-"` // 缓存key
|
|
||||||
CacheValue string `json:"-"` // 缓存值
|
|
||||||
CacheEnable bool `json:"cache_enable"` // 是否允许缓存
|
|
||||||
CacheError error `json:"-"` // 缓存是否异常
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseFailInfo 失败信息
|
// ResponseFailInfo 失败信息
|
||||||
@ -52,6 +48,20 @@ type ResponseFailInfo struct {
|
|||||||
Message string `json:"message"` // 失败信息
|
Message string `json:"message"` // 失败信息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResponseCacheInfo 缓存信息
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 22:26 2024/6/14
|
||||||
|
type ResponseCacheInfo struct {
|
||||||
|
IsCache bool `json:"is_cache"` // 是否命中缓存
|
||||||
|
SetCache bool `json:"set_cache"` // 是否设置缓存
|
||||||
|
CacheKey string `json:"cache_key"` // 缓存key
|
||||||
|
CacheValue string `json:"cache_value"` // 缓存值
|
||||||
|
CacheEnable bool `json:"cache_enable"` // 是否允许缓存
|
||||||
|
CacheError error `json:"-"` // 缓存是否异常
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RequestFailTypeSend = "SEND_REQUEST_FAIL" // 发送请求即失败, 问题出现在客户端
|
RequestFailTypeSend = "SEND_REQUEST_FAIL" // 发送请求即失败, 问题出现在客户端
|
||||||
RequestFailTypeClientError = "CLIENT_REQUEST_ERROR" // 请求失败, 原因出在客户端, 对应http code 4xx
|
RequestFailTypeClientError = "CLIENT_REQUEST_ERROR" // 请求失败, 原因出在客户端, 对应http code 4xx
|
||||||
|
Loading…
x
Reference in New Issue
Block a user