From e847c02f4169fc2e64faa306a3f3b4d56cc9c797 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 31 Aug 2016 06:58:55 +0200 Subject: [PATCH] Done v+t, using local cache to avoid querying db every user update --- .../Commands/VoicePlusTextCommands.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs index e1055c10..a3166161 100644 --- a/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/VoicePlusTextCommands.cs @@ -5,6 +5,7 @@ using NadekoBot.Attributes; using NadekoBot.Extensions; using NadekoBot.Services; using System; +using System.Collections.Concurrent; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -17,9 +18,10 @@ namespace NadekoBot.Modules.Administration public class VoicePlusTextCommands { Regex channelNameRegex = new Regex(@"[^a-zA-Z0-9 -]", RegexOptions.Compiled); + //guildid/voiceplustextenabled + private ConcurrentDictionary voicePlusTextCache; public VoicePlusTextCommands() { - // changing servers may cause bugs NadekoBot.Client.UserUpdated += UserUpdatedEventHandler; } @@ -33,12 +35,11 @@ namespace NadekoBot.Modules.Administration { if (before.VoiceChannel == after.VoiceChannel) return; - //todo This is WAY TOO MUCH - using (var uow = DbHandler.UnitOfWork()) - { - if (!uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled) - return; - } + bool isEnabled; + voicePlusTextCache.TryGetValue(guild.Id, out isEnabled); + if (!isEnabled) + return; + if (!botUserPerms.ManageChannels || !botUserPerms.ManageRoles) { try @@ -51,6 +52,7 @@ namespace NadekoBot.Modules.Administration using (var uow = DbHandler.UnitOfWork()) { uow.GuildConfigs.For(before.Guild.Id).VoicePlusTextEnabled = false; + voicePlusTextCache.TryUpdate(guild.Id, false, true); } return; } @@ -117,6 +119,7 @@ namespace NadekoBot.Modules.Administration var conf = uow.GuildConfigs.For(guild.Id); isEnabled = conf.VoicePlusTextEnabled = !conf.VoicePlusTextEnabled; } + voicePlusTextCache.AddOrUpdate(guild.Id, isEnabled, (id, val) => isEnabled); if (isEnabled) { foreach (var textChannel in guild.GetTextChannels().Where(c => c.Name.EndsWith("-voice")))