-
Notifications
You must be signed in to change notification settings - Fork 5
/
caching_result.go
151 lines (117 loc) · 3.39 KB
/
caching_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gbox
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/eko/gocache/v2/store"
"github.com/pquerna/cachecontrol/cacheobject"
)
const (
CachingQueryResultStale cachingQueryResultStatus = "STALE"
CachingQueryResultValid cachingQueryResultStatus = "VALID"
)
type cachingQueryResultStatus string
type cachingQueryResult struct {
Header http.Header
Body json.RawMessage
HitTime uint64
CreatedAt time.Time
Expiration time.Duration
MaxAge caddy.Duration
Swr caddy.Duration
Tags cachingTags
plan *cachingPlan
}
func (c *Caching) getCachingQueryResult(ctx context.Context, plan *cachingPlan) (*cachingQueryResult, error) {
result := &cachingQueryResult{
plan: plan,
}
if _, err := c.store.Get(ctx, plan.queryResultCacheKey, result); err != nil {
return nil, err
}
return result, nil
}
func (c *Caching) cachingQueryResult(ctx context.Context, request *cachingRequest, plan *cachingPlan, body []byte, header http.Header) (err error) {
tags := make(cachingTags)
tagAnalyzer := newCachingTagAnalyzer(request, c.TypeKeys)
if err = tagAnalyzer.AnalyzeResult(body, plan.Types, tags); err != nil {
return err
}
result := &cachingQueryResult{
Body: body,
Header: header,
CreatedAt: time.Now(),
MaxAge: plan.MaxAge,
Swr: plan.Swr,
Tags: tags,
Expiration: time.Duration(plan.MaxAge) + time.Duration(plan.Swr),
}
result.normalizeHeader()
return c.store.Set(ctx, plan.queryResultCacheKey, result, &store.Options{
Tags: tags.ToSlice(),
Expiration: result.Expiration,
})
}
func (c *Caching) increaseQueryResultHitTimes(ctx context.Context, r *cachingQueryResult) error {
r.HitTime++
return c.store.Set(ctx, r.plan.queryResultCacheKey, r, &store.Options{
Expiration: r.Expiration - time.Since(r.CreatedAt),
})
}
func (r *cachingQueryResult) Status() cachingQueryResultStatus {
if time.Duration(r.MaxAge) >= r.Age() {
return CachingQueryResultValid
}
return CachingQueryResultStale
}
// ValidFor check caching result still valid with cache control directives
// https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.1
func (r *cachingQueryResult) ValidFor(cc *cacheobject.RequestCacheDirectives) bool {
status := r.Status()
age := r.Age()
if cc.NoCache && status == CachingQueryResultStale {
return false
}
if cc.MinFresh != -1 {
maxAge := time.Duration(r.MaxAge)
d := age + time.Duration(cc.MinFresh)*time.Second
if d > maxAge {
return false
}
}
// max-age request
if cc.MaxAge != -1 {
d := time.Duration(cc.MaxAge) * time.Second
if d >= age && status == CachingQueryResultValid {
return true
}
// max-age with max-stale
if (cc.MaxStaleSet || cc.MaxStale != -1) && status == CachingQueryResultStale {
// client is willing to accept a stale response of any age.
if cc.MaxStale == -1 {
return true
}
d += time.Duration(cc.MaxStale) * time.Second
return d >= age
}
return false
}
// max-stale only
if cc.MaxStaleSet || cc.MaxStale != -1 {
if cc.MaxStale == -1 || status == CachingQueryResultValid {
return true
}
d := time.Duration(r.MaxAge) + time.Duration(cc.MaxStale)*time.Second
return d >= age
}
return true
}
func (r *cachingQueryResult) Age() time.Duration {
return time.Since(r.CreatedAt)
}
func (r *cachingQueryResult) normalizeHeader() {
r.Header.Del("date")
r.Header.Del("server")
}