feat: add cache for list files

This commit is contained in:
Noah Hsu
2022-06-13 21:14:01 +08:00
parent 6056fdbddc
commit c525406516
3 changed files with 23 additions and 4 deletions

View File

@ -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) {