From 451e418b186ddaece638ff2d3a978eb84ad624ef Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 28 Sep 2022 21:19:36 +0800 Subject: [PATCH] perf: return cache before check obj to reduce recursion --- internal/op/fs.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/op/fs.go b/internal/op/fs.go index 9c0bceecb10..e7e0e85fb8a 100644 --- a/internal/op/fs.go +++ b/internal/op/fs.go @@ -38,6 +38,13 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li } path = utils.StandardizePath(path) log.Debugf("op.List %s", path) + key := Key(storage, path) + if len(refresh) == 0 || !refresh[0] { + if files, ok := listCache.Get(key); ok { + log.Debugf("use cache when list %s", path) + return files, nil + } + } dir, err := Get(ctx, storage, path) if err != nil { return nil, errors.WithMessage(err, "failed get dir") @@ -46,22 +53,14 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li if !dir.IsDir() { return nil, errors.WithStack(errs.NotFolder) } - if storage.Config().NoCache { - objs, err := storage.List(ctx, dir, args) - return objs, errors.WithStack(err) - } - key := Key(storage, path) - if len(refresh) == 0 || !refresh[0] { - if files, ok := listCache.Get(key); ok && len(files) > 0 { - return files, nil - } - } objs, err, _ := listG.Do(key, func() ([]model.Obj, error) { files, err := storage.List(ctx, dir, args) if err != nil { return nil, errors.Wrapf(err, "failed to list objs") } - listCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration))) + if !storage.Config().NoCache && len(files) > 0 { + listCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration))) + } return files, nil }) return objs, err @@ -128,6 +127,7 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er return f, nil } } + log.Debugf("cant find obj with name: %s", name) return nil, errors.WithStack(errs.ObjectNotFound) }