mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-18 21:07:44 +03:00
Fix memory leak in CachedGenreRepository (#3031)
that the scanner was run, the ttlcache was also created each time. This caused (under testing with 166 genres in the database) the memory consumed by navidrome to 101.18MB over approx 3 days; 96% of which is in instances of this cache. Swapping to a singleton has reduced this to down to ~ 2.6MB Co-authored-by: Rob Emery <git@mintsoft.net>
This commit is contained in:
parent
de0a08915c
commit
52235c291d
@ -8,25 +8,27 @@ import (
|
|||||||
"github.com/jellydator/ttlcache/v2"
|
"github.com/jellydator/ttlcache/v2"
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
"github.com/navidrome/navidrome/model"
|
"github.com/navidrome/navidrome/model"
|
||||||
|
"github.com/navidrome/navidrome/utils/singleton"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newCachedGenreRepository(ctx context.Context, repo model.GenreRepository) model.GenreRepository {
|
func newCachedGenreRepository(ctx context.Context, repo model.GenreRepository) model.GenreRepository {
|
||||||
r := &cachedGenreRepo{
|
return singleton.GetInstance(func() *cachedGenreRepo {
|
||||||
GenreRepository: repo,
|
r := &cachedGenreRepo{
|
||||||
ctx: ctx,
|
GenreRepository: repo,
|
||||||
}
|
ctx: ctx,
|
||||||
genres, err := repo.GetAll()
|
}
|
||||||
if err != nil {
|
genres, err := repo.GetAll()
|
||||||
log.Error(ctx, "Could not load genres from DB", err)
|
|
||||||
return repo
|
|
||||||
}
|
|
||||||
|
|
||||||
r.cache = ttlcache.NewCache()
|
if err != nil {
|
||||||
for _, g := range genres {
|
log.Error(ctx, "Could not load genres from DB", err)
|
||||||
_ = r.cache.Set(strings.ToLower(g.Name), g.ID)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
r.cache = ttlcache.NewCache()
|
||||||
return r
|
for _, g := range genres {
|
||||||
|
_ = r.cache.Set(strings.ToLower(g.Name), g.ID)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type cachedGenreRepo struct {
|
type cachedGenreRepo struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user