结束缓存相关处理
()
| 1186 | |
| 1187 | // 结束缓存相关处理 |
| 1188 | func (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 | } |
no test coverage detected