This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
cache_wrapper.go
197 lines (161 loc) · 4.3 KB
/
cache_wrapper.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
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
190
191
192
193
194
195
196
197
package storage
import (
"context"
"io"
"time"
)
type CacheOptions struct {
// MaxAge is the maximum time allowed since the underlying File's ModTime
// This means that if the cache is older than MaxAge, the Cache will fetch from the src again.
// If the expired File is still present on the src (i.e. not updated), it will be ignored.
MaxAge time.Duration
// DefaultExpired makes the cache treat a File as expired if its CreationTime cannot be checked.
// By default, it is false, which means the cache will treat zero-CreationTime files as valid.
// Only useful if MaxAge is set.
DefaultExpired bool
// NoData disables caching of the contents of the entries, it only stores the metadata.
NoData bool
}
// NewCacheWrapper creates an FS implementation which caches files opened from src into cache.
func NewCacheWrapper(src, cache FS, options *CacheOptions) FS {
if options == nil {
options = &CacheOptions{}
}
return &cacheWrapper{
src: src,
cache: cache,
options: options,
}
}
type openForwarder struct {
src func() (io.ReadCloser, error)
rc io.ReadCloser
}
func (f *openForwarder) open() error {
if f.rc != nil {
return nil
}
rc, err := f.src()
if err != nil {
return err
}
f.rc = rc
return nil
}
func (f *openForwarder) Read(p []byte) (n int, err error) {
if err = f.open(); err != nil {
return 0, err
}
return f.rc.Read(p)
}
func (f *openForwarder) Close() (err error) {
if f.rc == nil {
return nil
}
return f.rc.Close()
}
type cacheWrapper struct {
src FS
cache FS
options *CacheOptions
}
func (c *cacheWrapper) isExpired(file *File) bool {
if c.options.MaxAge == 0 {
// No expiration behavior
return false
}
creationTime := file.CreationTime
if creationTime.IsZero() {
creationTime = file.ModTime // Fallback to ModTime
}
if creationTime.IsZero() {
return c.options.DefaultExpired
}
return time.Since(creationTime) > c.options.MaxAge
}
func (c *cacheWrapper) openCache(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
f, err := c.cache.Open(ctx, path, options)
if err != nil {
return nil, err
}
if c.options.NoData {
// Override the ReadCloser to actually fetch from the src
// If Read is not called, it still allows to read the attributes
f.ReadCloser = &openForwarder{
src: func() (io.ReadCloser, error) {
return c.src.Open(ctx, path, options)
},
}
}
return f, nil
}
// Open implements FS.
func (c *cacheWrapper) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
f, err := c.openCache(ctx, path, options)
if err == nil {
if !c.isExpired(f) {
return f, nil
}
} else if !IsNotExist(err) {
return nil, err
}
sf, err := c.src.Open(ctx, path, options)
if err != nil {
return nil, err
}
defer sf.Close()
cacheAttrs := sf.Attributes
cacheAttrs.CreationTime = time.Now() // The cache requires the CreationTime, so the original value is overwritten
wc, err := c.cache.Create(ctx, path, &WriterOptions{
Attributes: cacheAttrs,
})
if err != nil {
return nil, err
}
if !c.options.NoData {
if _, err := io.Copy(wc, sf); err != nil {
wc.Close()
return nil, err
}
}
if err := wc.Close(); err != nil {
return nil, err
}
ff, err := c.openCache(ctx, path, options)
if err != nil {
return nil, err
}
return ff, nil
}
// Attributes implements FS.
func (c *cacheWrapper) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error) {
f, err := c.Open(ctx, path, options)
if err != nil {
return nil, err
}
return &f.Attributes, nil
}
// Delete implements FS.
func (c *cacheWrapper) Delete(ctx context.Context, path string) error {
err := c.cache.Delete(ctx, path)
if err != nil && !IsNotExist(err) {
return err
}
return c.src.Delete(ctx, path)
}
// Create implements FS.
func (c *cacheWrapper) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
err := c.cache.Delete(ctx, path)
if err != nil && !IsNotExist(err) {
return nil, err
}
return c.src.Create(ctx, path, options)
}
// Walk implements FS.
func (c *cacheWrapper) Walk(ctx context.Context, path string, fn WalkFn) error {
return c.src.Walk(ctx, path, fn)
}
func (c *cacheWrapper) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
// Pass-through
return c.src.URL(ctx, path, options)
}