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 表示一个缓存项 // CacheItem 表示一个缓存项
type CacheItem struct { type CacheItem struct {
FilePath string FilePath string
ContentType string ContentType string
Size int64 ContentEncoding string
LastAccess time.Time Size int64
Hash string LastAccess time.Time
CreatedAt time.Time Hash string
AccessCount int64 CreatedAt time.Time
AccessCount int64
} }
// CacheStats 缓存统计信息 // CacheStats 缓存统计信息
@ -196,13 +197,14 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca
} }
item := &CacheItem{ item := &CacheItem{
FilePath: filePath, FilePath: filePath,
ContentType: resp.Header.Get("Content-Type"), ContentType: resp.Header.Get("Content-Type"),
Size: int64(len(body)), ContentEncoding: resp.Header.Get("Content-Encoding"),
LastAccess: time.Now(), Size: int64(len(body)),
Hash: hashStr, LastAccess: time.Now(),
CreatedAt: time.Now(), Hash: hashStr,
AccessCount: 1, CreatedAt: time.Now(),
AccessCount: 1,
} }
cm.items.Store(key, item) cm.items.Store(key, item)
@ -391,13 +393,14 @@ func (cm *CacheManager) Commit(key CacheKey, tempPath string, resp *http.Respons
// 创建缓存项 // 创建缓存项
item := &CacheItem{ item := &CacheItem{
FilePath: filePath, FilePath: filePath,
ContentType: resp.Header.Get("Content-Type"), ContentType: resp.Header.Get("Content-Type"),
Size: size, ContentEncoding: resp.Header.Get("Content-Encoding"),
LastAccess: time.Now(), Size: size,
Hash: hashStr, LastAccess: time.Now(),
CreatedAt: time.Now(), Hash: hashStr,
AccessCount: 1, CreatedAt: time.Now(),
AccessCount: 1,
} }
cm.items.Store(key, item) 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 { if item, hit, notModified := h.Cache.Get(cacheKey, r); hit {
// 从缓存提供响应 // 从缓存提供响应
w.Header().Set("Content-Type", item.ContentType) w.Header().Set("Content-Type", item.ContentType)
if item.ContentEncoding != "" {
w.Header().Set("Content-Encoding", item.ContentEncoding)
}
w.Header().Set("Proxy-Go-Cache", "HIT") w.Header().Set("Proxy-Go-Cache", "HIT")
if notModified { if notModified {
w.WriteHeader(http.StatusNotModified) 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 { if item, hit, notModified := h.Cache.Get(cacheKey, r); hit {
// 从缓存提供响应 // 从缓存提供响应
w.Header().Set("Content-Type", item.ContentType) w.Header().Set("Content-Type", item.ContentType)
if item.ContentEncoding != "" {
w.Header().Set("Content-Encoding", item.ContentEncoding)
}
w.Header().Set("Proxy-Go-Cache", "HIT") w.Header().Set("Proxy-Go-Cache", "HIT")
if notModified { if notModified {
w.WriteHeader(http.StatusNotModified) 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 { if item, hit, notModified := fixedPathCache.Get(cacheKey, r); hit {
// 从缓存提供响应 // 从缓存提供响应
w.Header().Set("Content-Type", item.ContentType) w.Header().Set("Content-Type", item.ContentType)
if item.ContentEncoding != "" {
w.Header().Set("Content-Encoding", item.ContentEncoding)
}
w.Header().Set("Proxy-Go-Cache", "HIT") w.Header().Set("Proxy-Go-Cache", "HIT")
if notModified { if notModified {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)