Feat: cache for OneDrive META request

pull/265/head
HFO4 5 years ago
parent 96983ddc70
commit 0eb04ed0ea

@ -102,6 +102,7 @@ func addDefaultSettings() {
{Name: "onedrive_callback_check", Value: `20`, Type: "timeout"},
{Name: "aria2_call_timeout", Value: `5`, Type: "timeout"},
{Name: "onedrive_chunk_retries", Value: `1`, Type: "retry"},
{Name: "onedrive_source_timeout", Value: `1800`, Type: "timeout"},
{Name: "reset_after_upload_failed", Value: `0`, Type: "upload"},
{Name: "login_captcha", Value: `0`, Type: "login"},
{Name: "reg_captcha", Value: `0`, Type: "login"},

@ -4,7 +4,7 @@ package conf
var BackendVersion = "3.0.0-beta1"
// RequiredDBVersion 与当前版本匹配的数据库版本
var RequiredDBVersion = "3.0.0-alpha1"
var RequiredDBVersion = "3.0.0-rc1"
// IsPro 是否为Pro版本
var IsPro = "false"

@ -506,7 +506,7 @@ func TestClient_Upload(t *testing.T) {
asserts.Error(err)
}
// 大文件 分两个分片 成功
// 大文件 分两个分片 reader 返回EOF
{
client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
clientMock := ClientMock{}
@ -523,24 +523,11 @@ func TestClient_Upload(t *testing.T) {
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"123321"}`)),
},
})
clientMock.On(
"Request",
"PUT",
"123321",
testMock.Anything,
testMock.Anything,
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"http://dev.com/2"}`)),
},
})
client.Request = clientMock
err := client.Upload(context.Background(), "123.jpg", 15*1024*1024, strings.NewReader("123"))
clientMock.AssertExpectations(t)
asserts.NoError(err)
asserts.Error(err)
}
// 大文件 分两个分片 失败
@ -561,19 +548,6 @@ func TestClient_Upload(t *testing.T) {
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"123321"}`)),
},
})
clientMock.On(
"Request",
"PUT",
"123321",
testMock.Anything,
testMock.Anything,
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"http://dev.com/2"}`)),
},
})
client.Request = clientMock
err := client.Upload(context.Background(), "123.jpg", 15*1024*1024, strings.NewReader("123"))

@ -129,6 +129,7 @@ func TestDriver_Source(t *testing.T) {
}
handler.Client, _ = NewClient(&model.Policy{})
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
cache.Set("setting_onedrive_source_timeout", "1800", 0)
// 失败
{
@ -137,6 +138,17 @@ func TestDriver_Source(t *testing.T) {
asserts.Empty(res)
}
// 命中缓存 成功
{
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
handler.Client.Credential.AccessToken = "1"
cache.Set("onedrive_source_0_123.jpg", "res", 0)
res, err := handler.Source(context.Background(), "123.jpg", url.URL{}, 0, true, 0)
cache.Deletes([]string{"0_123.jpg"}, "onedrive_source_")
asserts.NoError(err)
asserts.Equal("res", res)
}
// 成功
{
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()

@ -3,7 +3,9 @@ package onedrive
import (
"context"
"errors"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/HFO4/cloudreve/pkg/request"
@ -101,8 +103,20 @@ func (handler Driver) Source(
isDownload bool,
speed int,
) (string, error) {
// 尝试从缓存中查找
if cachedURL, ok := cache.Get(fmt.Sprintf("onedrive_source_%d_%s", handler.Policy.ID, path)); ok {
return cachedURL.(string), nil
}
// 缓存不存在,重新获取
res, err := handler.Client.Meta(ctx, "", path)
if err == nil {
// 写入新的缓存
cache.Set(
fmt.Sprintf("onedrive_source_%d_%s", handler.Policy.ID, path),
res.DownloadURL,
model.GetIntSetting("onedrive_source_timeout", 1800),
)
return res.DownloadURL, nil
}
return "", err

Loading…
Cancel
Save