From 84652d6e3d393c00c94cb058b8b1460de8e4c0d6 Mon Sep 17 00:00:00 2001 From: alyssa Date: Thu, 17 Oct 2024 01:55:35 +0900 Subject: [PATCH] feat(bot): config key to toggle local cache lookups --- Myriad/Cache/HTTPDiscordCache.cs | 34 ++++++++++++++++++-------------- PluralKit.Bot/BotConfig.cs | 1 + PluralKit.Bot/Modules.cs | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Myriad/Cache/HTTPDiscordCache.cs b/Myriad/Cache/HTTPDiscordCache.cs index 29f0f014..c27fc349 100644 --- a/Myriad/Cache/HTTPDiscordCache.cs +++ b/Myriad/Cache/HTTPDiscordCache.cs @@ -21,7 +21,7 @@ public class HttpDiscordCache: IDiscordCache public EventHandler<(bool?, string)> OnDebug; - public HttpDiscordCache(ILogger logger, HttpClient client, string cacheEndpoint, int shardCount, ulong ownUserId) + public HttpDiscordCache(ILogger logger, HttpClient client, string cacheEndpoint, int shardCount, ulong ownUserId, bool useInnerCache) { _logger = logger; _client = client; @@ -29,19 +29,19 @@ public class HttpDiscordCache: IDiscordCache _shardCount = shardCount; _ownUserId = ownUserId; _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad(); - _innerCache = new MemoryDiscordCache(ownUserId); + if (useInnerCache) _innerCache = new MemoryDiscordCache(ownUserId); } - public ValueTask SaveGuild(Guild guild) => _innerCache.SaveGuild(guild); - public ValueTask SaveChannel(Channel channel) => _innerCache.SaveChannel(channel); + public ValueTask SaveGuild(Guild guild) => _innerCache?.SaveGuild(guild) ?? default; + public ValueTask SaveChannel(Channel channel) => _innerCache?.SaveChannel(channel) ?? default; public ValueTask SaveUser(User user) => default; - public ValueTask SaveSelfMember(ulong guildId, GuildMemberPartial member) => _innerCache.SaveSelfMember(guildId, member); - public ValueTask SaveRole(ulong guildId, Myriad.Types.Role role) => _innerCache.SaveRole(guildId, role); - public ValueTask SaveDmChannelStub(ulong channelId) => _innerCache.SaveDmChannelStub(channelId); - public ValueTask RemoveGuild(ulong guildId) => _innerCache.RemoveGuild(guildId); - public ValueTask RemoveChannel(ulong channelId) => _innerCache.RemoveChannel(channelId); - public ValueTask RemoveUser(ulong userId) => _innerCache.RemoveUser(userId); - public ValueTask RemoveRole(ulong guildId, ulong roleId) => _innerCache.RemoveRole(guildId, roleId); + public ValueTask SaveSelfMember(ulong guildId, GuildMemberPartial member) => _innerCache?.SaveSelfMember(guildId, member) ?? default; + public ValueTask SaveRole(ulong guildId, Myriad.Types.Role role) => _innerCache?.SaveRole(guildId, role) ?? default; + public ValueTask SaveDmChannelStub(ulong channelId) => _innerCache?.SaveDmChannelStub(channelId) ?? default; + public ValueTask RemoveGuild(ulong guildId) => _innerCache?.RemoveGuild(guildId) ?? default; + public ValueTask RemoveChannel(ulong channelId) => _innerCache?.RemoveChannel(channelId) ?? default; + public ValueTask RemoveUser(ulong userId) => _innerCache?.RemoveUser(userId) ?? default; + public ValueTask RemoveRole(ulong guildId, ulong roleId) => _innerCache?.RemoveRole(guildId, roleId) ?? default; public ulong GetOwnUser() => _ownUserId; @@ -66,8 +66,9 @@ public class HttpDiscordCache: IDiscordCache public async Task TryGetGuild(ulong guildId) { - var lres = await _innerCache.TryGetGuild(guildId); var hres = await QueryCache($"/guilds/{guildId}", guildId); + if (_innerCache == null) return hres; + var lres = await _innerCache.TryGetGuild(guildId); if (lres == null && hres == null) return null; if (lres == null) @@ -87,8 +88,9 @@ public class HttpDiscordCache: IDiscordCache public async Task TryGetChannel(ulong guildId, ulong channelId) { - var lres = await _innerCache.TryGetChannel(guildId, channelId); var hres = await QueryCache($"/guilds/{guildId}/channels/{channelId}", guildId); + if (_innerCache == null) return hres; + var lres = await _innerCache.TryGetChannel(guildId, channelId); if (lres == null && hres == null) return null; if (lres == null) { @@ -112,8 +114,9 @@ public class HttpDiscordCache: IDiscordCache public async Task TryGetSelfMember(ulong guildId) { - var lres = await _innerCache.TryGetSelfMember(guildId); var hres = await QueryCache($"/guilds/{guildId}/members/@me", guildId); + if (_innerCache == null) return hres; + var lres = await _innerCache.TryGetSelfMember(guildId); if (lres == null && hres == null) return null; if (lres == null) { @@ -163,8 +166,9 @@ public class HttpDiscordCache: IDiscordCache public async Task> GetGuildChannels(ulong guildId) { - var lres = await _innerCache.GetGuildChannels(guildId); var hres = await QueryCache>($"/guilds/{guildId}/channels", guildId); + if (_innerCache == null) return hres; + var lres = await _innerCache.GetGuildChannels(guildId); if (lres == null && hres == null) return null; if (lres == null) { diff --git a/PluralKit.Bot/BotConfig.cs b/PluralKit.Bot/BotConfig.cs index 05fc3943..c5c05191 100644 --- a/PluralKit.Bot/BotConfig.cs +++ b/PluralKit.Bot/BotConfig.cs @@ -22,6 +22,7 @@ public class BotConfig public bool UseRedisRatelimiter { get; set; } = false; public string? HttpCacheUrl { get; set; } + public bool HttpUseInnerCache { get; set; } = false; public string? RedisGatewayUrl { get; set; } diff --git a/PluralKit.Bot/Modules.cs b/PluralKit.Bot/Modules.cs index 11f40a0a..ea790f61 100644 --- a/PluralKit.Bot/Modules.cs +++ b/PluralKit.Bot/Modules.cs @@ -51,7 +51,7 @@ public class BotModule: Module if (botConfig.HttpCacheUrl != null) { var cache = new HttpDiscordCache(c.Resolve(), - c.Resolve(), botConfig.HttpCacheUrl, botConfig.Cluster?.TotalShards ?? 1, botConfig.ClientId); + c.Resolve(), botConfig.HttpCacheUrl, botConfig.Cluster?.TotalShards ?? 1, botConfig.ClientId, botConfig.HttpUseInnerCache); var metrics = c.Resolve();