dylinkprov: implement random cache replacement policy

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2024-07-22 14:36:48 +03:00
parent 1dc2dcd539
commit 9581ea8885
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 24 additions and 16 deletions

2
TODO
View File

@ -1,5 +1,3 @@
- Avoid invoking getRemoteLink multiple times for the same item id - Avoid invoking getRemoteLink multiple times for the same item id
for link providers for link providers
- Limite cache size (Cache replacement policy)

View File

@ -1,21 +1,24 @@
package dylinkprovider package dylinkprovider
import ( import (
"context" "context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
"gitea.hbanafa.com/hesham/yttopodcast/ytlinkprov" "gitea.hbanafa.com/hesham/yttopodcast/ytlinkprov"
"github.com/lrstanley/go-ytdlp" "github.com/lrstanley/go-ytdlp"
) )
const Q_EXPIRE = "expire" const (
Q_EXPIRE = "expire"
MAX_MAP_SZ = 1000
)
type DynamicCacheExpLinkProvider struct { type DynamicCacheExpLinkProvider struct {
cache map[string]url.URL cache map[string]url.URL
@ -37,7 +40,7 @@ func NewDynCacheExpLinkProv(l *log.Logger) *DynamicCacheExpLinkProvider {
// GetLink implements ytlinkprov.YtLinkProvider. // GetLink implements ytlinkprov.YtLinkProvider.
func (d *DynamicCacheExpLinkProvider) GetLink(id string) (link string, err error) { func (d *DynamicCacheExpLinkProvider) GetLink(id string) (link string, err error) {
d.lock.RLock() d.lock.RLock()
cl1, ok := d.cache[id] cl1, ok := d.cache[id]
d.lock.RUnlock() d.lock.RUnlock()
@ -58,11 +61,18 @@ func (d *DynamicCacheExpLinkProvider) GetLink(id string) (link string, err error
} }
d.lock.Lock() d.lock.Lock()
if len(d.cache) >= MAX_MAP_SZ {
var k string
for kk := range d.cache {
k = kk
break
}
delete(d.cache, k)
}
d.cache[id] = *newlinkurl d.cache[id] = *newlinkurl
d.lock.Unlock() d.lock.Unlock()
d.l.Printf("[cache] new entry for %s\n", id) d.l.Printf("[cache] new entry for %s\n", id)
d.l.Printf("%d items in map\n", len(d.cache))
return newlinkurl.String(), nil return newlinkurl.String(), nil
} }