Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EasyFortniteStats-ImageApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="7.1.7" />
<PackageReference Include="AsyncKeyedLock" Version="8.0.0" />
<PackageReference Include="SkiaSharp" Version="[2.88.9,3.0)" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="[2.88.9,3.0)" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.1" />
Expand Down
12 changes: 4 additions & 8 deletions SharedAssets.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using AsyncKeyedLock;
using Microsoft.Extensions.Caching.Memory;
using SkiaSharp;

Expand All @@ -7,7 +8,7 @@ namespace EasyFortniteStats_ImageApi;
public class SharedAssets(IMemoryCache memoryCache)
{
private static readonly MemoryCacheEntryOptions CacheOptions = new() { Priority = CacheItemPriority.NeverRemove };
private static readonly SemaphoreSlim Semaphore = new(1);
private static readonly AsyncNonKeyedLocker Semaphore = new(1);

public async ValueTask<SKBitmap?> GetBitmap(string format, string? arg1)
{
Expand All @@ -24,26 +25,23 @@ public class SharedAssets(IMemoryCache memoryCache)
var cached = memoryCache.Get<SKBitmap?>(key);
if (cached is not null) return cached;

await Semaphore.WaitAsync();
using var _ = await Semaphore.LockAsync();

cached = memoryCache.Get<SKBitmap?>(key);
if (cached is not null)
{
Semaphore.Release();
return cached;
}

if (!File.Exists(path))
{
memoryCache.Set(key, (SKBitmap?)null, CacheOptions);
Semaphore.Release();
return null;
}

using var data = await ReadToSkData(path);
var bitmap = SKBitmap.Decode(data);
memoryCache.Set(key, bitmap, CacheOptions);
Semaphore.Release();
return bitmap;
}

Expand All @@ -53,19 +51,17 @@ public async ValueTask<SKTypeface> GetFont(string path)
var cached = memoryCache.Get<SKTypeface>(key);
if (cached is not null) return cached;

await Semaphore.WaitAsync();
using var _ = await Semaphore.LockAsync();

cached = memoryCache.Get<SKTypeface>(key);
if (cached is not null)
{
Semaphore.Release();
return cached;
}

using var data = await ReadToSkData(path);
var typeface = SKTypeface.FromData(data);
memoryCache.Set(key, typeface, CacheOptions);
Semaphore.Release();
return typeface;
}

Expand Down