diff options
author | Linnea Gräf <nea@nea.moe> | 2024-08-11 22:10:30 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-08-11 22:10:30 +0200 |
commit | 84cfa163f938089c72d9c1d089c136e05e052abe (patch) | |
tree | 506fe8ac7ee5db97d8423a0e55aed02f471bd1e9 /MediuxDownloader.cs | |
parent | f60e3645e18a7f590dc913d0f555fcb82f072d07 (diff) | |
download | JCoverXtremePro-84cfa163f938089c72d9c1d089c136e05e052abe.tar.gz JCoverXtremePro-84cfa163f938089c72d9c1d089c136e05e052abe.tar.bz2 JCoverXtremePro-84cfa163f938089c72d9c1d089c136e05e052abe.zip |
Diffstat (limited to 'MediuxDownloader.cs')
-rw-r--r-- | MediuxDownloader.cs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/MediuxDownloader.cs b/MediuxDownloader.cs index a027773..c7c415d 100644 --- a/MediuxDownloader.cs +++ b/MediuxDownloader.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -71,11 +73,31 @@ public class MediuxDownloader .Content.ReadAsStringAsync().ConfigureAwait(false); } + private ConcurrentDictionary<string, SemaphoreSlim> cacheLock = new(); + private ConcurrentDictionary<string, JsonNode> cache = new(); + public async Task<JsonNode> GetMediuxMetadata(string url) { - Plugin.Logger.LogInformation("Loading data from {Url}", url); - var text = await GetString(url).ConfigureAwait(false); - return ExtractJsonNodes(text).First(); + var semaphore = cacheLock.GetOrAdd(url, ignored => new SemaphoreSlim(1)); + try + { + await semaphore.WaitAsync().ConfigureAwait(false); + if (cache.TryGetValue(url, out var data)) + { + Plugin.Logger.LogInformation("Loading cached data from {Url}", url); + return data; + } + + Plugin.Logger.LogInformation("Loading data from {Url}", url); + var text = await GetString(url).ConfigureAwait(false); + var node = ExtractJsonNodes(text).First(); + cache[url] = node; + return node; + } + finally + { + semaphore.Release(); + } } public async Task<HttpResponseMessage> DownloadFile(string url) |