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
This commit is contained in:
sunshineinabox 2024-03-09 20:57:24 -08:00
commit 722c2e864f
3 changed files with 13 additions and 18 deletions

View file

@ -20,6 +20,7 @@
<PackageVersion Include="LibHac" Version="0.19.0" /> <PackageVersion Include="LibHac" Version="0.19.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> <PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.3.0" /> <PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.3.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" /> <PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
@ -48,7 +49,6 @@
<PackageVersion Include="System.Drawing.Common" Version="8.0.2" /> <PackageVersion Include="System.Drawing.Common" Version="8.0.2" />
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" /> <PackageVersion Include="System.IO.Hashing" Version="8.0.0" />
<PackageVersion Include="System.Management" Version="8.0.0" /> <PackageVersion Include="System.Management" Version="8.0.0" />
<PackageVersion Include="System.Runtime.Caching" Version="8.0.0" />
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" /> <PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -44,6 +44,7 @@
<PackageReference Include="Avalonia.Svg.Skia" /> <PackageReference Include="Avalonia.Svg.Skia" />
<PackageReference Include="DynamicData" /> <PackageReference Include="DynamicData" />
<PackageReference Include="FluentAvaloniaUI" /> <PackageReference Include="FluentAvaloniaUI" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="OpenTK.Core" /> <PackageReference Include="OpenTK.Core" />
<PackageReference Include="Ryujinx.Audio.OpenAL.Dependencies" Condition="'$(RuntimeIdentifier)' != 'linux-x64' AND '$(RuntimeIdentifier)' != 'linux-arm64' AND '$(RuntimeIdentifier)' != 'osx-x64' AND '$(RuntimeIdentifier)' != 'osx-arm64'" /> <PackageReference Include="Ryujinx.Audio.OpenAL.Dependencies" Condition="'$(RuntimeIdentifier)' != 'linux-x64' AND '$(RuntimeIdentifier)' != 'linux-arm64' AND '$(RuntimeIdentifier)' != 'osx-x64' AND '$(RuntimeIdentifier)' != 'osx-arm64'" />
@ -58,7 +59,6 @@
<!--NOTE: DO NOT REMOVE, THIS IS REQUIRED AS A RESULT OF A TRIMMING ISSUE IN AVALONIA --> <!--NOTE: DO NOT REMOVE, THIS IS REQUIRED AS A RESULT OF A TRIMMING ISSUE IN AVALONIA -->
<PackageReference Include="System.Drawing.Common" /> <PackageReference Include="System.Drawing.Common" />
<PackageReference Include="System.Runtime.Caching" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -2,11 +2,10 @@ using Avalonia;
using Avalonia.Data.Converters; using Avalonia.Data.Converters;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Microsoft.Extensions.Caching.Memory;
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.IO.Hashing;
using System.Runtime.Caching;
namespace Ryujinx.Ava.UI.Helpers namespace Ryujinx.Ava.UI.Helpers
{ {
@ -14,8 +13,7 @@ namespace Ryujinx.Ava.UI.Helpers
{ {
public static readonly BitmapArrayValueConverter Instance = new(); public static readonly BitmapArrayValueConverter Instance = new();
private readonly MemoryCache cache = MemoryCache.Default; private MemoryCache cache;
private readonly CacheItemPolicy policy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(60) };
private readonly int MaxCacheSize = 24; private readonly int MaxCacheSize = 24;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
@ -25,27 +23,29 @@ namespace Ryujinx.Ava.UI.Helpers
return null; 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)) if (value is byte[] buffer && targetType == typeof(IImage))
{ {
var bufferhash = BufferHash(buffer); cache.TryGetValue(buffer, out bool result);
var retrieved = cache.Contains(bufferhash);
if (retrieved == false) if (result == false)
{ {
MemoryStream mem = new(buffer); MemoryStream mem = new(buffer);
var bitmap = new Bitmap(mem).CreateScaledBitmap(new PixelSize(256, 256)); var bitmap = new Bitmap(mem).CreateScaledBitmap(new PixelSize(256, 256));
cache.Add(bufferhash, bitmap, policy); cache.Set(buffer, bitmap, options);
return bitmap; return bitmap;
} }
else 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(); throw new NotSupportedException();
@ -55,10 +55,5 @@ namespace Ryujinx.Ava.UI.Helpers
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
private static string BufferHash(byte[] input)
{
var hashBytes = Crc32.HashToUInt32(input);
return hashBytes.ToString();
}
} }
} }