mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
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:
parent
1c44fe1bf4
commit
35ea14a91f
45
internal/cache/manager.go
vendored
45
internal/cache/manager.go
vendored
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user