feat: link cache

This commit is contained in:
Noah Hsu
2022-06-13 19:56:33 +08:00
parent e16ab876aa
commit 2f52b5d354
3 changed files with 14 additions and 1 deletions

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
}