MCPcopy Index your code
hub / github.com/GoEdgeLab/EdgeNode / PrepareCache

Method PrepareCache

internal/nodes/http_writer.go:158–509  ·  view source on GitHub ↗

PrepareCache 准备缓存

(resp *http.Response, size int64)

Source from the content-addressed store, hash-verified

156
157// PrepareCache 准备缓存
158func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
159 if resp == nil {
160 return
161 }
162
163 var cachePolicy = this.req.ReqServer.HTTPCachePolicy
164 if cachePolicy == nil || !cachePolicy.IsOn {
165 return
166 }
167
168 var cacheRef = this.req.cacheRef
169 if cacheRef == nil || !cacheRef.IsOn {
170 return
171 }
172
173 var addStatusHeader = this.req.web != nil && this.req.web.Cache != nil && this.req.web.Cache.AddStatusHeader
174
175 // 不支持Range
176 if this.isPartial {
177 if !cacheRef.AllowPartialContent {
178 this.req.varMapping["cache.status"] = "BYPASS"
179 if addStatusHeader {
180 this.Header().Set("X-Cache", "BYPASS, not supported partial content")
181 }
182 return
183 }
184 if this.cacheStorage.Policy().Type != serverconfigs.CachePolicyStorageFile {
185 this.req.varMapping["cache.status"] = "BYPASS"
186 if addStatusHeader {
187 this.Header().Set("X-Cache", "BYPASS, not supported partial content in memory storage")
188 }
189 return
190 }
191 }
192
193 // 如果允许 ChunkedEncoding,就无需尺寸的判断,因为此时的 size 为 -1
194 if !cacheRef.AllowChunkedEncoding && size < 0 {
195 this.req.varMapping["cache.status"] = "BYPASS"
196 if addStatusHeader {
197 this.Header().Set("X-Cache", "BYPASS, ChunkedEncoding")
198 }
199 return
200 }
201
202 var contentSize = size
203 if this.isPartial {
204 // 从Content-Range中读取内容总长度
205 var contentRange = this.Header().Get("Content-Range")
206 _, totalSize := httpRequestParseContentRangeHeader(contentRange)
207 if totalSize > 0 {
208 contentSize = totalSize
209 }
210 }
211 if contentSize >= 0 && ((cacheRef.MaxSizeBytes() > 0 && contentSize > cacheRef.MaxSizeBytes()) ||
212 (cachePolicy.MaxSizeBytes() > 0 && contentSize > cachePolicy.MaxSizeBytes()) || (cacheRef.MinSizeBytes() > contentSize)) {
213 this.req.varMapping["cache.status"] = "BYPASS"
214 if addStatusHeader {
215 this.Header().Set("X-Cache", "BYPASS, Content-Length")

Callers 1

PrepareMethod · 0.95

Calls 15

HeaderMethod · 0.95
StatusCodeMethod · 0.95
GetHeaderMethod · 0.95
SetHeaderMethod · 0.95
calculateHeaderLengthMethod · 0.95
shouldIgnoreHeaderMethod · 0.95
NowFunction · 0.92
CanIgnoreErrFunction · 0.92
ErrorFunction · 0.92
NewFilterReaderCloserFunction · 0.92
NewTeeReaderCloserFunction · 0.92

Tested by

no test coverage detected