mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-21 20:18:47 +08:00
feat: add cache for list files
This commit is contained in:
parent
6056fdbddc
commit
c525406516
2
go.mod
2
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
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user