diff --git a/src/NadekoBot/Modules/Searches/Services/AnimeSearchService.cs b/src/NadekoBot/Modules/Searches/Services/AnimeSearchService.cs index 638db338..1a372d8c 100644 --- a/src/NadekoBot/Modules/Searches/Services/AnimeSearchService.cs +++ b/src/NadekoBot/Modules/Searches/Services/AnimeSearchService.cs @@ -11,10 +11,14 @@ namespace NadekoBot.Modules.Searches.Services public class AnimeSearchService : INService { private readonly Logger _log; + private readonly IDataCache _cache; + private readonly HttpClient _http; - public AnimeSearchService() + public AnimeSearchService(IDataCache cache) { _log = LogManager.GetCurrentClassLogger(); + _cache = cache; + _http = new HttpClient(); } public async Task GetAnimeData(string query) @@ -25,11 +29,16 @@ namespace NadekoBot.Modules.Searches.Services { var link = "https://aniapi.nadekobot.me/anime/" + Uri.EscapeDataString(query.Replace("/", " ")); - using (var http = new HttpClient()) + link = link.ToLowerInvariant(); + var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false); + if (!ok) { - var res = await http.GetStringAsync(link).ConfigureAwait(false); - return JsonConvert.DeserializeObject(res); + data = await _http.GetStringAsync(link).ConfigureAwait(false); + await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false); } + + + return JsonConvert.DeserializeObject(data); } catch { @@ -44,12 +53,17 @@ namespace NadekoBot.Modules.Searches.Services try { - var link = "https://aniapi.nadekobot.me/manga/" + Uri.EscapeDataString(query.Replace("/", " ")); - using (var http = new HttpClient()) + var link = "https://aniapi.nadekobot.me/manga/" + Uri.EscapeDataString(query.Replace("/", " ")); + link = link.ToLowerInvariant(); + var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false); + if (!ok) { - var res = await http.GetStringAsync(link).ConfigureAwait(false); - return JsonConvert.DeserializeObject(res); + data = await _http.GetStringAsync(link).ConfigureAwait(false); + await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false); } + + + return JsonConvert.DeserializeObject(data); } catch { diff --git a/src/NadekoBot/Modules/Xp/Club.cs b/src/NadekoBot/Modules/Xp/Club.cs index a911ab4f..8c4ce4c5 100644 --- a/src/NadekoBot/Modules/Xp/Club.cs +++ b/src/NadekoBot/Modules/Xp/Club.cs @@ -282,7 +282,7 @@ namespace NadekoBot.Modules.Xp } else { - await ReplyErrorLocalized("club_disaband_error").ConfigureAwait(false); + await ReplyErrorLocalized("club_disband_error").ConfigureAwait(false); } } diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 5fd01eba..95fd9574 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -242,13 +242,6 @@ namespace NadekoBot #if GLOBAL_NADEKO isPublicNadeko = true; #endif - //_log.Info(string.Join(", ", CommandService.Commands - // .Distinct(x => x.Name + x.Module.Name) - // .SelectMany(x => x.Aliases) - // .GroupBy(x => x) - // .Where(x => x.Count() > 1) - // .Select(x => x.Key + $"({x.Count()})"))); - //unload modules which are not available on the public bot if(isPublicNadeko) diff --git a/src/NadekoBot/Services/IDataCache.cs b/src/NadekoBot/Services/IDataCache.cs index b708a66b..3be7ad35 100644 --- a/src/NadekoBot/Services/IDataCache.cs +++ b/src/NadekoBot/Services/IDataCache.cs @@ -11,6 +11,8 @@ namespace NadekoBot.Services { ConnectionMultiplexer Redis { get; } Task<(bool Success, byte[] Data)> TryGetImageDataAsync(string key); + Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key); Task SetImageDataAsync(string key, byte[] data); + Task SetAnimeDataAsync(string link, string data); } } diff --git a/src/NadekoBot/Services/Impl/RedisCache.cs b/src/NadekoBot/Services/Impl/RedisCache.cs index 716b46ea..84083ef8 100644 --- a/src/NadekoBot/Services/Impl/RedisCache.cs +++ b/src/NadekoBot/Services/Impl/RedisCache.cs @@ -25,5 +25,16 @@ namespace NadekoBot.Services.Impl { return _db.StringSetAsync("image_" + key, data); } + + public async Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key) + { + string x = await _db.StringGetAsync("anime_" + key); + return (x != null, x); + } + + public Task SetAnimeDataAsync(string key, string data) + { + return _db.StringSetAsync("anime_" + key, data); + } } }