From 86805bafa9b5d846784554bdcd0de6195ae79f1c Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Fri, 10 Nov 2017 13:35:39 +0100 Subject: [PATCH] Fixed .crypto errors? Closes #1816 --- .../Searches/Services/SearchesService.cs | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs b/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs index 6876dfea..2755cfb1 100644 --- a/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs +++ b/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs @@ -53,10 +53,28 @@ namespace NadekoBot.Modules.Searches.Services private readonly ConcurrentDictionary> _blacklistedTags = new ConcurrentDictionary>(); private readonly Timer _t; + private readonly SemaphoreSlim _cryptoLock = new SemaphoreSlim(1, 1); public async Task CryptoData() { - var data = await _cache.Redis.GetDatabase() - .StringGetAsync("crypto_data").ConfigureAwait(false); + string data; + var r = _cache.Redis.GetDatabase(); + await _cryptoLock.WaitAsync().ConfigureAwait(false); + try + { + data = await r.StringGetAsync("crypto_data").ConfigureAwait(false); + + if (data == null) + { + data = await Http.GetStringAsync("https://api.coinmarketcap.com/v1/ticker/") + .ConfigureAwait(false); + + await r.StringSetAsync("crypto_data", data, TimeSpan.FromHours(6)).ConfigureAwait(false); + } + } + finally + { + _cryptoLock.Release(); + } return JsonConvert.DeserializeObject(data); } @@ -121,14 +139,7 @@ namespace NadekoBot.Modules.Searches.Services var r = _cache.Redis.GetDatabase(); try { - var data = (string)(await r.StringGetAsync("crypto_data").ConfigureAwait(false)); - if (data == null) - { - data = await Http.GetStringAsync("https://api.coinmarketcap.com/v1/ticker/") - .ConfigureAwait(false); - - await r.StringSetAsync("crypto_data", data, TimeSpan.FromHours(6)).ConfigureAwait(false); - } + } catch (Exception ex) {