Skip to content

Commit 83ce20f

Browse files
committed
cachefs: Port to use mailgun/groupcache which supports expiration
1 parent 3895606 commit 83ce20f

7 files changed

Lines changed: 32 additions & 50 deletions

File tree

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cachefs
22

3-
Package `cachefs` implements a read-only cache around a `fs.FS`, using `groupcache`.
3+
Package `cachefs` implements a read-only cache around a `fs.FS`, using mailgun's `groupcache`.
44

55
Using `cachefs` is straightforward:
66

@@ -15,7 +15,4 @@ Using `cachefs` is straightforward:
1515
`cachefs` "wraps" the underlying file system with caching. You can specify groupcache parameters - the group name
1616
and the cache size.
1717

18-
`groupcache` does not support expiration, but `cachefs` supports quantizing values so that expiration happens
19-
around the expiration duration provided. Expiration can be disabled by specifying 0 for the duration.
20-
21-
See https://pkg.go.dev/github.com/golang/groupcache for more information on `groupcache`.
18+
See https://pkg.go.dev/github.com/mailgun/groupcache for more information on `groupcache`.

fs.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and the cache size.
1717
groupcache does not support expiration, but cachefs supports quantizing values so that expiration happens
1818
around the expiration duration provided. Expiration can be disabled by specifying 0 for the duration.
1919
20-
See https://pkg.go.dev/github.com/golang/groupcache for more information on groupcache.
20+
See https://pkg.go.dev/github.com/mailgun/groupcache for more information on groupcache.
2121
*/
2222
package cachefs
2323

@@ -28,12 +28,10 @@ import (
2828
"fmt"
2929
"io"
3030
"io/fs"
31-
"net/url"
32-
"strconv"
3331
"time"
3432

35-
"github.com/golang/groupcache"
3633
"github.com/google/uuid"
34+
"github.com/mailgun/groupcache/v2"
3735
)
3836

3937
// Config stores the configuration settings of your cache.
@@ -67,14 +65,10 @@ func (cfs *cacheFS) Open(name string) (fs.File, error) {
6765

6866
var (
6967
buf groupcache.ByteView
70-
q = make(url.Values, 2)
7168
f file
7269
)
73-
t := quantize(time.Now(), cfs.duration, name)
74-
q.Set("t", strconv.FormatInt(t, 10))
75-
q.Set("path", name)
7670
ctx := context.Background()
77-
err := cfs.cache.Get(ctx, q.Encode(), groupcache.ByteViewSink(&buf))
71+
err := cfs.cache.Get(ctx, name, groupcache.ByteViewSink(&buf))
7872
if err != nil {
7973
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
8074
}
@@ -105,13 +99,7 @@ func New(innerFS fs.FS, config *Config) fs.FS {
10599
duration: config.Duration,
106100
cache: groupcache.NewGroup(config.GroupName, config.SizeInBytes, groupcache.GetterFunc(
107101
func(ctx context.Context, key string, dest groupcache.Sink) error {
108-
// Parse query which contains quantize info and path
109-
q, err := url.ParseQuery(key)
110-
if err != nil {
111-
return fmt.Errorf("invalid cache key: %w", err)
112-
}
113-
// Open file
114-
f, err := innerFS.Open(q.Get("path"))
102+
f, err := innerFS.Open(key)
115103
if err != nil {
116104
return err
117105
}
@@ -184,7 +172,7 @@ func New(innerFS fs.FS, config *Config) fs.FS {
184172
if n != len(data) {
185173
return fmt.Errorf("wrote incorrect number of bytes: %d of %d", n, len(data))
186174
}
187-
return dest.SetBytes(buf.Bytes())
175+
return dest.SetBytes(buf.Bytes(), time.Now().Add(config.Duration))
188176
})),
189177
}
190178
}

fs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestFS(t *testing.T) {
2323
fs.WalkDir(fileSys, ".", func(path string, d fs.DirEntry, err error) error {
2424
if err != nil {
2525
t.Error(err)
26+
return err
2627
}
2728
if path == "" {
2829
t.Error("Path is empty")

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ module github.com/ancientlore/cachefs
33
go 1.17
44

55
require (
6-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
76
github.com/google/uuid v1.3.0
7+
github.com/mailgun/groupcache v1.3.0
8+
github.com/mailgun/groupcache/v2 v2.4.1
89
)
910

1011
require (
1112
github.com/golang/protobuf v1.5.2 // indirect
13+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
14+
github.com/segmentio/fasthash v1.0.3 // indirect
15+
github.com/sirupsen/logrus v1.6.0 // indirect
16+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
1217
google.golang.org/protobuf v1.26.0 // indirect
1318
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
23
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
4+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
35
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
46
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
57
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
68
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
79
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
810
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
911
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
12+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
13+
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
14+
github.com/mailgun/groupcache v1.3.0 h1:qie8iED3OIo2ICCbYx9vZybsVUGBJdZkroidGfao7q4=
15+
github.com/mailgun/groupcache v1.3.0/go.mod h1:IC2jAVGyQ4t9S8D1Hsul0zMWkMDuVR8N/Cex7bgCvNg=
16+
github.com/mailgun/groupcache/v2 v2.4.1 h1:E2WWpvUTfuBJINX698P1YBWtw8Y+0iHFAAkaYNORYkA=
17+
github.com/mailgun/groupcache/v2 v2.4.1/go.mod h1:L+wDfh8BrXRyYH1VHBtjv/UxYhjIFYvnk9kmpF8/ij4=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
20+
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
21+
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
22+
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
23+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
24+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
25+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
26+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1027
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
1128
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1229
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

package_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"log"
55
"testing"
66

7-
"github.com/golang/groupcache"
7+
"github.com/mailgun/groupcache/v2"
88
)
99

1010
func TestMain(t *testing.M) {

quantize.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)