缓存策略增加“允许读取不完整的Partial Content”选项

This commit is contained in:
GoEdgeLab
2024-05-07 20:06:06 +08:00
parent bbc892ad4b
commit c463b2a053
2 changed files with 18 additions and 7 deletions

View File

@@ -1004,6 +1004,11 @@ func (this *FileStorage) CanSendfile() bool {
return this.options.EnableSendfile return this.options.EnableSendfile
} }
// Options 获取当前缓存存储的选项
func (this *FileStorage) Options() *serverconfigs.HTTPFileCacheStorage {
return this.options
}
// 获取Key对应的文件路径 // 获取Key对应的文件路径
func (this *FileStorage) keyPath(key string) (hash string, path string, diskIsFull bool) { func (this *FileStorage) keyPath(key string) (hash string, path string, diskIsFull bool) {
hash = stringutil.Md5(key) hash = stringutil.Md5(key)

View File

@@ -722,14 +722,20 @@ func (this *HTTPRequest) tryPartialReader(storage caches.StorageInterface, key s
ranges[0][1] < 0 && ranges[0][1] < 0 &&
!partialReader.IsCompleted() { !partialReader.IsCompleted() {
if partialReader.BodySize() > 0 { if partialReader.BodySize() > 0 {
var r = ranges[0] var options = this.ReqServer.HTTPCachePolicy.Options
r2, findOk := partialReader.Ranges().FindRangeAtPosition(r.Start()) if options != nil {
if findOk && r2.Length() >= (256<<10) /* worth reading */ { fileStorage, isFileStorage := storage.(*caches.FileStorage)
isOk = true if isFileStorage && fileStorage.Options() != nil && fileStorage.Options().EnableIncompletePartialContent {
ranges[0] = [2]int64{r.Start(), partialReader.BodySize() - 1} // Content-Range: bytes 0-[CONTENT_LENGTH - 1]/CONTENT_LENGTH var r = ranges[0]
r2, findOk := partialReader.Ranges().FindRangeAtPosition(r.Start())
if findOk && r2.Length() >= (256<<10) /* worth reading */ {
isOk = true
ranges[0] = [2]int64{r.Start(), partialReader.BodySize() - 1} // Content-Range: bytes 0-[CONTENT_LENGTH - 1]/CONTENT_LENGTH
pReader.SetNextReader(NewHTTPRequestPartialReader(this, r2.End(), partialReader)) pReader.SetNextReader(NewHTTPRequestPartialReader(this, r2.End(), partialReader))
return pReader, ranges, r2.End() - 1 /* not include last byte */, true return pReader, ranges, r2.End() - 1 /* not include last byte */, true
}
}
} }
} }
return return