mirror of
https://github.com/rclone/rclone.git
synced 2025-06-30 12:30:56 +02:00
fs/cache: add --fs-cache-expire-duration to control the fs cache
This commit makes the previously statically configured fs cache configurable. It introduces two parameters `--fs-cache-expire-duration` and `--fs-cache-expire-interval` to control the caching of the items. It also adds new interfaces to lib/cache to set these.
This commit is contained in:
31
lib/cache/cache.go
vendored
31
lib/cache/cache.go
vendored
@ -28,6 +28,30 @@ func New() *Cache {
|
||||
}
|
||||
}
|
||||
|
||||
// SetExpireDuration sets the interval at which things expire
|
||||
//
|
||||
// If it is less than or equal to 0 then things are never cached
|
||||
func (c *Cache) SetExpireDuration(d time.Duration) *Cache {
|
||||
c.expireDuration = d
|
||||
return c
|
||||
}
|
||||
|
||||
// returns true if we aren't to cache anything
|
||||
func (c *Cache) noCache() bool {
|
||||
return c.expireDuration <= 0
|
||||
}
|
||||
|
||||
// SetExpireInterval sets the interval at which the cache expiry runs
|
||||
//
|
||||
// Set to 0 or a -ve number to disable
|
||||
func (c *Cache) SetExpireInterval(d time.Duration) *Cache {
|
||||
if d <= 0 {
|
||||
d = 100 * 365 * 24 * time.Hour
|
||||
}
|
||||
c.expireInterval = d
|
||||
return c
|
||||
}
|
||||
|
||||
// cacheEntry is stored in the cache
|
||||
type cacheEntry struct {
|
||||
value interface{} // cached item
|
||||
@ -69,7 +93,9 @@ func (c *Cache) Get(key string, create CreateFunc) (value interface{}, err error
|
||||
err: err,
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.cache[key] = entry
|
||||
if !c.noCache() {
|
||||
c.cache[key] = entry
|
||||
}
|
||||
}
|
||||
defer c.mu.Unlock()
|
||||
c.used(entry)
|
||||
@ -100,6 +126,9 @@ func (c *Cache) Unpin(key string) {
|
||||
func (c *Cache) Put(key string, value interface{}) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.noCache() {
|
||||
return
|
||||
}
|
||||
entry := &cacheEntry{
|
||||
value: value,
|
||||
key: key,
|
||||
|
27
lib/cache/cache_test.go
vendored
27
lib/cache/cache_test.go
vendored
@ -100,7 +100,7 @@ func TestPut(t *testing.T) {
|
||||
func TestCacheExpire(t *testing.T) {
|
||||
c, create := setup(t)
|
||||
|
||||
c.expireInterval = time.Millisecond
|
||||
c.SetExpireInterval(time.Millisecond)
|
||||
assert.Equal(t, false, c.expireRunning)
|
||||
|
||||
_, err := c.Get("/", create)
|
||||
@ -127,6 +127,31 @@ func TestCacheExpire(t *testing.T) {
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func TestCacheNoExpire(t *testing.T) {
|
||||
c, create := setup(t)
|
||||
|
||||
assert.False(t, c.noCache())
|
||||
|
||||
c.SetExpireDuration(0)
|
||||
assert.Equal(t, false, c.expireRunning)
|
||||
|
||||
assert.True(t, c.noCache())
|
||||
|
||||
f, err := c.Get("/", create)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, f)
|
||||
|
||||
c.mu.Lock()
|
||||
assert.Equal(t, 0, len(c.cache))
|
||||
c.mu.Unlock()
|
||||
|
||||
c.Put("/alien", "slime")
|
||||
|
||||
c.mu.Lock()
|
||||
assert.Equal(t, 0, len(c.cache))
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func TestCachePin(t *testing.T) {
|
||||
c, create := setup(t)
|
||||
|
||||
|
Reference in New Issue
Block a user