-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
190 lines (175 loc) · 5.26 KB
/
fs.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
/*
Package cachefs implements a read-only cache around a fs.FS, using groupcache.
Using cachefs is straightforward:
// Setup groupcache (in this example with no peers)
groupcache.RegisterPeerPicker(func() groupcache.PeerPicker { return groupcache.NoPeers{} })
// Create the cached file system with group name "groupName", a 10MB cache, and a ten second expiration
cachedFileSystem := cachefs.New(os.DirFS("."), &cachefs.Config{GroupName: "groupName", SizeInBytes: 10*1024*1024, Duration: 10*time.Second})
// Use the file system as usual...
cachefs "wraps" the underlying file system with caching. You can specify groupcache parameters - the group name
and the cache size.
groupcache does not support expiration, but cachefs supports quantizing values so that expiration happens
around the expiration duration provided. Expiration can be disabled by specifying 0 for the duration.
See https://pkg.go.dev/github.com/golang/groupcache for more information on groupcache.
*/
package cachefs
import (
"bytes"
"context"
"encoding/gob"
"fmt"
"io"
"io/fs"
"net/url"
"strconv"
"time"
"github.com/golang/groupcache"
"github.com/google/uuid"
)
// Config stores the configuration settings of your cache.
type Config struct {
GroupName string // Name of the groupcache group
SizeInBytes int64 // Size of the cache
Duration time.Duration // Duration after which items can expire
NoStat bool // Don't do extra file Stat calls in ReadDir
}
// An cacheFS provides cached access to a hierarchical file system.
type cacheFS struct {
fs fs.FS
duration time.Duration
cache *groupcache.Group
}
// Open opens the named file.
//
// When Open returns an error, it should be of type *fs.PathError
// with the Op field set to "open", the Path field set to name,
// and the Err field describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// fs.ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
func (cfs *cacheFS) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
var (
buf groupcache.ByteView
q = make(url.Values, 2)
f file
)
t := quantize(time.Now(), cfs.duration, name)
q.Set("t", strconv.FormatInt(t, 10))
q.Set("path", name)
ctx := context.Background()
err := cfs.cache.Get(ctx, q.Encode(), groupcache.ByteViewSink(&buf))
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
rdr := countingReader{Reader: buf.Reader()}
decoder := gob.NewDecoder(&rdr)
err = decoder.Decode(&f)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
// rest of the slice is the file data
f.ReadSeeker = buf.SliceFrom(rdr.Count()).Reader()
return &f, nil
}
// New creates a new cached FS around innerFS using groupcache with the given
// configuration. The returned FS is read-only. If config is nil, it defaults
// to a 1MB cache using a random GUID as a name.
func New(innerFS fs.FS, config *Config) fs.FS {
if config == nil {
config = &Config{
GroupName: uuid.NewString(),
SizeInBytes: 1024 * 1024,
}
}
noStat := config.NoStat
return &cacheFS{
duration: config.Duration,
cache: groupcache.NewGroup(config.GroupName, config.SizeInBytes, groupcache.GetterFunc(
func(ctx context.Context, key string, dest groupcache.Sink) error {
// Parse query which contains quantize info and path
q, err := url.ParseQuery(key)
if err != nil {
return fmt.Errorf("invalid cache key: %w", err)
}
// Open file
f, err := innerFS.Open(q.Get("path"))
if err != nil {
return err
}
defer f.Close()
// Get file info
info, err := f.Stat()
if err != nil {
return err
}
// setup result data
resultFile := file{
FI: fileInfo{
Nm: info.Name(),
Sz: info.Size(),
Md: info.Mode(),
Mt: info.ModTime(),
},
}
var data []byte
if info.IsDir() {
// Read directory info
entries, err := f.(fs.ReadDirFile).ReadDir(-1)
if err != nil {
return err
}
resultFile.Dirs = make([]dirEntry, 0, len(entries))
for _, entry := range entries {
if !noStat {
fi, err := entry.Info()
if err != nil {
// Pretend it doesn't exist, like (*os.File).Readdir does.
continue
}
resultFile.Dirs = append(resultFile.Dirs, dirEntry{
FI: fileInfo{
Nm: fi.Name(),
Md: fi.Mode(),
Sz: fi.Size(),
Mt: fi.ModTime(),
},
})
} else {
resultFile.Dirs = append(resultFile.Dirs, dirEntry{
FI: fileInfo{
Nm: entry.Name(),
Md: entry.Type(),
},
})
}
}
} else {
// Read file
data, err = io.ReadAll(f)
if err != nil {
return err
}
}
// Encode the result
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
err = encoder.Encode(resultFile)
if err != nil {
return err
}
// Write data afterward to avoid extra copies of large stuff
n, err := buf.Write(data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("wrote incorrect number of bytes: %d of %d", n, len(data))
}
return dest.SetBytes(buf.Bytes())
})),
}
}