feat: link cache

refactor/fs
Noah Hsu 2022-06-13 19:56:33 +08:00
parent e16ab876aa
commit 2f52b5d354
3 changed files with 14 additions and 1 deletions

1
go.mod
View File

@ -13,6 +13,7 @@ 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
View File

@ -1,3 +1,5 @@
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/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=

View File

@ -2,6 +2,7 @@ package operations
import (
"context"
"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/utils"
@ -50,12 +51,21 @@ func Get(ctx context.Context, account driver.Driver, path string) (driver.FileIn
return nil, errors.WithStack(driver.ErrorObjectNotFound)
}
// Link get link, if is a url. show have an expiry time
var linkCache = cache.NewMemCache[*driver.Link]()
// Link get link, if is an url. should have an expiry time
func Link(ctx context.Context, account driver.Driver, path string, args driver.LinkArgs) (*driver.Link, error) {
key := stdpath.Join(account.GetAccount().VirtualPath, path)
if link, ok := linkCache.Get(key); ok {
return link, nil
}
link, err := account.Link(ctx, path, args)
if err != nil {
return nil, errors.WithMessage(err, "failed get link")
}
if link.Expiration != nil {
linkCache.Set(key, link, cache.WithEx(*link.Expiration))
}
return link, nil
}