Caching anime and manga seraches. Club disband error message fixed.
This commit is contained in:
parent
25258a0c61
commit
16fd835d4b
@ -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<AnimeResult> 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<AnimeResult>(res);
|
||||
data = await _http.GetStringAsync(link).ConfigureAwait(false);
|
||||
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
return JsonConvert.DeserializeObject<AnimeResult>(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<MangaResult>(res);
|
||||
data = await _http.GetStringAsync(link).ConfigureAwait(false);
|
||||
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
return JsonConvert.DeserializeObject<MangaResult>(data);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@ -282,7 +282,7 @@ namespace NadekoBot.Modules.Xp
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyErrorLocalized("club_disaband_error").ConfigureAwait(false);
|
||||
await ReplyErrorLocalized("club_disband_error").ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user