Abstract away redis even more with AlmostRedis.cs

This commit is contained in:
Runebaas 2018-05-26 02:33:45 +02:00
parent f53258e348
commit 35f0a5c8f8
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
10 changed files with 153 additions and 78 deletions

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using Geekbot.net.Lib.Logger;
using StackExchange.Redis;
namespace Geekbot.net.Lib.AlmostRedis
{
// if anyone ever sees this, please come up with a better fucking name, i'd appriciate it
public class AlmostRedis : IAlmostRedis
{
private readonly GeekbotLogger _logger;
private readonly RunParameters _runParameters;
private IDatabase _database;
private ConnectionMultiplexer _connection;
private IAlmostRedis _almostRedisImplementation;
public AlmostRedis(GeekbotLogger logger, RunParameters runParameters)
{
_logger = logger;
_runParameters = runParameters;
}
public void Connect()
{
_connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}");
_database = _connection.GetDatabase(int.Parse(_runParameters.RedisDatabase));
_logger.Information(LogSource.Redis, $"Connected to Redis on {_connection.Configuration} at {_database.Database}");
}
public IDatabase Db
{
get { return _database; }
}
public ConnectionMultiplexer Connection
{
get { return _connection; }
}
public IEnumerable<RedisKey> GetAllKeys()
{
return _connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys();
}
}
}

View file

@ -0,0 +1,13 @@
using System.Collections.Generic;
using StackExchange.Redis;
namespace Geekbot.net.Lib.AlmostRedis
{
public interface IAlmostRedis
{
void Connect();
IDatabase Db { get; }
ConnectionMultiplexer Connection { get; }
IEnumerable<RedisKey> GetAllKeys();
}
}

View file

@ -4,24 +4,26 @@ using System.IO;
using System.Linq;
using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Logger;
using StackExchange.Redis;
using Utf8Json;
namespace Geekbot.net.Lib.Localization
{
public class TranslationHandler : ITranslationHandler
{
private readonly DatabaseContext _database;
private readonly IGeekbotLogger _logger;
private readonly IDatabase _redis;
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
private Dictionary<ulong, string> _serverLanguages;
private List<string> _supportedLanguages;
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, IDatabase redis, IGeekbotLogger logger)
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, DatabaseContext database, IGeekbotLogger logger)
{
_database = database;
_logger = logger;
_redis = redis;
_logger.Information(LogSource.Geekbot, "Loading Translations");
LoadTranslations();
LoadServerLanguages(clientGuilds);
@ -81,14 +83,16 @@ namespace Geekbot.net.Lib.Localization
_serverLanguages = new Dictionary<ulong, string>();
foreach (var guild in clientGuilds)
{
var language = _redis.HashGet($"{guild.Id}:Settings", "Language");
var language = _database.GuildSettings
.FirstOrDefault(g => g.GuildId.Equals(guild.Id.AsLong()))
?.Language ?? "EN";
if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language))
{
_serverLanguages[guild.Id] = "EN";
}
else
{
_serverLanguages[guild.Id] = language.ToString();
_serverLanguages[guild.Id] = language;
}
}
}
@ -137,7 +141,9 @@ namespace Geekbot.net.Lib.Localization
try
{
if (!_supportedLanguages.Contains(language)) return false;
_redis.HashSet($"{guildId}:Settings", new[]{ new HashEntry("Language", language) });
var guild = GetGuild(guildId);
guild.Language = language;
_database.GuildSettings.Update(guild);
_serverLanguages[guildId] = language;
return true;
}
@ -152,5 +158,17 @@ namespace Geekbot.net.Lib.Localization
{
return _supportedLanguages;
}
private GuildSettingsModel GetGuild(ulong guildId)
{
var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
if (guild != null) return guild;
_database.GuildSettings.Add(new GuildSettingsModel
{
GuildId = guildId.AsLong()
});
_database.SaveChanges();
return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
}
}
}