Skip to content

Commit c525406

Browse files
committed
feat: add cache for list files
1 parent 6056fdb commit c525406

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/alist-org/alist/v3
33
go 1.18
44

55
require (
6+
github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448
67
github.com/caarlos0/env/v6 v6.9.3
78
github.com/gin-gonic/gin v1.8.0
89
github.com/json-iterator/go v1.1.12
@@ -13,7 +14,6 @@ require (
1314
)
1415

1516
require (
16-
github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345 // indirect
1717
github.com/gin-contrib/sse v0.1.0 // indirect
1818
github.com/go-playground/locales v0.14.0 // indirect
1919
github.com/go-playground/universal-translator v0.18.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345 h1:8NM5hexnIasEVWK47FRhIcXYnhyH9pJLZ0bIAMSIDqE=
22
github.com/Xhofe/go-cache v0.0.0-20220613100912-dbdb5bb9a345/go.mod h1:sSBbaOg90XwWKtpT56kVujF0bIeVITnPlssLclogS04=
3+
github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448 h1:0TL8OCXaQD1YhG0D3YAfDcm/n4QRo4rCGiU0Pa5nQC4=
4+
github.com/Xhofe/go-cache v0.0.0-20220613125742-9554c28ee448/go.mod h1:sSBbaOg90XwWKtpT56kVujF0bIeVITnPlssLclogS04=
35
github.com/caarlos0/env/v6 v6.9.3 h1:Tyg69hoVXDnpO5Qvpsu8EoquarbPyQb+YwExWHP8wWU=
46
github.com/caarlos0/env/v6 v6.9.3/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
57
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

internal/operations/fs.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,38 @@ package operations
22

33
import (
44
"context"
5+
stdpath "path"
6+
"time"
7+
58
"github.com/Xhofe/go-cache"
69
"github.com/alist-org/alist/v3/internal/driver"
710
"github.com/alist-org/alist/v3/internal/model"
811
"github.com/alist-org/alist/v3/pkg/singleflight"
912
"github.com/alist-org/alist/v3/pkg/utils"
1013
"github.com/pkg/errors"
11-
stdpath "path"
1214
)
1315

1416
// In order to facilitate adding some other things before and after file operations
1517

18+
var filesCache = cache.NewMemCache[[]driver.FileInfo]()
19+
var filesG singleflight.Group[[]driver.FileInfo]
20+
1621
// List files in storage, not contains virtual file
17-
// TODO: cache, and prevent cache breakdown
1822
func List(ctx context.Context, account driver.Driver, path string) ([]driver.FileInfo, error) {
19-
return account.List(ctx, path)
23+
key := stdpath.Join(account.GetAccount().VirtualPath, path)
24+
if files, ok := filesCache.Get(key); ok {
25+
return files, nil
26+
}
27+
files, err, _ := filesG.Do(key, func() ([]driver.FileInfo, error) {
28+
files, err := account.List(ctx, path)
29+
if err != nil {
30+
return nil, errors.WithMessage(err, "failed to list files")
31+
}
32+
// TODO: get duration from global config or account's config
33+
filesCache.Set(key, files, cache.WithEx[[]driver.FileInfo](time.Minute*30))
34+
return files, nil
35+
})
36+
return files, err
2037
}
2138

2239
func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) {

0 commit comments

Comments
 (0)