Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/offline_download/thunder/util.go
1562 views
1
package thunder
2
3
import (
4
"context"
5
"time"
6
7
"github.com/Xhofe/go-cache"
8
"github.com/alist-org/alist/v3/drivers/thunder"
9
"github.com/alist-org/alist/v3/internal/op"
10
"github.com/alist-org/alist/v3/pkg/singleflight"
11
)
12
13
var taskCache = cache.NewMemCache(cache.WithShards[[]thunder.OfflineTask](16))
14
var taskG singleflight.Group[[]thunder.OfflineTask]
15
16
func (t *Thunder) GetTasks(thunderDriver *thunder.Thunder) ([]thunder.OfflineTask, error) {
17
key := op.Key(thunderDriver, "/drive/v1/task")
18
if !t.refreshTaskCache {
19
if tasks, ok := taskCache.Get(key); ok {
20
return tasks, nil
21
}
22
}
23
t.refreshTaskCache = false
24
tasks, err, _ := taskG.Do(key, func() ([]thunder.OfflineTask, error) {
25
ctx := context.Background()
26
tasks, err := thunderDriver.OfflineList(ctx, "")
27
if err != nil {
28
return nil, err
29
}
30
// 添加缓存 10s
31
if len(tasks) > 0 {
32
taskCache.Set(key, tasks, cache.WithEx[[]thunder.OfflineTask](time.Second*10))
33
} else {
34
taskCache.Del(key)
35
}
36
return tasks, nil
37
})
38
if err != nil {
39
return nil, err
40
}
41
return tasks, nil
42
}
43
44