add expiry to cached full size cover art
This commit is contained in:
@@ -29,6 +29,12 @@ type ImageCache struct {
|
|||||||
MaxSize int
|
MaxSize int
|
||||||
DefaultTTL time.Duration
|
DefaultTTL time.Duration
|
||||||
|
|
||||||
|
// Sets a callback that is invoked whenever the periodic
|
||||||
|
// eviction has been run. Allows for "tacking on" extra
|
||||||
|
// cleanup tasks outside of the ImageCache's jurisdiction
|
||||||
|
// that are run on the same schedule.
|
||||||
|
OnEvictTaskRan func()
|
||||||
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
cache map[string]CacheItem
|
cache map[string]CacheItem
|
||||||
}
|
}
|
||||||
@@ -157,6 +163,9 @@ func (i *ImageCache) periodicallyEvict(ctx context.Context, interval time.Durati
|
|||||||
return
|
return
|
||||||
case <-t.C:
|
case <-t.C:
|
||||||
i.EvictExpired()
|
i.EvictExpired()
|
||||||
|
if i.OnEvictTaskRan != nil {
|
||||||
|
i.OnEvictTaskRan()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-1
@@ -20,7 +20,10 @@ import (
|
|||||||
|
|
||||||
const CachedImageValidTime = 24 * time.Hour
|
const CachedImageValidTime = 24 * time.Hour
|
||||||
|
|
||||||
const coverArtThumbnailSize = 300
|
const (
|
||||||
|
coverArtThumbnailSize = 300
|
||||||
|
fullSizeCoverExpires = 5 * time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
type ImageManager struct {
|
type ImageManager struct {
|
||||||
s *ServerManager
|
s *ServerManager
|
||||||
@@ -29,6 +32,7 @@ type ImageManager struct {
|
|||||||
|
|
||||||
cachedFullSizeCover image.Image
|
cachedFullSizeCover image.Image
|
||||||
cachedFullSizeCoverID string
|
cachedFullSizeCoverID string
|
||||||
|
cachedFullSizeCoverAccessedAt int64 // unixMillis
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewImageManager(ctx context.Context, s *ServerManager, baseCacheDir string) *ImageManager {
|
func NewImageManager(ctx context.Context, s *ServerManager, baseCacheDir string) *ImageManager {
|
||||||
@@ -45,6 +49,7 @@ func NewImageManager(ctx context.Context, s *ServerManager, baseCacheDir string)
|
|||||||
DefaultTTL: 1 * time.Minute,
|
DefaultTTL: 1 * time.Minute,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
i.thumbnailCache.OnEvictTaskRan = i.clearExpiredFullSizeCover
|
||||||
i.thumbnailCache.Init(ctx, 2*time.Minute)
|
i.thumbnailCache.Init(ctx, 2*time.Minute)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@@ -74,6 +79,7 @@ func (i *ImageManager) GetCoverThumbnailWithTTL(coverID string, ttl time.Duratio
|
|||||||
|
|
||||||
func (i *ImageManager) GetFullSizeCoverArt(coverID string) (image.Image, error) {
|
func (i *ImageManager) GetFullSizeCoverArt(coverID string) (image.Image, error) {
|
||||||
if i.cachedFullSizeCoverID == coverID {
|
if i.cachedFullSizeCoverID == coverID {
|
||||||
|
i.cachedFullSizeCoverAccessedAt = time.Now().UnixMilli()
|
||||||
return i.cachedFullSizeCover, nil
|
return i.cachedFullSizeCover, nil
|
||||||
}
|
}
|
||||||
if i.s.Server == nil {
|
if i.s.Server == nil {
|
||||||
@@ -85,6 +91,7 @@ func (i *ImageManager) GetFullSizeCoverArt(coverID string) (image.Image, error)
|
|||||||
}
|
}
|
||||||
i.cachedFullSizeCover = im
|
i.cachedFullSizeCover = im
|
||||||
i.cachedFullSizeCoverID = coverID
|
i.cachedFullSizeCoverID = coverID
|
||||||
|
i.cachedFullSizeCoverAccessedAt = time.Now().UnixMilli()
|
||||||
return im, nil
|
return im, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,3 +222,11 @@ func (i *ImageManager) loadLocalImage(path string) (image.Image, bool) {
|
|||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *ImageManager) clearExpiredFullSizeCover() {
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if now-i.cachedFullSizeCoverAccessedAt > fullSizeCoverExpires.Milliseconds() {
|
||||||
|
i.cachedFullSizeCoverID = ""
|
||||||
|
i.cachedFullSizeCover = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user