读取缓存
(useStale bool)
| 21 | |
| 22 | // 读取缓存 |
| 23 | func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { |
| 24 | // 需要动态Upgrade的不缓存 |
| 25 | if len(this.RawReq.Header.Get("Upgrade")) > 0 { |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | this.cacheCanTryStale = false |
| 30 | |
| 31 | var cachePolicy = this.ReqServer.HTTPCachePolicy |
| 32 | if cachePolicy == nil || !cachePolicy.IsOn { |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | if this.web.Cache == nil || !this.web.Cache.IsOn || (len(cachePolicy.CacheRefs) == 0 && len(this.web.Cache.CacheRefs) == 0) { |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | // 添加 X-Cache Header |
| 41 | var addStatusHeader = this.web.Cache.AddStatusHeader |
| 42 | var cacheBypassDescription = "" |
| 43 | if addStatusHeader { |
| 44 | defer func() { |
| 45 | if len(cacheBypassDescription) > 0 { |
| 46 | this.writer.Header().Set("X-Cache", cacheBypassDescription) |
| 47 | return |
| 48 | } |
| 49 | var cacheStatus = this.varMapping["cache.status"] |
| 50 | if cacheStatus != "HIT" { |
| 51 | this.writer.Header().Set("X-Cache", cacheStatus) |
| 52 | } |
| 53 | }() |
| 54 | } |
| 55 | |
| 56 | // 检查服务独立的缓存条件 |
| 57 | var refType = "" |
| 58 | for _, cacheRef := range this.web.Cache.CacheRefs { |
| 59 | if !cacheRef.IsOn { |
| 60 | continue |
| 61 | } |
| 62 | if (cacheRef.Conds != nil && cacheRef.Conds.HasRequestConds() && cacheRef.Conds.MatchRequest(this.Format)) || |
| 63 | (cacheRef.SimpleCond != nil && cacheRef.SimpleCond.Match(this.Format)) { |
| 64 | if cacheRef.IsReverse { |
| 65 | return |
| 66 | } |
| 67 | this.cacheRef = cacheRef |
| 68 | refType = "server" |
| 69 | break |
| 70 | } |
| 71 | } |
| 72 | if this.cacheRef == nil && !this.web.Cache.DisablePolicyRefs { |
| 73 | // 检查策略默认的缓存条件 |
| 74 | for _, cacheRef := range cachePolicy.CacheRefs { |
| 75 | if !cacheRef.IsOn { |
| 76 | continue |
| 77 | } |
| 78 | if (cacheRef.Conds != nil && cacheRef.Conds.HasRequestConds() && cacheRef.Conds.MatchRequest(this.Format)) || |
| 79 | (cacheRef.SimpleCond != nil && cacheRef.SimpleCond.Match(this.Format)) { |
| 80 | if cacheRef.IsReverse { |
no test coverage detected