Add download cache again

This commit is contained in:
Ben Sarmiento
2024-05-25 06:01:20 +02:00
parent 53c71b1249
commit 2f777f63e9
2 changed files with 98 additions and 35 deletions

View File

@@ -30,8 +30,9 @@ type TorrentManager struct {
log *logutil.Logger
repairLog *logutil.Logger
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
RootNode *fs.FileNode
@@ -63,8 +64,9 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
log: log,
repairLog: repairLog,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadMap: cmap.New[*realdebrid.Download](),
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadMap: cmap.New[*realdebrid.Download](),
DownloadCache: cmap.New[*realdebrid.Download](),
RootNode: fs.NewFileNode("root", true),
@@ -94,15 +96,18 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
// proxy function
func (t *TorrentManager) UnrestrictLinkUntilOk(link string) *realdebrid.Download {
if strings.HasPrefix(link, "https://real-debrid.com/d/") && t.DownloadCache.Has(link[0:39]) {
ret, _ := t.DownloadCache.Get(link[0:39])
return ret
}
ret, err := t.api.UnrestrictLink(link, t.Config.ShouldServeFromRclone())
t.DownloadCache.Set(ret.Link[0:39], ret)
if err != nil {
t.log.Warnf("Cannot unrestrict link %s: %v", link, err)
return nil
}
if ret != nil && ret.Link != "" && ret.Filename != "" {
if t.Config.EnableDownloadMount() {
t.DownloadMap.Set(ret.Filename, ret)
}
if t.Config.EnableDownloadMount() {
t.DownloadMap.Set(ret.Filename, ret)
}
return ret
}
@@ -272,26 +277,20 @@ func (t *TorrentManager) mountDownloads() {
return
}
t.DownloadMap.Clear()
t.DownloadCache.Clear()
_ = t.workerPool.Submit(func() {
page := 1
offset := 0
for {
downloads, totalDownloads, err := t.api.GetDownloads(page, offset)
if err != nil {
// if we get an error, we just stop
t.log.Warnf("Cannot get downloads on page %d: %v", page, err)
continue
}
for i := range downloads {
t.DownloadMap.Set(downloads[i].Filename, &downloads[i])
}
offset += len(downloads)
page++
if offset >= totalDownloads {
break
}
downloads, totalDownloads, err := t.api.GetDownloads()
if err != nil {
t.log.Errorf("Cannot get downloads: %v", err)
}
t.log.Debugf("Got %d downloads", totalDownloads)
for i := range downloads {
idx := i
if strings.HasPrefix(downloads[idx].Link, "https://real-debrid.com/d/") {
t.DownloadCache.Set(downloads[idx].Link[0:39], &downloads[idx])
}
t.DownloadMap.Set(downloads[idx].Filename, &downloads[idx])
}
t.log.Infof("Compiled into %d downloads", t.DownloadMap.Count())
})
}