more efficient caching
This commit is contained in:
@@ -11,14 +11,15 @@ import (
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
)
|
||||
|
||||
type TorrentManager struct {
|
||||
token string
|
||||
torrents []Torrent
|
||||
workerPool chan bool
|
||||
config config.ConfigInterface
|
||||
checksum string
|
||||
config config.ConfigInterface
|
||||
cache *expirable.LRU[string, string]
|
||||
workerPool chan bool
|
||||
}
|
||||
|
||||
func (t *TorrentManager) refreshTorrents() {
|
||||
@@ -30,6 +31,7 @@ func (t *TorrentManager) refreshTorrents() {
|
||||
continue
|
||||
}
|
||||
t.checksum = checksum
|
||||
t.cache.Purge()
|
||||
t.torrents = t.getAll()
|
||||
for _, torrent := range t.torrents {
|
||||
go func(id string) {
|
||||
@@ -45,11 +47,11 @@ func (t *TorrentManager) refreshTorrents() {
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
// it will fetch all torrents and their info in the background
|
||||
// and store them in-memory
|
||||
func NewTorrentManager(config config.ConfigInterface) *TorrentManager {
|
||||
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
|
||||
handler := &TorrentManager{
|
||||
token: config.GetToken(),
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
config: config,
|
||||
cache: cache,
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
}
|
||||
|
||||
// Initialize torrents for the first time
|
||||
@@ -71,7 +73,7 @@ func NewTorrentManager(config config.ConfigInterface) *TorrentManager {
|
||||
}
|
||||
|
||||
func (t *TorrentManager) getChecksum() string {
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.token, 1)
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.config.GetToken(), 1)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get torrents: %v\n", err)
|
||||
return t.checksum
|
||||
@@ -86,7 +88,7 @@ func (t *TorrentManager) getChecksum() string {
|
||||
func (t *TorrentManager) getAll() []Torrent {
|
||||
log.Println("Getting all torrents")
|
||||
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.token, 0)
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.config.GetToken(), 0)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get torrents: %v\n", err)
|
||||
return nil
|
||||
@@ -172,7 +174,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
return torrent
|
||||
}
|
||||
log.Println("Getting info for", torrentID)
|
||||
info, err := realdebrid.GetTorrentInfo(t.token, torrentID)
|
||||
info, err := realdebrid.GetTorrentInfo(t.config.GetToken(), torrentID)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get info: %v\n", err)
|
||||
return nil
|
||||
@@ -209,7 +211,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
defer func() { <-sem }() // Release semaphore
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictCheck(t.token, lnk)
|
||||
return realdebrid.UnrestrictCheck(t.config.GetToken(), lnk)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp != nil {
|
||||
|
||||
Reference in New Issue
Block a user