feat(cache): Add Content-Encoding support in cache management

- Update CacheItem struct to include ContentEncoding field
- Modify Put and Commit methods to store Content-Encoding header
- Enhance cache retrieval to set Content-Encoding header for cached responses
- Implement Content-Encoding preservation across proxy and mirror handlers
This commit is contained in:
wood chen 2025-02-15 18:16:04 +08:00
parent 1c44fe1bf4
commit 35ea14a91f
4 changed files with 33 additions and 21 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)