Repository navigation
Expand file tree
/
Copy pathdiskcache.go
More file actions
189 lines (171 loc) · 5.09 KB
/
Copy pathdiskcache.go
File metadata and controls
189 lines (171 loc) · 5.09 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
const allClicksTTL = time.Hour
type issueEntry struct {
Clicks map[string]int `json:"c"`
Saved time.Time `json:"s"`
}
type analyticsEntry struct {
A analytics `json:"a"`
Saved time.Time `json:"s"`
}
type diskData struct {
AllClicks map[string]int `json:"ac,omitempty"`
AllSaved time.Time `json:"as,omitempty"`
// AllTotal is the number of click events AllClicks was built from, as
// reported by the events collection, and AllNewest is the timestamp of the
// most recent event counted. Together they let a refresh fetch only what
// arrived since, then check the result adds up to what the API reports.
AllTotal int `json:"at,omitempty"`
AllNewest time.Time `json:"an_newest,omitempty"`
IssueClicks map[string]issueEntry `json:"ic,omitempty"`
Analytics map[string]analyticsEntry `json:"an,omitempty"`
}
type diskCache struct {
mu sync.Mutex
path string
d diskData
}
func newDiskCache(dir string) *diskCache {
dc := &diskCache{
path: filepath.Join(dir, "cache.json"),
d: diskData{
AllClicks: map[string]int{},
IssueClicks: map[string]issueEntry{},
Analytics: map[string]analyticsEntry{},
},
}
if err := dc.load(); err != nil {
fmt.Fprintf(os.Stderr, "disk cache: %v\n", err)
}
return dc
}
func (dc *diskCache) load() error {
f, err := os.Open(dc.path)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
defer f.Close()
var d diskData
if err := json.NewDecoder(f).Decode(&d); err != nil {
return fmt.Errorf("corrupt cache, starting fresh: %w", err)
}
if d.AllClicks == nil {
d.AllClicks = map[string]int{}
}
if d.IssueClicks == nil {
d.IssueClicks = map[string]issueEntry{}
}
if d.Analytics == nil {
d.Analytics = map[string]analyticsEntry{}
}
dc.d = d
return nil
}
// persistLocked writes to disk. Caller must hold dc.mu.
func (dc *diskCache) persistLocked() {
if err := os.MkdirAll(filepath.Dir(dc.path), 0700); err != nil {
return
}
tmp := dc.path + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return
}
if err := json.NewEncoder(f).Encode(dc.d); err != nil {
f.Close()
os.Remove(tmp)
return
}
f.Close()
os.Rename(tmp, dc.path)
}
func (dc *diskCache) allClicksFresh() bool {
dc.mu.Lock()
defer dc.mu.Unlock()
return len(dc.d.AllClicks) > 0 && time.Since(dc.d.AllSaved) < allClicksTTL
}
func (dc *diskCache) getAllClicks() map[string]int {
dc.mu.Lock()
defer dc.mu.Unlock()
return dc.d.AllClicks
}
func (dc *diskCache) setAllClicks(counts map[string]int, total int, newest time.Time) {
dc.mu.Lock()
defer dc.mu.Unlock()
dc.d.AllClicks = counts
dc.d.AllTotal = total
dc.d.AllNewest = newest
dc.d.AllSaved = time.Now()
dc.persistLocked()
}
// allClicksState returns the cached clicks together with the high-water mark
// and event total they were built from. ok is false when there is nothing to
// sync against, including cache files written before the mark was recorded, so
// those fall back to one full walk and pick the mark up from there.
func (dc *diskCache) allClicksState() (counts map[string]int, newest time.Time, total int, ok bool) {
dc.mu.Lock()
defer dc.mu.Unlock()
if len(dc.d.AllClicks) == 0 || dc.d.AllNewest.IsZero() {
return nil, time.Time{}, 0, false
}
return dc.d.AllClicks, dc.d.AllNewest, dc.d.AllTotal, true
}
// markAllClicksVerified records that the cached clicks were just confirmed
// current. Freshness here means "checked against the source", not "recently
// downloaded", so a confirmed-unchanged cache resets the TTL without refetching.
func (dc *diskCache) markAllClicksVerified() {
dc.mu.Lock()
defer dc.mu.Unlock()
dc.d.AllSaved = time.Now()
dc.persistLocked()
}
// getIssueClicks takes its TTL from the caller for the same reason
// getAnalytics does: a just-sent issue is still accumulating clicks, while an
// old one has stopped. See engagementTTLFor.
func (dc *diskCache) getIssueClicks(emailID string, ttl time.Duration) (map[string]int, bool) {
dc.mu.Lock()
defer dc.mu.Unlock()
e, ok := dc.d.IssueClicks[emailID]
if !ok || time.Since(e.Saved) > ttl {
return nil, false
}
return e.Clicks, true
}
func (dc *diskCache) setIssueClicks(emailID string, counts map[string]int) {
dc.mu.Lock()
defer dc.mu.Unlock()
dc.d.IssueClicks[emailID] = issueEntry{Clicks: counts, Saved: time.Now()}
dc.persistLocked()
}
// getAnalytics takes the TTL from the caller rather than using a fixed one: how
// long an issue's analytics stay valid depends on how long ago it was sent, and
// the disk cache doesn't know publish dates. See analyticsTTLFor.
func (dc *diskCache) getAnalytics(emailID string, ttl time.Duration) (analytics, bool) {
dc.mu.Lock()
defer dc.mu.Unlock()
e, ok := dc.d.Analytics[emailID]
if !ok || time.Since(e.Saved) > ttl {
return analytics{}, false
}
return e.A, true
}
func (dc *diskCache) setAnalytics(emailID string, a analytics) {
dc.mu.Lock()
defer dc.mu.Unlock()
if dc.d.Analytics == nil {
dc.d.Analytics = map[string]analyticsEntry{}
}
dc.d.Analytics[emailID] = analyticsEntry{A: a, Saved: time.Now()}
dc.persistLocked()
}