优化Partial Content缓存

This commit is contained in:
GoEdgeLab
2022-03-04 22:42:03 +08:00
parent cf074fdaea
commit 1d3319a0b7
8 changed files with 104 additions and 29 deletions

View File

@@ -65,7 +65,8 @@ type HTTPWriter struct {
isFinished bool // 是否已完成
// Partial
isPartial bool
isPartial bool
partialFileIsNew bool
// WebP
webpIsEncoding bool
@@ -284,6 +285,10 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
}
this.cacheWriter = cacheWriter
if this.isPartial {
this.partialFileIsNew = cacheWriter.(*caches.PartialFileWriter).IsNew()
}
// 写入Header
for k, v := range this.Header() {
for _, v1 := range v {
@@ -912,6 +917,28 @@ func (this *HTTPWriter) Close() {
if this.isOk && this.cacheWriter != nil {
err := this.cacheWriter.Close()
if err == nil {
if !this.isPartial || this.partialFileIsNew {
var expiredAt = this.cacheWriter.ExpiredAt()
this.cacheStorage.AddToList(&caches.Item{
Type: this.cacheWriter.ItemType(),
Key: this.cacheWriter.Key(),
ExpiredAt: expiredAt,
StaleAt: expiredAt + int64(this.calculateStaleLife()),
HeaderSize: this.cacheWriter.HeaderSize(),
BodySize: this.cacheWriter.BodySize(),
Host: this.req.ReqHost,
ServerId: this.req.ReqServer.Id,
})
}
}
}
} else {
if !this.isPartial || !this.cacheIsFinished {
_ = this.cacheWriter.Discard()
} else {
// Partial的文件内容不删除
err := this.cacheWriter.Close()
if err == nil && this.partialFileIsNew {
var expiredAt = this.cacheWriter.ExpiredAt()
this.cacheStorage.AddToList(&caches.Item{
Type: this.cacheWriter.ItemType(),
@@ -925,8 +952,6 @@ func (this *HTTPWriter) Close() {
})
}
}
} else {
_ = this.cacheWriter.Discard()
}
}