From 65e2bb02346cc7e36a350af31aa9ed24b472ef39 Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 14 Jun 2022 23:11:55 -0400 Subject: [PATCH] feat(bot): remove cluster-local DM channel cache --- PluralKit.Bot/BotMetrics.cs | 7 ----- .../Services/PrivateChannelService.cs | 30 ++++++------------- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/PluralKit.Bot/BotMetrics.cs b/PluralKit.Bot/BotMetrics.cs index 94dd0f6e..23eb324e 100644 --- a/PluralKit.Bot/BotMetrics.cs +++ b/PluralKit.Bot/BotMetrics.cs @@ -22,13 +22,6 @@ public static class BotMetrics Context = "Bot" }; - public static MeterOptions LocalDMCacheHits => new() - { - Name = "Cluster local DM Cache Hits", - MeasurementUnit = Unit.Calls, - Context = "Bot" - }; - public static MeterOptions DatabaseDMCacheHits => new() { Name = "Database DM Cache Hits", diff --git a/PluralKit.Bot/Services/PrivateChannelService.cs b/PluralKit.Bot/Services/PrivateChannelService.cs index 3c2781e7..dc1ea03a 100644 --- a/PluralKit.Bot/Services/PrivateChannelService.cs +++ b/PluralKit.Bot/Services/PrivateChannelService.cs @@ -11,8 +11,6 @@ namespace PluralKit.Bot; public class PrivateChannelService { - private static readonly Dictionary _channelsCache = new(); - private readonly IMetrics _metrics; private readonly ILogger _logger; private readonly ModelRepository _repo; @@ -27,42 +25,32 @@ public class PrivateChannelService public async Task TrySavePrivateChannel(MessageCreateEvent evt) { - if (evt.GuildId != null) return; - if (_channelsCache.TryGetValue(evt.Author.Id, out _)) return; - - await SaveDmChannel(evt.Author.Id, evt.ChannelId); + if (evt.GuildId == null) await SaveDmChannel(evt.Author.Id, evt.ChannelId); } public async Task GetOrCreateDmChannel(ulong userId) { - if (_channelsCache.TryGetValue(userId, out var cachedChannelId)) - { - _metrics.Measure.Meter.Mark(BotMetrics.LocalDMCacheHits); - return cachedChannelId; - } - var channelId = await _repo.GetDmChannel(userId); - if (channelId == null) + if (channelId != null) { - _metrics.Measure.Meter.Mark(BotMetrics.DMCacheMisses); - var channel = await _rest.CreateDm(userId); - channelId = channel.Id; + _metrics.Measure.Meter.Mark(BotMetrics.DatabaseDMCacheHits); + return channelId.Value; } - _metrics.Measure.Meter.Mark(BotMetrics.DatabaseDMCacheHits); + _metrics.Measure.Meter.Mark(BotMetrics.DMCacheMisses); + + var channel = await _rest.CreateDm(userId); // spawn off saving the channel as to not block the current thread - // todo: don't save to database again if we just fetched it from there - _ = SaveDmChannel(userId, channelId.Value); + _ = SaveDmChannel(userId, channel.Id); - return channelId.Value; + return channel.Id; } private async Task SaveDmChannel(ulong userId, ulong channelId) { try { - _channelsCache.Add(userId, channelId); await _repo.UpdateAccount(userId, new() { DmChannel = channelId }); } catch (Exception e)