From c525406516c0c3a7a6f6f368937c00e96dc2d36d Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 13 Jun 2022 21:14:01 +0800 Subject: [PATCH] feat: add cache for list files --- go.mod | 2 +- go.sum | 2 ++ internal/operations/fs.go | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 9e620fbc..530be4c8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/alist-org/alist/v3 go 1.18 require ( + github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448 github.com/caarlos0/env/v6 v6.9.3 github.com/gin-gonic/gin v1.8.0 github.com/json-iterator/go v1.1.12 @@ -13,7 +14,6 @@ require ( ) require ( - github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect diff --git a/go.sum b/go.sum index 9b13574e..b145b358 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345 h1:8NM5hexnIasEVWK47FRhIcXYnhyH9pJLZ0bIAMSIDqE= github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345/go.mod h1:sSBbaOg90XwWKtpT56kVujF0bIeVITnPlssLclogS04= +github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448 h1:0TL8OCXaQD1YhG0D3YAfDcm/n4QRo4rCGiU0Pa5nQC4= +github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448/go.mod h1:sSBbaOg90XwWKtpT56kVujF0bIeVITnPlssLclogS04= github.com/caarlos0/env/v6 v6.9.3 h1:Tyg69hoVXDnpO5Qvpsu8EoquarbPyQb+YwExWHP8wWU= github.com/caarlos0/env/v6 v6.9.3/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 938854cc..f8d34b1b 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -2,21 +2,38 @@ package operations import ( "context" + stdpath "path" + "time" + "github.com/Xhofe/go-cache" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/singleflight" "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" - stdpath "path" ) // In order to facilitate adding some other things before and after file operations +var filesCache = cache.NewMemCache[[]driver.FileInfo]() +var filesG singleflight.Group[[]driver.FileInfo] + // List files in storage, not contains virtual file -// TODO: cache, and prevent cache breakdown func List(ctx context.Context, account driver.Driver, path string) ([]driver.FileInfo, error) { - return account.List(ctx, path) + key := stdpath.Join(account.GetAccount().VirtualPath, path) + if files, ok := filesCache.Get(key); ok { + return files, nil + } + files, err, _ := filesG.Do(key, func() ([]driver.FileInfo, error) { + files, err := account.List(ctx, path) + if err != nil { + return nil, errors.WithMessage(err, "failed to list files") + } + // TODO: get duration from global config or account's config + filesCache.Set(key, files, cache.WithEx[[]driver.FileInfo](time.Minute*30)) + return files, nil + }) + return files, err } func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) {