2019-05-23 13:26:16 +02:00
|
|
|
// Package cache implements the Fs cache
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-07-31 23:19:07 +02:00
|
|
|
"github.com/rclone/rclone/lib/cache"
|
2019-05-23 13:26:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-07-31 23:19:07 +02:00
|
|
|
c = cache.New()
|
2019-05-23 13:26:16 +02:00
|
|
|
)
|
|
|
|
|
2019-07-31 23:19:07 +02: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) (value interface{}, ok bool, error error) {
|
|
|
|
f, err := create(fsString)
|
|
|
|
ok = err == nil || err == fs.ErrorIsFile
|
|
|
|
return f, ok, err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return value.(fs.Fs), nil
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-31 23:19:07 +02:00
|
|
|
return GetFn(fsString, fs.NewFs)
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts an fs.Fs named fsString into the cache
|
|
|
|
func Put(fsString string, f fs.Fs) {
|
2019-07-31 23:19:07 +02:00
|
|
|
c.Put(fsString, f)
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes everything from the cahce
|
|
|
|
func Clear() {
|
2019-07-31 23:19:07 +02:00
|
|
|
c.Clear()
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|