Skip to content

cachefs: Add InvalidateCacheFS interface and extend cacheFS to implement it #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cachefs

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

Using `cachefs` is straightforward:

Expand All @@ -15,7 +15,4 @@ Using `cachefs` is straightforward:
`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`.
See https://pkg.go.dev/github.com/mailgun/groupcache for more information on `groupcache`.
38 changes: 21 additions & 17 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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.
See https://pkg.go.dev/github.com/mailgun/groupcache for more information on groupcache.
*/
package cachefs

Expand All @@ -28,14 +28,20 @@ import (
"fmt"
"io"
"io/fs"
"net/url"
"strconv"
"time"

"github.com/golang/groupcache"
"github.com/google/uuid"
"github.com/mailgun/groupcache/v2"
)

// A InvalidateCacheFS is a file system with a InvalidateCache method.
type InvalidateCacheFS interface {
fs.FS

// InvalidateCache invalidates the cache for a path on the FS.
InvalidateCache(ctx context.Context, path string) error
}

// Config stores the configuration settings of your cache.
type Config struct {
GroupName string // Name of the groupcache group
Expand All @@ -51,6 +57,9 @@ type cacheFS struct {
cache *groupcache.Group
}

// Statically declare that cacheFS satisfies InvalidateCacheFS.
var _ InvalidateCacheFS = (*cacheFS)(nil)

// Open opens the named file.
//
// When Open returns an error, it should be of type *fs.PathError
Expand All @@ -67,14 +76,10 @@ func (cfs *cacheFS) Open(name string) (fs.File, error) {

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))
err := cfs.cache.Get(ctx, name, groupcache.ByteViewSink(&buf))
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
Expand Down Expand Up @@ -105,13 +110,7 @@ func New(innerFS fs.FS, config *Config) fs.FS {
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"))
f, err := innerFS.Open(key)
if err != nil {
return err
}
Expand Down Expand Up @@ -184,7 +183,12 @@ func New(innerFS fs.FS, config *Config) fs.FS {
if n != len(data) {
return fmt.Errorf("wrote incorrect number of bytes: %d of %d", n, len(data))
}
return dest.SetBytes(buf.Bytes())
return dest.SetBytes(buf.Bytes(), time.Now().Add(config.Duration))
})),
}
}

// InvalidateCache invalidates the cache for a path in the filesystem.
func (cfs *cacheFS) InvalidateCache(ctx context.Context, path string) error {
return cfs.cache.Remove(ctx, path)
}
1 change: 1 addition & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestFS(t *testing.T) {
fs.WalkDir(fileSys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
t.Error(err)
return err
}
if path == "" {
t.Error("Path is empty")
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ module github.com/ancientlore/cachefs
go 1.17

require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/uuid v1.3.0
github.com/mailgun/groupcache v1.3.0
github.com/mailgun/groupcache/v2 v2.4.1
)

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mailgun/groupcache v1.3.0 h1:qie8iED3OIo2ICCbYx9vZybsVUGBJdZkroidGfao7q4=
github.com/mailgun/groupcache v1.3.0/go.mod h1:IC2jAVGyQ4t9S8D1Hsul0zMWkMDuVR8N/Cex7bgCvNg=
github.com/mailgun/groupcache/v2 v2.4.1 h1:E2WWpvUTfuBJINX698P1YBWtw8Y+0iHFAAkaYNORYkA=
github.com/mailgun/groupcache/v2 v2.4.1/go.mod h1:L+wDfh8BrXRyYH1VHBtjv/UxYhjIFYvnk9kmpF8/ij4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
2 changes: 1 addition & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"testing"

"github.com/golang/groupcache"
"github.com/mailgun/groupcache/v2"
)

func TestMain(t *testing.M) {
Expand Down
26 changes: 0 additions & 26 deletions quantize.go

This file was deleted.