Adjusted cache to allow for throttling and locking.

This commit is contained in:
2025-02-26 23:10:36 -05:00
parent 6835431d1e
commit c1a7d23096
5 changed files with 89 additions and 31 deletions

View File

@@ -1,12 +1,19 @@
using Harmonia.Core.Imaging;
using Harmonia.Core.Models;
using Microsoft.Extensions.Caching.Memory;
namespace Harmonia.Core.Caching;
public class AudioImageMemoryCache(IAudioImageExtractor audioImageExtractor) : MemoryCache<Song, SongPictureInfo>, IAudioImageCache
{
protected override long? SizeLimit => 2000000000;
protected override double CompactionPercentage => 0.2;
protected override int MaxConcurrentRequests => 8;
protected override MemoryCacheOptions Options => new()
{
SizeLimit = 2000000000,
CompactionPercentage = 0.2,
};
protected override TimeSpan SlidingExpiration => TimeSpan.FromSeconds(600);
protected override object? GetKey(Song key)
@@ -23,14 +30,11 @@ public class AudioImageMemoryCache(IAudioImageExtractor audioImageExtractor) : M
return null;
}
protected override SongPictureInfo? Fetch(Song key)
protected override ValueTask<SongPictureInfo?> FetchAsync(Song key, CancellationToken cancellationToken)
{
return audioImageExtractor.ExtractImage(key.FileName);
}
SongPictureInfo? songPictureInfo = audioImageExtractor.ExtractImage(key.FileName);
protected override void AddToCache(object key, SongPictureInfo entry)
{
base.AddToCache(key, entry);
return ValueTask.FromResult(songPictureInfo);
}
protected override long GetEntrySize(SongPictureInfo entry)