Fixed images not automatically reloading on startup if the keys don't exist

This commit is contained in:
Kwoth
2021-09-17 17:10:38 +02:00
parent 3470762b30
commit e70a91ae60
5 changed files with 37 additions and 41 deletions

View File

@@ -17,6 +17,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- Timer should no longer increase on some repeaters - Timer should no longer increase on some repeaters
- Repeaters should no longer have periods when they're missing from the list - Repeaters should no longer have periods when they're missing from the list
- Fixed several commands which used error color for success confirmation messages - Fixed several commands which used error color for success confirmation messages
- Fixed images not automatically reloading on startup if the keys don't exist
## [3.0.3] - 15.09.2021 ## [3.0.3] - 15.09.2021

View File

@@ -102,7 +102,6 @@ namespace NadekoBot
.AddSingleton(Client) // discord socket client .AddSingleton(Client) // discord socket client
.AddSingleton(_commandService) .AddSingleton(_commandService)
.AddSingleton(this) .AddSingleton(this)
.AddSingleton<IDataCache, RedisCache>()
.AddSingleton<ISeria, JsonSeria>() .AddSingleton<ISeria, JsonSeria>()
.AddSingleton<IPubSub, RedisPubSub>() .AddSingleton<IPubSub, RedisPubSub>()
.AddSingleton<IConfigSeria, YamlSeria>() .AddSingleton<IConfigSeria, YamlSeria>()
@@ -132,10 +131,18 @@ namespace NadekoBot
} }
else else
{ {
svcs.AddSingleton<ICoordinator, RemoteGrpcCoordinator>() svcs.AddSingleton<RemoteGrpcCoordinator>()
.AddSingleton<IReadyExecutor>(x => (IReadyExecutor)x.GetRequiredService<ICoordinator>()); .AddSingleton<ICoordinator>(x => x.GetRequiredService<RemoteGrpcCoordinator>())
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RemoteGrpcCoordinator>());
} }
svcs.AddSingleton<RedisLocalDataCache>()
.AddSingleton<ILocalDataCache>(x => x.GetRequiredService<RedisLocalDataCache>())
.AddSingleton<RedisImagesCache>()
.AddSingleton<IImageCache>(x => x.GetRequiredService<RedisImagesCache>())
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RedisImagesCache>())
.AddSingleton<IDataCache, RedisCache>();
svcs.Scan(scan => scan svcs.Scan(scan => scan
.FromAssemblyOf<IReadyExecutor>() .FromAssemblyOf<IReadyExecutor>()
.AddClasses(classes => classes.AssignableToAny( .AddClasses(classes => classes.AssignableToAny(

View File

@@ -11,11 +11,12 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using NadekoBot.Common.ModuleBehaviors;
using Serilog; using Serilog;
namespace NadekoBot.Services namespace NadekoBot.Services
{ {
public sealed class RedisImagesCache : IImageCache public sealed class RedisImagesCache : IImageCache, IReadyExecutor
{ {
private readonly ConnectionMultiplexer _con; private readonly ConnectionMultiplexer _con;
private readonly IBotCredentials _creds; private readonly IBotCredentials _creds;
@@ -73,6 +74,14 @@ namespace NadekoBot.Services
Currency, Currency,
} }
public async Task OnReadyAsync()
{
if (await AllKeysExist())
return;
await Reload();
}
public RedisImagesCache(ConnectionMultiplexer con, IBotCredentials creds) public RedisImagesCache(ConnectionMultiplexer con, IBotCredentials creds)
{ {
_con = con; _con = con;

View File

@@ -5,7 +5,6 @@ using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using Discord.WebSocket; using Discord.WebSocket;
namespace NadekoBot.Services namespace NadekoBot.Services
@@ -20,12 +19,13 @@ namespace NadekoBot.Services
private readonly string _redisKey; private readonly string _redisKey;
private readonly EndPoint _redisEndpoint; private readonly EndPoint _redisEndpoint;
public RedisCache(ConnectionMultiplexer redis, IBotCredentials creds, DiscordSocketClient client) public RedisCache(ConnectionMultiplexer redis, IBotCredentials creds,
IImageCache imageCache, ILocalDataCache dataCache)
{ {
Redis = redis; Redis = redis;
_redisEndpoint = Redis.GetEndPoints().First(); _redisEndpoint = Redis.GetEndPoints().First();
LocalImages = new RedisImagesCache(Redis, creds); LocalImages = imageCache;
LocalData = new RedisLocalDataCache(Redis, creds, client.ShardId); LocalData = dataCache;
_redisKey = creds.RedisKey(); _redisKey = creds.RedisKey();
} }

View File

@@ -7,6 +7,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Discord;
using Discord.WebSocket;
using Serilog; using Serilog;
namespace NadekoBot.Services namespace NadekoBot.Services
@@ -25,56 +27,33 @@ namespace NadekoBot.Services
public IReadOnlyDictionary<string, SearchPokemon> Pokemons public IReadOnlyDictionary<string, SearchPokemon> Pokemons
{ {
get get => Get<Dictionary<string, SearchPokemon>>("pokemon_list");
{ private set => Set("pokemon_list", value);
return Get<Dictionary<string, SearchPokemon>>("pokemon_list");
}
private set
{
Set("pokemon_list", value);
}
} }
public IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities public IReadOnlyDictionary<string, SearchPokemonAbility> PokemonAbilities
{ {
get get => Get<Dictionary<string, SearchPokemonAbility>>("pokemon_abilities");
{ private set => Set("pokemon_abilities", value);
return Get<Dictionary<string, SearchPokemonAbility>>("pokemon_abilities");
}
private set
{
Set("pokemon_abilities", value);
}
} }
public TriviaQuestion[] TriviaQuestions public TriviaQuestion[] TriviaQuestions
{ {
get get => Get<TriviaQuestion[]>("trivia_questions");
{ private set => Set("trivia_questions", value);
return Get<TriviaQuestion[]>("trivia_questions");
}
private set
{
Set("trivia_questions", value);
}
} }
public IReadOnlyDictionary<int, string> PokemonMap public IReadOnlyDictionary<int, string> PokemonMap
{ {
get get => Get<Dictionary<int, string>>("pokemon_map");
{ private set => Set("pokemon_map", value);
return Get<Dictionary<int, string>>("pokemon_map");
}
private set
{
Set("pokemon_map", value);
}
} }
public RedisLocalDataCache(ConnectionMultiplexer con, IBotCredentials creds, int shardId) public RedisLocalDataCache(ConnectionMultiplexer con, IBotCredentials creds, DiscordSocketClient client)
{ {
_con = con; _con = con;
_creds = creds; _creds = creds;
var shardId = client.ShardId;
if (shardId == 0) if (shardId == 0)
{ {