优化WebP+缓存

This commit is contained in:
刘祥超
2021-10-03 18:00:57 +08:00
parent 38a7cc17da
commit adadb52d4e
4 changed files with 42 additions and 12 deletions

View File

@@ -188,7 +188,7 @@ func (this *FileList) Exist(hash string) (bool, error) {
var expiredAt int64 var expiredAt int64
err = rows.Scan(&expiredAt) err = rows.Scan(&expiredAt)
if err != nil { if err != nil {
return true, nil return false, nil
} }
this.memoryCache.Write(hash, 1, expiredAt) this.memoryCache.Write(hash, 1, expiredAt)
return true, nil return true, nil

View File

@@ -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) err = storage.Purge(msg.Keys, msg.Type)
if err != nil { if err != nil {
this.replyFail(message.RequestId, "purge keys failed: "+err.Error()) this.replyFail(message.RequestId, "purge keys failed: "+err.Error())

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/caches" "github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"net/http" "net/http"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -98,6 +99,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
this.cacheRef = nil this.cacheRef = nil
return return
} }
this.cacheKey = key this.cacheKey = key
// 读取缓存 // 读取缓存
@@ -112,7 +114,20 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
bytePool32k.Put(buf) bytePool32k.Put(buf)
}() }()
reader, err := storage.OpenReader(key) 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 != nil {
if err == caches.ErrNotFound { if err == caches.ErrNotFound {
// cache相关变量 // cache相关变量
@@ -125,6 +140,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
} }
return return
} }
}
defer func() { defer func() {
_ = reader.Close() _ = reader.Close()
}() }()

View File

@@ -27,6 +27,7 @@ import (
// 限制WebP能够同时使用的Buffer内存使用量 // 限制WebP能够同时使用的Buffer内存使用量
const webpMaxBufferSize int64 = 1_000_000_000 const webpMaxBufferSize int64 = 1_000_000_000
const webpSuffix = "@GOEDGE_WEBP"
var webpTotalBufferSize int64 = 0 var webpTotalBufferSize int64 = 0
var webpBufferPool = utils.NewBufferPool(1024) var webpBufferPool = utils.NewBufferPool(1024)
@@ -494,7 +495,11 @@ func (this *HTTPWriter) prepareCache(size int64) {
life = 60 life = 60
} }
expiredAt := utils.UnixTime() + life 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 err != nil {
if !caches.CanIgnoreErr(err) { if !caches.CanIgnoreErr(err) {
remotelogs.Error("HTTP_WRITER", "write cache failed: "+err.Error()) remotelogs.Error("HTTP_WRITER", "write cache failed: "+err.Error())