Add multiple genres to MediaFile

This commit is contained in:
Deluan
2021-07-16 11:03:28 -04:00
committed by Deluan Quintão
parent 7cd3a8ba67
commit 39da741a80
21 changed files with 309 additions and 72 deletions
+45
View File
@@ -0,0 +1,45 @@
package scanner
import (
"context"
"strings"
"time"
"github.com/ReneKroon/ttlcache/v2"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
)
func newCachedGenreRepository(ctx context.Context, repo model.GenreRepository) model.GenreRepository {
r := &cachedGenreRepo{
GenreRepository: repo,
ctx: ctx,
}
genres, err := repo.GetAll()
if err != nil {
log.Error(ctx, "Could not load genres from DB", err)
return repo
}
r.cache = ttlcache.NewCache()
for _, g := range genres {
_ = r.cache.Set(strings.ToLower(g.Name), g.ID)
}
return r
}
type cachedGenreRepo struct {
model.GenreRepository
cache *ttlcache.Cache
ctx context.Context
}
func (r *cachedGenreRepo) Put(g *model.Genre) error {
id, err := r.cache.GetByLoader(strings.ToLower(g.Name), func(key string) (interface{}, time.Duration, error) {
err := r.GenreRepository.Put(g)
return g.ID, 24 * time.Hour, err
})
g.ID = id.(string)
return err
}