优化缓存管理日志记录,确保在请求为空时不会导致错误,并在日志中正确显示请求方法。

This commit is contained in:
wood chen 2025-07-12 00:49:36 +08:00
parent 818dd11dda
commit 5750062168
2 changed files with 8 additions and 1 deletions

View File

@ -230,7 +230,11 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca
}
cm.items.Store(key, item)
log.Printf("[Cache] NEW %s %s (%s) from %s", resp.Request.Method, key.URL, formatBytes(item.Size), utils.GetRequestSource(resp.Request))
method := "GET"
if resp.Request != nil {
method = resp.Request.Method
}
log.Printf("[Cache] NEW %s %s (%s) from %s", method, key.URL, formatBytes(item.Size), utils.GetRequestSource(resp.Request))
return item, nil
}

View File

@ -94,6 +94,9 @@ func GenerateRequestID() string {
// 获取请求来源
func GetRequestSource(r *http.Request) string {
if r == nil {
return ""
}
referer := r.Header.Get("Referer")
if referer != "" {
return fmt.Sprintf(" (from: %s)", referer)