Files
downkyicore/DownKyi/CustomControl/AsyncImageLoader/Loaders/RamCachedWebImageLoader.cs
yaobiao131 f744d264e0 fix: 优化项目代码
1、修复部分字幕下载问题
2、修复自定义aria2设置出错问题
2025-03-19 21:54:14 +08:00

31 lines
956 B
C#

using System.Collections.Concurrent;
using System.Net.Http;
using System.Threading.Tasks;
using Avalonia.Media.Imaging;
namespace DownKyi.CustomControl.AsyncImageLoader.Loaders;
public class RamCachedWebImageLoader : BaseWebImageLoader
{
private readonly ConcurrentDictionary<string, Task<Bitmap?>> _memoryCache = new();
/// <inheritdoc />
public RamCachedWebImageLoader()
{
}
/// <inheritdoc />
public RamCachedWebImageLoader(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient)
{
}
/// <inheritdoc />
public override async Task<Bitmap?> ProvideImageAsync(string url)
{
var bitmap = await _memoryCache.GetOrAdd(url, LoadAsync).ConfigureAwait(false);
// If load failed - remove from cache and return
// Next load attempt will try to load image again
if (bitmap == null) _memoryCache.TryRemove(url, out _);
return bitmap;
}
}