Skip to content

Commit

Permalink
feat: add cache for list files
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 13, 2022
1 parent 6056fdb commit c525406
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
23 changes: 20 additions & 3 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c525406

Please sign in to comment.