Updated audio image extraction logic. Added caching classes.

This commit is contained in:
2025-02-26 19:07:29 -05:00
parent b38e4dc858
commit dfdf514fe2
11 changed files with 259 additions and 14 deletions

View 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);
}