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

Method finishCache

internal/nodes/http_writer.go:1188–1246  ·  view source on GitHub ↗

结束缓存相关处理

()

Source from the content-addressed store, hash-verified

1186
1187// 结束缓存相关处理
1188func (this *HTTPWriter) finishCache() {
1189 // 缓存
1190 if this.cacheWriter != nil {
1191 if this.isOk && this.cacheIsFinished {
1192 // 对比缓存前后的Content-Length
1193 var method = this.req.Method()
1194 if method != http.MethodHead && this.StatusCode() != http.StatusNoContent && !this.isPartial {
1195 var contentLengthString = this.GetHeader("Content-Length")
1196 if len(contentLengthString) > 0 {
1197 var contentLength = types.Int64(contentLengthString)
1198 if contentLength != this.cacheWriter.BodySize() {
1199 this.isOk = false
1200 _ = this.cacheWriter.Discard()
1201 this.cacheWriter = nil
1202 }
1203 }
1204 }
1205
1206 if this.isOk && this.cacheWriter != nil {
1207 err := this.cacheWriter.Close()
1208 if err == nil {
1209 if !this.isPartial || this.partialFileIsNew {
1210 var expiredAt = this.cacheWriter.ExpiredAt()
1211 this.cacheStorage.AddToList(&caches.Item{
1212 Type: this.cacheWriter.ItemType(),
1213 Key: this.cacheWriter.Key(),
1214 ExpiresAt: expiredAt,
1215 StaleAt: expiredAt + int64(this.calculateStaleLife()),
1216 HeaderSize: this.cacheWriter.HeaderSize(),
1217 BodySize: this.cacheWriter.BodySize(),
1218 Host: this.req.ReqHost,
1219 ServerId: this.req.ReqServer.Id,
1220 })
1221 }
1222 }
1223 }
1224 } else {
1225 if !this.isPartial || !this.cacheIsFinished {
1226 _ = this.cacheWriter.Discard()
1227 } else {
1228 // Partial的文件内容不删除
1229 err := this.cacheWriter.Close()
1230 if err == nil && this.partialFileIsNew {
1231 var expiredAt = this.cacheWriter.ExpiredAt()
1232 this.cacheStorage.AddToList(&caches.Item{
1233 Type: this.cacheWriter.ItemType(),
1234 Key: this.cacheWriter.Key(),
1235 ExpiresAt: expiredAt,
1236 StaleAt: expiredAt + int64(this.calculateStaleLife()),
1237 HeaderSize: this.cacheWriter.HeaderSize(),
1238 BodySize: this.cacheWriter.BodySize(),
1239 Host: this.req.ReqHost,
1240 ServerId: this.req.ReqServer.Id,
1241 })
1242 }
1243 }
1244 }
1245 }

Callers 1

CloseMethod · 0.95

Calls 12

StatusCodeMethod · 0.95
GetHeaderMethod · 0.95
calculateStaleLifeMethod · 0.95
MethodMethod · 0.80
BodySizeMethod · 0.65
DiscardMethod · 0.65
CloseMethod · 0.65
ExpiredAtMethod · 0.65
AddToListMethod · 0.65
ItemTypeMethod · 0.65
KeyMethod · 0.65
HeaderSizeMethod · 0.65

Tested by

no test coverage detected