feat(bot): remove cluster-local DM channel cache
This commit is contained in:
parent
f0b5749d5c
commit
65e2bb0234
@ -22,13 +22,6 @@ public static class BotMetrics
|
|||||||
Context = "Bot"
|
Context = "Bot"
|
||||||
};
|
};
|
||||||
|
|
||||||
public static MeterOptions LocalDMCacheHits => new()
|
|
||||||
{
|
|
||||||
Name = "Cluster local DM Cache Hits",
|
|
||||||
MeasurementUnit = Unit.Calls,
|
|
||||||
Context = "Bot"
|
|
||||||
};
|
|
||||||
|
|
||||||
public static MeterOptions DatabaseDMCacheHits => new()
|
public static MeterOptions DatabaseDMCacheHits => new()
|
||||||
{
|
{
|
||||||
Name = "Database DM Cache Hits",
|
Name = "Database DM Cache Hits",
|
||||||
|
@ -11,8 +11,6 @@ namespace PluralKit.Bot;
|
|||||||
|
|
||||||
public class PrivateChannelService
|
public class PrivateChannelService
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<ulong, ulong> _channelsCache = new();
|
|
||||||
|
|
||||||
private readonly IMetrics _metrics;
|
private readonly IMetrics _metrics;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly ModelRepository _repo;
|
private readonly ModelRepository _repo;
|
||||||
@ -27,42 +25,32 @@ public class PrivateChannelService
|
|||||||
|
|
||||||
public async Task TrySavePrivateChannel(MessageCreateEvent evt)
|
public async Task TrySavePrivateChannel(MessageCreateEvent evt)
|
||||||
{
|
{
|
||||||
if (evt.GuildId != null) return;
|
if (evt.GuildId == null) await SaveDmChannel(evt.Author.Id, evt.ChannelId);
|
||||||
if (_channelsCache.TryGetValue(evt.Author.Id, out _)) return;
|
|
||||||
|
|
||||||
await SaveDmChannel(evt.Author.Id, evt.ChannelId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ulong> GetOrCreateDmChannel(ulong userId)
|
public async Task<ulong> GetOrCreateDmChannel(ulong userId)
|
||||||
{
|
{
|
||||||
if (_channelsCache.TryGetValue(userId, out var cachedChannelId))
|
|
||||||
{
|
|
||||||
_metrics.Measure.Meter.Mark(BotMetrics.LocalDMCacheHits);
|
|
||||||
return cachedChannelId;
|
|
||||||
}
|
|
||||||
|
|
||||||
var channelId = await _repo.GetDmChannel(userId);
|
var channelId = await _repo.GetDmChannel(userId);
|
||||||
if (channelId == null)
|
if (channelId != null)
|
||||||
{
|
{
|
||||||
_metrics.Measure.Meter.Mark(BotMetrics.DMCacheMisses);
|
_metrics.Measure.Meter.Mark(BotMetrics.DatabaseDMCacheHits);
|
||||||
var channel = await _rest.CreateDm(userId);
|
return channelId.Value;
|
||||||
channelId = channel.Id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_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
|
// 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, channel.Id);
|
||||||
_ = SaveDmChannel(userId, channelId.Value);
|
|
||||||
|
|
||||||
return channelId.Value;
|
return channel.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SaveDmChannel(ulong userId, ulong channelId)
|
private async Task SaveDmChannel(ulong userId, ulong channelId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_channelsCache.Add(userId, channelId);
|
|
||||||
await _repo.UpdateAccount(userId, new() { DmChannel = channelId });
|
await _repo.UpdateAccount(userId, new() { DmChannel = channelId });
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
Loading…
Reference in New Issue
Block a user