github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/cache/cache.go (about)

     1  // Package cache implements the Fs cache
     2  package cache
     3  
     4  import (
     5  	"github.com/rclone/rclone/fs"
     6  	"github.com/rclone/rclone/lib/cache"
     7  )
     8  
     9  var (
    10  	c = cache.New()
    11  )
    12  
    13  // GetFn gets a fs.Fs named fsString either from the cache or creates
    14  // it afresh with the create function
    15  func GetFn(fsString string, create func(fsString string) (fs.Fs, error)) (f fs.Fs, err error) {
    16  	value, err := c.Get(fsString, func(fsString string) (f interface{}, ok bool, err error) {
    17  		f, err = create(fsString)
    18  		ok = err == nil || err == fs.ErrorIsFile
    19  		return f, ok, err
    20  	})
    21  	if err != nil && err != fs.ErrorIsFile {
    22  		return nil, err
    23  	}
    24  	return value.(fs.Fs), err
    25  }
    26  
    27  // Get gets a fs.Fs named fsString either from the cache or creates it afresh
    28  func Get(fsString string) (f fs.Fs, err error) {
    29  	return GetFn(fsString, fs.NewFs)
    30  }
    31  
    32  // Put puts an fs.Fs named fsString into the cache
    33  func Put(fsString string, f fs.Fs) {
    34  	c.Put(fsString, f)
    35  }
    36  
    37  // Clear removes everything from the cahce
    38  func Clear() {
    39  	c.Clear()
    40  }