From 9581ea88856926b3a8f5d0e79cd46bfce80f9ee4 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 22 Jul 2024 14:36:48 +0300 Subject: [PATCH] dylinkprov: implement random cache replacement policy Signed-off-by: HeshamTB --- TODO | 2 -- dylinkprovider/dylinkprov.go | 38 +++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/TODO b/TODO index f2b7ca8..823493d 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,3 @@ - Avoid invoking getRemoteLink multiple times for the same item id for link providers -- Limite cache size (Cache replacement policy) - diff --git a/dylinkprovider/dylinkprov.go b/dylinkprovider/dylinkprov.go index af738c7..218a9bb 100644 --- a/dylinkprovider/dylinkprov.go +++ b/dylinkprovider/dylinkprov.go @@ -1,21 +1,24 @@ package dylinkprovider import ( - "context" - "fmt" - "log" - "net/http" - "net/url" - "strconv" - "strings" - "sync" - "time" + "context" + "fmt" + "log" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" - "gitea.hbanafa.com/hesham/yttopodcast/ytlinkprov" - "github.com/lrstanley/go-ytdlp" + "gitea.hbanafa.com/hesham/yttopodcast/ytlinkprov" + "github.com/lrstanley/go-ytdlp" ) -const Q_EXPIRE = "expire" +const ( + Q_EXPIRE = "expire" + MAX_MAP_SZ = 1000 +) type DynamicCacheExpLinkProvider struct { cache map[string]url.URL @@ -37,7 +40,7 @@ func NewDynCacheExpLinkProv(l *log.Logger) *DynamicCacheExpLinkProvider { // GetLink implements ytlinkprov.YtLinkProvider. func (d *DynamicCacheExpLinkProvider) GetLink(id string) (link string, err error) { - + d.lock.RLock() cl1, ok := d.cache[id] d.lock.RUnlock() @@ -58,11 +61,18 @@ func (d *DynamicCacheExpLinkProvider) GetLink(id string) (link string, err error } 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.lock.Unlock() 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 }