1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-28 22:23:41 +00:00

lib/transform: add caching support

adds local caching to avoid re-parsing when possible
This commit is contained in:
nielash
2025-05-04 04:11:54 -04:00
committed by Nick Craig-Wood
parent ab7d6e72e1
commit 19fe519ac8

View File

@@ -3,7 +3,9 @@ package transform
import (
"context"
"errors"
"slices"
"strings"
"sync"
"github.com/rclone/rclone/fs"
)
@@ -40,6 +42,13 @@ func SetOptions(ctx context.Context, s ...string) (err error) {
return err
}
// cache to minimize re-parsing
var (
cachedNameTransform []string
cachedOpt []transform
cacheLock sync.Mutex
)
// getOptions sets the options from flags passed in.
func getOptions(ctx context.Context) (opt []transform, err error) {
if !Transforming(ctx) {
@@ -47,6 +56,12 @@ func getOptions(ctx context.Context) (opt []transform, err error) {
}
ci := fs.GetConfig(ctx)
// return cached opt if available
if cachedNameTransform != nil && slices.Equal(ci.NameTransform, cachedNameTransform) {
return cachedOpt, nil
}
for _, transform := range ci.NameTransform {
t, err := parse(transform)
if err != nil {
@@ -54,10 +69,17 @@ func getOptions(ctx context.Context) (opt []transform, err error) {
}
opt = append(opt, t)
}
// TODO: should we store opt in ci and skip re-parsing when present, for efficiency?
updateCache(ci.NameTransform, opt)
return opt, nil
}
func updateCache(nt []string, o []transform) {
cacheLock.Lock()
cachedNameTransform = slices.Clone(nt)
cachedOpt = o
cacheLock.Unlock()
}
// parse a single instance of --name-transform
func parse(s string) (t transform, err error) {
if s == "" {