trivia questions and pokemon data will be stored in redis, instead of per-shard
This commit is contained in:
@ -8,6 +8,7 @@ namespace NadekoBot.Core.Services
|
||||
{
|
||||
ConnectionMultiplexer Redis { get; }
|
||||
IImageCache LocalImages { get; }
|
||||
ILocalDataCache LocalData { get; }
|
||||
|
||||
Task<(bool Success, byte[] Data)> TryGetImageDataAsync(string key);
|
||||
Task<(bool Success, string Data)> TryGetAnimeDataAsync(string key);
|
||||
|
14
NadekoBot.Core/Services/ILocalDataCache.cs
Normal file
14
NadekoBot.Core/Services/ILocalDataCache.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using NadekoBot.Core.Common.Pokemon;
|
||||
using NadekoBot.Modules.Games.Common.Trivia;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NadekoBot.Core.Services
|
||||
{
|
||||
public interface ILocalDataCache
|
||||
{
|
||||
IReadOnlyDictionary<string, SearchPokemon> Pokemons { get; }
|
||||
IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities { get; }
|
||||
TriviaQuestion[] TriviaQuestions { get; }
|
||||
IReadOnlyDictionary<int, string> PokemonMap { get; }
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
using NadekoBot.Extensions;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Core.Services.Impl
|
||||
@ -11,6 +10,7 @@ namespace NadekoBot.Core.Services.Impl
|
||||
public ConnectionMultiplexer Redis { get; }
|
||||
|
||||
public IImageCache LocalImages { get; }
|
||||
public ILocalDataCache LocalData { get; }
|
||||
|
||||
private readonly IDatabase _db;
|
||||
private readonly string _redisKey;
|
||||
@ -20,6 +20,7 @@ namespace NadekoBot.Core.Services.Impl
|
||||
Redis = ConnectionMultiplexer.Connect("127.0.0.1");
|
||||
Redis.PreserveAsyncOrder = false;
|
||||
LocalImages = new RedisImagesCache(Redis, creds);
|
||||
LocalData = new RedisLocalDataCache(Redis, creds);
|
||||
_db = Redis.GetDatabase();
|
||||
_redisKey = creds.RedisKey();
|
||||
}
|
||||
|
122
NadekoBot.Core/Services/Impl/RedisLocalDataCache.cs
Normal file
122
NadekoBot.Core/Services/Impl/RedisLocalDataCache.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using NadekoBot.Core.Common.Pokemon;
|
||||
using NadekoBot.Extensions;
|
||||
using NadekoBot.Modules.Games.Common.Trivia;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace NadekoBot.Core.Services.Impl
|
||||
{
|
||||
public class RedisLocalDataCache : ILocalDataCache
|
||||
{
|
||||
private readonly ConnectionMultiplexer _con;
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly Logger _log;
|
||||
|
||||
private IDatabase _db => _con.GetDatabase();
|
||||
|
||||
private const string pokemonAbilitiesFile = "data/pokemon/pokemon_abilities7.json";
|
||||
private const string pokemonListFile = "data/pokemon/pokemon_list7.json";
|
||||
private const string pokemonMapPath = "data/pokemon/name-id_map4.json";
|
||||
private const string questionsFile = "data/trivia_questions.json";
|
||||
|
||||
public IReadOnlyDictionary<string, SearchPokemon> Pokemons
|
||||
{
|
||||
get
|
||||
{
|
||||
return Get<Dictionary<string, SearchPokemon>>("pokemon_list");
|
||||
}
|
||||
private set
|
||||
{
|
||||
Set("pokemon_list", value);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities
|
||||
{
|
||||
get
|
||||
{
|
||||
return Get<Dictionary<string, SearchPokemonAbility>>("pokemon_abilities");
|
||||
}
|
||||
private set
|
||||
{
|
||||
Set("pokemon_abilities", value);
|
||||
}
|
||||
}
|
||||
|
||||
public TriviaQuestion[] TriviaQuestions
|
||||
{
|
||||
get
|
||||
{
|
||||
return Get<TriviaQuestion[]>("trivia_questions");
|
||||
}
|
||||
private set
|
||||
{
|
||||
Set("trivia_questions", value);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<int, string> PokemonMap
|
||||
{
|
||||
get
|
||||
{
|
||||
return Get<Dictionary<int, string>>("pokemon_map");
|
||||
}
|
||||
private set
|
||||
{
|
||||
Set("pokemon_map", value);
|
||||
}
|
||||
}
|
||||
|
||||
public RedisLocalDataCache(ConnectionMultiplexer con, IBotCredentials creds)
|
||||
{
|
||||
_con = con;
|
||||
_creds = creds;
|
||||
_log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
if (!File.Exists(pokemonListFile))
|
||||
{
|
||||
_log.Warn(pokemonListFile + " is missing. Pokemon abilities not loaded.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Pokemons = JsonConvert.DeserializeObject<Dictionary<string, SearchPokemon>>(File.ReadAllText(pokemonListFile));
|
||||
}
|
||||
|
||||
if (!File.Exists(pokemonAbilitiesFile))
|
||||
{
|
||||
_log.Warn(pokemonAbilitiesFile + " is missing. Pokemon abilities not loaded.");
|
||||
}
|
||||
else
|
||||
{
|
||||
PokemonAbilities = JsonConvert.DeserializeObject<Dictionary<string, SearchPokemonAbility>>(File.ReadAllText(pokemonAbilitiesFile));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TriviaQuestions = JsonConvert.DeserializeObject<TriviaQuestion[]>(File.ReadAllText(questionsFile));
|
||||
PokemonMap = JsonConvert.DeserializeObject<PokemonNameId[]>(File.ReadAllText(pokemonMapPath))
|
||||
.ToDictionary(x => x.Id, x => x.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private T Get<T>(string key) where T : class
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(_db.StringGet($"{_creds.RedisKey()}_localdata_{key}"));
|
||||
}
|
||||
|
||||
private void Set(string key, object obj)
|
||||
{
|
||||
_db.StringSet($"{_creds.RedisKey()}_localdata_{key}", JsonConvert.SerializeObject(obj));
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ namespace NadekoBot.Core.Services.Impl
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly DateTime _started;
|
||||
|
||||
public const string BotVersion = "2.3.5";
|
||||
public const string BotVersion = "2.3.6";
|
||||
|
||||
public string Author => "Kwoth#2560";
|
||||
public string Library => "Discord.Net";
|
||||
|
Reference in New Issue
Block a user