如果缓存条件支持206 Partial Content,则第一次加载时自动尝试从分片缓存中读取内容

This commit is contained in:
刘祥超
2022-06-18 19:31:10 +08:00
parent 4c4033bb56
commit b1d0c8852e
2 changed files with 8 additions and 2 deletions

View File

@@ -236,6 +236,11 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
if reader == nil { if reader == nil {
reader, err = storage.OpenReader(key, useStale, false) reader, err = storage.OpenReader(key, useStale, false)
if err != nil && this.cacheRef.AllowPartialContent { if err != nil && this.cacheRef.AllowPartialContent {
// 尝试读取分片的缓存内容
if len(rangeHeader) == 0 {
// 默认读取开头
rangeHeader = "bytes=0-"
}
pReader, ranges := this.tryPartialReader(storage, key, useStale, rangeHeader) pReader, ranges := this.tryPartialReader(storage, key, useStale, rangeHeader)
if pReader != nil { if pReader != nil {
isPartialCache = true isPartialCache = true

View File

@@ -312,9 +312,10 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
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())
this.Header().Set("X-Cache", "BYPASS, write cache failed")
} else {
this.Header().Set("X-Cache", "BYPASS, "+err.Error())
} }
this.Header().Set("X-Cache", "BYPASS, too many requests")
return return
} }
this.cacheWriter = cacheWriter this.cacheWriter = cacheWriter