rclone/fs/cache/cache.go

41 lines
992 B
Go
Raw Normal View History

// Package cache implements the Fs cache
package cache
import (
"github.com/rclone/rclone/fs"
2019-08-01 05:19:07 +08:00
"github.com/rclone/rclone/lib/cache"
)
var (
2019-08-01 05:19:07 +08:00
c = cache.New()
)
2019-08-01 05:19:07 +08:00
// GetFn gets a fs.Fs named fsString either from the cache or creates
// it afresh with the create function
func GetFn(fsString string, create func(fsString string) (fs.Fs, error)) (f fs.Fs, err error) {
value, err := c.Get(fsString, func(fsString string) (f interface{}, ok bool, err error) {
f, err = create(fsString)
2019-08-01 05:19:07 +08:00
ok = err == nil || err == fs.ErrorIsFile
return f, ok, err
})
if err != nil && err != fs.ErrorIsFile {
2019-08-01 05:19:07 +08:00
return nil, err
}
return value.(fs.Fs), err
}
// Get gets a fs.Fs named fsString either from the cache or creates it afresh
func Get(fsString string) (f fs.Fs, err error) {
2019-08-01 05:19:07 +08:00
return GetFn(fsString, fs.NewFs)
}
// Put puts an fs.Fs named fsString into the cache
func Put(fsString string, f fs.Fs) {
2019-08-01 05:19:07 +08:00
c.Put(fsString, f)
}
// Clear removes everything from the cahce
func Clear() {
2019-08-01 05:19:07 +08:00
c.Clear()
}