mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +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
3
internal/cache/manager.go
vendored
3
internal/cache/manager.go
vendored
@ -46,6 +46,7 @@ func (k CacheKey) Hash() uint64 {
|
|||||||
type CacheItem struct {
|
type CacheItem struct {
|
||||||
FilePath string
|
FilePath string
|
||||||
ContentType string
|
ContentType string
|
||||||
|
ContentEncoding string
|
||||||
Size int64
|
Size int64
|
||||||
LastAccess time.Time
|
LastAccess time.Time
|
||||||
Hash string
|
Hash string
|
||||||
@ -198,6 +199,7 @@ 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"),
|
||||||
|
ContentEncoding: resp.Header.Get("Content-Encoding"),
|
||||||
Size: int64(len(body)),
|
Size: int64(len(body)),
|
||||||
LastAccess: time.Now(),
|
LastAccess: time.Now(),
|
||||||
Hash: hashStr,
|
Hash: hashStr,
|
||||||
@ -393,6 +395,7 @@ 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"),
|
||||||
|
ContentEncoding: resp.Header.Get("Content-Encoding"),
|
||||||
Size: size,
|
Size: size,
|
||||||
LastAccess: time.Now(),
|
LastAccess: time.Now(),
|
||||||
Hash: hashStr,
|
Hash: hashStr,
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user