Implement better artwork cache keys

This commit is contained in:
Deluan 2023-01-17 20:37:10 -05:00
parent 93adda66d9
commit bedd2b2074
5 changed files with 39 additions and 11 deletions

View File

@ -21,12 +21,9 @@ type cacheKey struct {
func (k *cacheKey) Key() string {
return fmt.Sprintf(
"%s.%d.%d.%d.%t",
k.artID.ID,
"%s.%d",
k.artID,
k.lastUpdate.UnixMilli(),
k.size,
conf.Server.CoverJpegQuality,
conf.Server.EnableMediaFileCoverArt,
)
}

View File

@ -2,6 +2,8 @@ package artwork
import (
"context"
"crypto/md5"
"fmt"
"io"
"strings"
"time"
@ -34,6 +36,18 @@ func newAlbumArtworkReader(ctx context.Context, artwork *artwork, artID model.Ar
return a, nil
}
func (a *albumArtworkReader) Key() string {
var hash [16]byte
if conf.Server.EnableExternalServices {
hash = md5.Sum([]byte(conf.Server.Agents + conf.Server.CoverArtPriority))
}
return fmt.Sprintf(
"%s.%x.%t",
a.cacheKey.Key(),
hash,
conf.Server.EnableExternalServices,
)
}
func (a *albumArtworkReader) LastUpdated() time.Time {
return a.album.UpdatedAt
}

View File

@ -62,13 +62,12 @@ func newArtistReader(ctx context.Context, artwork *artwork, artID model.ArtworkI
}
func (a *artistReader) Key() string {
agentsHash := md5.Sum([]byte(conf.Server.Agents + conf.Server.Spotify.ID))
hash := md5.Sum([]byte(conf.Server.Agents + conf.Server.Spotify.ID))
return fmt.Sprintf(
"%s.%d.%d.%x",
a.artID,
a.lastUpdate.UnixMilli(),
a.size,
agentsHash,
"%s.%x.%t",
a.cacheKey.Key(),
hash,
conf.Server.EnableExternalServices,
)
}

View File

@ -2,9 +2,11 @@ package artwork
import (
"context"
"fmt"
"io"
"time"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/model"
)
@ -38,6 +40,13 @@ func newMediafileArtworkReader(ctx context.Context, artwork *artwork, artID mode
return a, nil
}
func (a *mediafileArtworkReader) Key() string {
return fmt.Sprintf(
"%s.%t",
a.cacheKey.Key(),
conf.Server.EnableMediaFileCoverArt,
)
}
func (a *mediafileArtworkReader) LastUpdated() time.Time {
return a.lastUpdate
}

View File

@ -38,6 +38,15 @@ func resizedFromOriginal(ctx context.Context, a *artwork, artID model.ArtworkID,
return r, nil
}
func (a *resizedArtworkReader) Key() string {
return fmt.Sprintf(
"%s.%d.%d",
a.cacheKey.Key(),
a.size,
conf.Server.CoverJpegQuality,
)
}
func (a *resizedArtworkReader) LastUpdated() time.Time {
return a.lastUpdate
}