From 722c2e864f99ae0e411eb91c22beccee23cc0be5 Mon Sep 17 00:00:00 2001 From: sunshineinabox Date: Sat, 9 Mar 2024 20:57:24 -0800 Subject: [PATCH] use Microsoft.Extensions.Caching.Memory instead of System.Runtime.Caching. For some reason setting size limit in cache options works, but setting a compaction value does not seem to work need to investigate this further --- Directory.Packages.props | 2 +- src/Ryujinx/Ryujinx.csproj | 2 +- .../UI/Helpers/BitmapArrayValueConverter.cs | 27 ++++++++----------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index faae9499e9..47904c3fae 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -20,6 +20,7 @@ + @@ -48,7 +49,6 @@ - \ No newline at end of file diff --git a/src/Ryujinx/Ryujinx.csproj b/src/Ryujinx/Ryujinx.csproj index a8a010b7f9..d79153cdb2 100644 --- a/src/Ryujinx/Ryujinx.csproj +++ b/src/Ryujinx/Ryujinx.csproj @@ -44,6 +44,7 @@ + @@ -58,7 +59,6 @@ - diff --git a/src/Ryujinx/UI/Helpers/BitmapArrayValueConverter.cs b/src/Ryujinx/UI/Helpers/BitmapArrayValueConverter.cs index a936198f4e..7fe6acefd1 100644 --- a/src/Ryujinx/UI/Helpers/BitmapArrayValueConverter.cs +++ b/src/Ryujinx/UI/Helpers/BitmapArrayValueConverter.cs @@ -2,11 +2,10 @@ using Avalonia; using Avalonia.Data.Converters; using Avalonia.Media; using Avalonia.Media.Imaging; +using Microsoft.Extensions.Caching.Memory; using System; using System.Globalization; using System.IO; -using System.IO.Hashing; -using System.Runtime.Caching; namespace Ryujinx.Ava.UI.Helpers { @@ -14,8 +13,7 @@ namespace Ryujinx.Ava.UI.Helpers { public static readonly BitmapArrayValueConverter Instance = new(); - private readonly MemoryCache cache = MemoryCache.Default; - private readonly CacheItemPolicy policy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(60) }; + private MemoryCache cache; private readonly int MaxCacheSize = 24; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) @@ -25,27 +23,29 @@ namespace Ryujinx.Ava.UI.Helpers return null; } + cache = new MemoryCache(new MemoryCacheOptions()); + var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromSeconds(10), Size = 1 }; + if (value is byte[] buffer && targetType == typeof(IImage)) { - var bufferhash = BufferHash(buffer); - var retrieved = cache.Contains(bufferhash); + cache.TryGetValue(buffer, out bool result); - if (retrieved == false) + if (result == false) { MemoryStream mem = new(buffer); var bitmap = new Bitmap(mem).CreateScaledBitmap(new PixelSize(256, 256)); - cache.Add(bufferhash, bitmap, policy); + cache.Set(buffer, bitmap, options); return bitmap; } else { - return cache.Get(bufferhash); + return cache.Get(buffer); } } - if (cache.GetCount() >= MaxCacheSize) + if (cache.Count >= MaxCacheSize) { - cache.Trim(50); + cache.Compact(50); } throw new NotSupportedException(); @@ -55,10 +55,5 @@ namespace Ryujinx.Ava.UI.Helpers { throw new NotSupportedException(); } - private static string BufferHash(byte[] input) - { - var hashBytes = Crc32.HashToUInt32(input); - return hashBytes.ToString(); - } } }