55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
using Avalonia.Media.Imaging;
|
|
using Harmonia.Core.Caching;
|
|
using Harmonia.Core.Imaging;
|
|
using Harmonia.Core.Models;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Harmonia.UI.Caching;
|
|
|
|
public class AudioBitmapCache(IAudioImageExtractor audioImageExtractor) : MemoryCache<Song, Bitmap>, IAudioBitmapCache
|
|
{
|
|
protected override MemoryCacheOptions Options => new()
|
|
{
|
|
SizeLimit = 40,
|
|
CompactionPercentage = 0.2,
|
|
};
|
|
|
|
protected override TimeSpan SlidingExpiration => TimeSpan.FromSeconds(600);
|
|
|
|
protected override int MaxConcurrentRequests => 8;
|
|
|
|
protected override object? GetKey(Song key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key.ImageHash) == false)
|
|
{
|
|
return key.ImageHash;
|
|
}
|
|
else if (string.IsNullOrWhiteSpace(key.ImageName) == false)
|
|
{
|
|
return key.ImageName;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected override async ValueTask<Bitmap?> FetchAsync(Song key, CancellationToken cancellationToken)
|
|
{
|
|
SongPictureInfo? songPictureInfo = await audioImageExtractor.ExtractImageAsync(key.FileName, cancellationToken);
|
|
|
|
if (songPictureInfo == null)
|
|
return null;
|
|
|
|
using MemoryStream stream = new(songPictureInfo.Data);
|
|
|
|
return new Bitmap(stream);
|
|
}
|
|
|
|
protected override long GetEntrySize(Bitmap entry)
|
|
{
|
|
return 1;
|
|
}
|
|
} |