Updated audio image extraction logic. Added caching classes.
This commit is contained in:
36
Harmonia.Core/Caching/AudioImageMemoryCache.cs
Normal file
36
Harmonia.Core/Caching/AudioImageMemoryCache.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public class AudioImageMemoryCache(IAudioImageExtractor audioImageExtractor) : MemoryCache<Song, SongPictureInfo>, IAudioImageCache
|
||||
{
|
||||
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 SongPictureInfo? Fetch(Song key)
|
||||
{
|
||||
return audioImageExtractor.ExtractImage(key.FileName);
|
||||
}
|
||||
|
||||
protected override void AddToCache(object key, SongPictureInfo entry)
|
||||
{
|
||||
base.AddToCache(key, entry);
|
||||
}
|
||||
|
||||
protected override long GetEntrySize(SongPictureInfo entry)
|
||||
{
|
||||
return entry.Size;
|
||||
}
|
||||
}
|
||||
40
Harmonia.Core/Caching/Cache.cs
Normal file
40
Harmonia.Core/Caching/Cache.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public abstract class Cache<TKey, TValue> : ICache<TKey, TValue> where TKey : notnull
|
||||
{
|
||||
protected abstract TValue? TryGetValue(object key);
|
||||
protected abstract TValue? Fetch(TKey key);
|
||||
protected abstract void AddToCache(object key, TValue value);
|
||||
|
||||
public TValue? Get(TKey key)
|
||||
{
|
||||
object? actualKey = GetKey(key);
|
||||
|
||||
if (actualKey == null)
|
||||
return default;
|
||||
|
||||
return TryGetValue(actualKey) ?? Refresh(key);
|
||||
}
|
||||
|
||||
public TValue? Refresh(TKey key)
|
||||
{
|
||||
object? actualKey = GetKey(key);
|
||||
|
||||
if (actualKey == null)
|
||||
return default;
|
||||
|
||||
TValue? value = Fetch(key);
|
||||
|
||||
if (value == null)
|
||||
return default;
|
||||
|
||||
AddToCache(actualKey, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
protected virtual object? GetKey(TKey key)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
}
|
||||
9
Harmonia.Core/Caching/IAudioImageCache.cs
Normal file
9
Harmonia.Core/Caching/IAudioImageCache.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public interface IAudioImageCache : ICache<Song, SongPictureInfo>
|
||||
{
|
||||
|
||||
}
|
||||
7
Harmonia.Core/Caching/ICache.cs
Normal file
7
Harmonia.Core/Caching/ICache.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public interface ICache<TKey, TValue>
|
||||
{
|
||||
TValue? Get(TKey key);
|
||||
TValue? Refresh(TKey key);
|
||||
}
|
||||
43
Harmonia.Core/Caching/MemoryCache.cs
Normal file
43
Harmonia.Core/Caching/MemoryCache.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public abstract class MemoryCache<TKey, TValue> : Cache<TKey, TValue> where TKey : notnull
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
protected virtual long? SizeLimit => 2000000000;
|
||||
protected virtual double CompactionPercentage => 0.2;
|
||||
protected virtual TimeSpan SlidingExpiration => TimeSpan.FromSeconds(600);
|
||||
|
||||
public MemoryCache()
|
||||
{
|
||||
MemoryCacheOptions memoryCacheOptions = new()
|
||||
{
|
||||
SizeLimit = SizeLimit,
|
||||
CompactionPercentage = CompactionPercentage
|
||||
};
|
||||
|
||||
_memoryCache = new MemoryCache(memoryCacheOptions);
|
||||
}
|
||||
|
||||
protected override TValue? TryGetValue(object key)
|
||||
{
|
||||
_memoryCache.TryGetValue(key, out TValue? value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
protected override void AddToCache(object key, TValue entry)
|
||||
{
|
||||
long entrySize = GetEntrySize(entry);
|
||||
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSize(entrySize)
|
||||
.SetSlidingExpiration(SlidingExpiration);
|
||||
|
||||
_memoryCache.Set(key, entry, cacheEntryOptions);
|
||||
}
|
||||
|
||||
protected abstract long GetEntrySize(TValue entry);
|
||||
}
|
||||
Reference in New Issue
Block a user