@@ -17,7 +17,7 @@ and the cache size.
1717groupcache does not support expiration, but cachefs supports quantizing values so that expiration happens
1818around 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*/
2222package 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}
0 commit comments