优化cache

This commit is contained in:
2024-06-14 22:29:41 +08:00
parent 533b948787
commit a21eb17f5f
2 changed files with 50 additions and 32 deletions

View File

@ -204,8 +204,8 @@ func (hc *HttpClient) Request() *define.Response {
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
response.IsSuccess = true //设置成功
response.IsCache, response.CacheError = hc.setCacheResult(response) // 设置缓存
response.IsSuccess = true //设置成功
response.CacheInfo.SetCache, response.CacheInfo.CacheError = hc.setCacheResult(response) // 设置缓存
break
}
@ -234,9 +234,16 @@ func (hc *HttpClient) newResponse() *define.Response {
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
CacheEnable: nil != hc.cacheInstance && hc.cacheInstance.Enable(),
RequestCount: 0,
FailInfo: nil,
CacheInfo: &define.ResponseCacheInfo{
IsCache: false,
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 {
return nil
}
response.IsCache = true // 设置缓存标记
response.CacheInfo.IsCache = true // 设置缓存标记
response.RequestStartTime = startTime // 开始时间
response.RequestFinishTime = time.Now().UnixMilli() // 结束时间
response.UsedTime = response.RequestFinishTime - response.RequestStartTime // 耗时
response.CacheKey = cacheKey // 缓存key
response.CacheValue = cacheValue // 缓存值
response.CacheInfo.CacheKey = cacheKey // 缓存key
response.CacheInfo.CacheValue = cacheValue // 缓存值
return response
}
@ -369,8 +376,9 @@ func (hc *HttpClient) setCacheResult(response *define.Response) (bool, error) {
return false, nil
}
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
}
response.CacheInfo.CacheKey = cacheKey
return true, nil
}