diff --git a/internal/caches/list_file.go b/internal/caches/list_file.go index 8c6c189..ebb30f7 100644 --- a/internal/caches/list_file.go +++ b/internal/caches/list_file.go @@ -188,7 +188,7 @@ func (this *FileList) Exist(hash string) (bool, error) { var expiredAt int64 err = rows.Scan(&expiredAt) if err != nil { - return true, nil + return false, nil } this.memoryCache.Write(hash, 1, expiredAt) return true, nil diff --git a/internal/nodes/api_stream.go b/internal/nodes/api_stream.go index 024a0ca..5386a90 100644 --- a/internal/nodes/api_stream.go +++ b/internal/nodes/api_stream.go @@ -342,6 +342,15 @@ func (this *APIStream) handlePurgeCache(message *pb.NodeStreamMessage) error { }() } + // WEBP缓存 + if msg.Type == "file" { + var keys = msg.Keys + for _, key := range keys { + keys = append(keys, key+webpSuffix) + } + msg.Keys = keys + } + err = storage.Purge(msg.Keys, msg.Type) if err != nil { this.replyFail(message.RequestId, "purge keys failed: "+err.Error()) diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index 521c105..ae4ccbb 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -6,6 +6,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/caches" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "net/http" + "path/filepath" "strconv" "strings" "time" @@ -98,6 +99,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { this.cacheRef = nil return } + this.cacheKey = key // 读取缓存 @@ -112,18 +114,32 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { bytePool32k.Put(buf) }() - reader, err := storage.OpenReader(key) - if err != nil { - if err == caches.ErrNotFound { - // cache相关变量 - this.varMapping["cache.status"] = "MISS" + var reader caches.Reader + var err error + + // 是否优先检查WebP + if this.web.WebP != nil && + this.web.WebP.IsOn && + this.web.WebP.MatchRequest(filepath.Ext(this.requestPath()), this.Format) && + this.web.WebP.MatchAccept(this.requestHeader("Accept")) { + reader, _ = storage.OpenReader(key + webpSuffix) + } + + // 检查正常的文件 + if reader == nil { + reader, err = storage.OpenReader(key) + if err != nil { + if err == caches.ErrNotFound { + // cache相关变量 + this.varMapping["cache.status"] = "MISS" + return + } + + if !this.canIgnore(err) { + remotelogs.Warn("HTTP_REQUEST_CACHE", "read from cache failed: "+err.Error()) + } return } - - if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", "read from cache failed: "+err.Error()) - } - return } defer func() { _ = reader.Close() diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index d94881c..e0f55b1 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -27,6 +27,7 @@ import ( // 限制WebP能够同时使用的Buffer内存使用量 const webpMaxBufferSize int64 = 1_000_000_000 +const webpSuffix = "@GOEDGE_WEBP" var webpTotalBufferSize int64 = 0 var webpBufferPool = utils.NewBufferPool(1024) @@ -494,7 +495,11 @@ func (this *HTTPWriter) prepareCache(size int64) { life = 60 } expiredAt := utils.UnixTime() + life - cacheWriter, err := storage.OpenWriter(this.req.cacheKey, expiredAt, this.StatusCode()) + var cacheKey = this.req.cacheKey + if this.webpIsEncoding { + cacheKey += webpSuffix + } + cacheWriter, err := storage.OpenWriter(cacheKey, expiredAt, this.StatusCode()) if err != nil { if !caches.CanIgnoreErr(err) { remotelogs.Error("HTTP_WRITER", "write cache failed: "+err.Error())