mirror of
https://github.com/rclone/rclone.git
synced 2024-11-26 02:14:42 +01:00
ceeac84cfe
This caches all the objects returned from the List call. This makes opening them much quicker so speeds up prune and restores. It also uses fewer transactions. It can be disabled with `--cache-objects=false`. This was discovered when using the B2 backend when the budget was being blown on list object calls which can avoided with a bit of caching. For typical 1 million file backup for a latop or server this will only use a small amount more memory.
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package restic
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/rclone/rclone/fstest/mockobject"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func (c *cache) String() string {
|
|
keys := []string{}
|
|
c.mu.Lock()
|
|
for k := range c.items {
|
|
keys = append(keys, k)
|
|
}
|
|
c.mu.Unlock()
|
|
sort.Strings(keys)
|
|
return strings.Join(keys, ",")
|
|
}
|
|
|
|
func TestCacheCRUD(t *testing.T) {
|
|
c := newCache()
|
|
assert.Equal(t, "", c.String())
|
|
assert.Nil(t, c.find("potato"))
|
|
o := mockobject.New("potato")
|
|
c.add(o.Remote(), o)
|
|
assert.Equal(t, "potato", c.String())
|
|
assert.Equal(t, o, c.find("potato"))
|
|
c.remove("potato")
|
|
assert.Equal(t, "", c.String())
|
|
assert.Nil(t, c.find("potato"))
|
|
c.remove("notfound")
|
|
}
|
|
|
|
func TestCacheRemovePrefix(t *testing.T) {
|
|
c := newCache()
|
|
for _, remote := range []string{
|
|
"a",
|
|
"b",
|
|
"b/1",
|
|
"b/2/3",
|
|
"b/2/4",
|
|
"b/2",
|
|
"c",
|
|
} {
|
|
c.add(remote, mockobject.New(remote))
|
|
}
|
|
assert.Equal(t, "a,b,b/1,b/2,b/2/3,b/2/4,c", c.String())
|
|
c.removePrefix("b")
|
|
assert.Equal(t, "a,b,c", c.String())
|
|
c.removePrefix("/")
|
|
assert.Equal(t, "", c.String())
|
|
}
|