@@ -2,21 +2,38 @@ package operations
2
2
3
3
import (
4
4
"context"
5
+ stdpath "path"
6
+ "time"
7
+
5
8
"github.com/Xhofe/go-cache"
6
9
"github.com/alist-org/alist/v3/internal/driver"
7
10
"github.com/alist-org/alist/v3/internal/model"
8
11
"github.com/alist-org/alist/v3/pkg/singleflight"
9
12
"github.com/alist-org/alist/v3/pkg/utils"
10
13
"github.com/pkg/errors"
11
- stdpath "path"
12
14
)
13
15
14
16
// In order to facilitate adding some other things before and after file operations
15
17
18
+ var filesCache = cache .NewMemCache [[]driver.FileInfo ]()
19
+ var filesG singleflight.Group [[]driver.FileInfo ]
20
+
16
21
// List files in storage, not contains virtual file
17
- // TODO: cache, and prevent cache breakdown
18
22
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
20
37
}
21
38
22
39
func Get (ctx context.Context , account driver.Driver , path string ) (driver.FileInfo , error ) {
0 commit comments