diff --git a/internal/cache/manager.go b/internal/cache/manager.go index f9123d5..de3e256 100644 --- a/internal/cache/manager.go +++ b/internal/cache/manager.go @@ -44,13 +44,14 @@ func (k CacheKey) Hash() uint64 { // CacheItem 表示一个缓存项 type CacheItem struct { - FilePath string - ContentType string - Size int64 - LastAccess time.Time - Hash string - CreatedAt time.Time - AccessCount int64 + FilePath string + ContentType string + ContentEncoding string + Size int64 + LastAccess time.Time + Hash string + CreatedAt time.Time + AccessCount int64 } // CacheStats 缓存统计信息 @@ -196,13 +197,14 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca } item := &CacheItem{ - FilePath: filePath, - ContentType: resp.Header.Get("Content-Type"), - Size: int64(len(body)), - LastAccess: time.Now(), - Hash: hashStr, - CreatedAt: time.Now(), - AccessCount: 1, + FilePath: filePath, + ContentType: resp.Header.Get("Content-Type"), + ContentEncoding: resp.Header.Get("Content-Encoding"), + Size: int64(len(body)), + LastAccess: time.Now(), + Hash: hashStr, + CreatedAt: time.Now(), + AccessCount: 1, } cm.items.Store(key, item) @@ -391,13 +393,14 @@ func (cm *CacheManager) Commit(key CacheKey, tempPath string, resp *http.Respons // 创建缓存项 item := &CacheItem{ - FilePath: filePath, - ContentType: resp.Header.Get("Content-Type"), - Size: size, - LastAccess: time.Now(), - Hash: hashStr, - CreatedAt: time.Now(), - AccessCount: 1, + FilePath: filePath, + ContentType: resp.Header.Get("Content-Type"), + ContentEncoding: resp.Header.Get("Content-Encoding"), + Size: size, + LastAccess: time.Now(), + Hash: hashStr, + CreatedAt: time.Now(), + AccessCount: 1, } cm.items.Store(key, item) diff --git a/internal/handler/mirror_proxy.go b/internal/handler/mirror_proxy.go index 4e13e1a..78dee6c 100644 --- a/internal/handler/mirror_proxy.go +++ b/internal/handler/mirror_proxy.go @@ -122,6 +122,9 @@ func (h *MirrorProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if item, hit, notModified := h.Cache.Get(cacheKey, r); hit { // 从缓存提供响应 w.Header().Set("Content-Type", item.ContentType) + if item.ContentEncoding != "" { + w.Header().Set("Content-Encoding", item.ContentEncoding) + } w.Header().Set("Proxy-Go-Cache", "HIT") if notModified { w.WriteHeader(http.StatusNotModified) diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index dac0aba..a8804c7 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -274,6 +274,9 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if item, hit, notModified := h.Cache.Get(cacheKey, r); hit { // 从缓存提供响应 w.Header().Set("Content-Type", item.ContentType) + if item.ContentEncoding != "" { + w.Header().Set("Content-Encoding", item.ContentEncoding) + } w.Header().Set("Proxy-Go-Cache", "HIT") if notModified { w.WriteHeader(http.StatusNotModified) diff --git a/internal/middleware/fixed_path_proxy.go b/internal/middleware/fixed_path_proxy.go index f77a97e..a17be66 100644 --- a/internal/middleware/fixed_path_proxy.go +++ b/internal/middleware/fixed_path_proxy.go @@ -51,6 +51,9 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle if item, hit, notModified := fixedPathCache.Get(cacheKey, r); hit { // 从缓存提供响应 w.Header().Set("Content-Type", item.ContentType) + if item.ContentEncoding != "" { + w.Header().Set("Content-Encoding", item.ContentEncoding) + } w.Header().Set("Proxy-Go-Cache", "HIT") if notModified { w.WriteHeader(http.StatusNotModified)