feat(bot): config key to toggle local cache lookups

This commit is contained in:
alyssa 2024-10-17 01:55:35 +09:00
parent 20ab671f98
commit 84652d6e3d
3 changed files with 21 additions and 16 deletions

View file

@ -21,7 +21,7 @@ public class HttpDiscordCache: IDiscordCache
public EventHandler<(bool?, string)> OnDebug; 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; _logger = logger;
_client = client; _client = client;
@ -29,19 +29,19 @@ public class HttpDiscordCache: IDiscordCache
_shardCount = shardCount; _shardCount = shardCount;
_ownUserId = ownUserId; _ownUserId = ownUserId;
_jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad(); _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad();
_innerCache = new MemoryDiscordCache(ownUserId); if (useInnerCache) _innerCache = new MemoryDiscordCache(ownUserId);
} }
public ValueTask SaveGuild(Guild guild) => _innerCache.SaveGuild(guild); public ValueTask SaveGuild(Guild guild) => _innerCache?.SaveGuild(guild) ?? default;
public ValueTask SaveChannel(Channel channel) => _innerCache.SaveChannel(channel); public ValueTask SaveChannel(Channel channel) => _innerCache?.SaveChannel(channel) ?? default;
public ValueTask SaveUser(User user) => default; public ValueTask SaveUser(User user) => default;
public ValueTask SaveSelfMember(ulong guildId, GuildMemberPartial member) => _innerCache.SaveSelfMember(guildId, member); 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); public ValueTask SaveRole(ulong guildId, Myriad.Types.Role role) => _innerCache?.SaveRole(guildId, role) ?? default;
public ValueTask SaveDmChannelStub(ulong channelId) => _innerCache.SaveDmChannelStub(channelId); public ValueTask SaveDmChannelStub(ulong channelId) => _innerCache?.SaveDmChannelStub(channelId) ?? default;
public ValueTask RemoveGuild(ulong guildId) => _innerCache.RemoveGuild(guildId); public ValueTask RemoveGuild(ulong guildId) => _innerCache?.RemoveGuild(guildId) ?? default;
public ValueTask RemoveChannel(ulong channelId) => _innerCache.RemoveChannel(channelId); public ValueTask RemoveChannel(ulong channelId) => _innerCache?.RemoveChannel(channelId) ?? default;
public ValueTask RemoveUser(ulong userId) => _innerCache.RemoveUser(userId); public ValueTask RemoveUser(ulong userId) => _innerCache?.RemoveUser(userId) ?? default;
public ValueTask RemoveRole(ulong guildId, ulong roleId) => _innerCache.RemoveRole(guildId, roleId); public ValueTask RemoveRole(ulong guildId, ulong roleId) => _innerCache?.RemoveRole(guildId, roleId) ?? default;
public ulong GetOwnUser() => _ownUserId; public ulong GetOwnUser() => _ownUserId;
@ -66,8 +66,9 @@ public class HttpDiscordCache: IDiscordCache
public async Task<Guild?> TryGetGuild(ulong guildId) public async Task<Guild?> TryGetGuild(ulong guildId)
{ {
var lres = await _innerCache.TryGetGuild(guildId);
var hres = await QueryCache<Guild?>($"/guilds/{guildId}", guildId); var hres = await QueryCache<Guild?>($"/guilds/{guildId}", guildId);
if (_innerCache == null) return hres;
var lres = await _innerCache.TryGetGuild(guildId);
if (lres == null && hres == null) return null; if (lres == null && hres == null) return null;
if (lres == null) if (lres == null)
@ -87,8 +88,9 @@ public class HttpDiscordCache: IDiscordCache
public async Task<Channel?> TryGetChannel(ulong guildId, ulong channelId) public async Task<Channel?> TryGetChannel(ulong guildId, ulong channelId)
{ {
var lres = await _innerCache.TryGetChannel(guildId, channelId);
var hres = await QueryCache<Channel?>($"/guilds/{guildId}/channels/{channelId}", guildId); var hres = await QueryCache<Channel?>($"/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 && hres == null) return null;
if (lres == null) if (lres == null)
{ {
@ -112,8 +114,9 @@ public class HttpDiscordCache: IDiscordCache
public async Task<GuildMemberPartial?> TryGetSelfMember(ulong guildId) public async Task<GuildMemberPartial?> TryGetSelfMember(ulong guildId)
{ {
var lres = await _innerCache.TryGetSelfMember(guildId);
var hres = await QueryCache<GuildMemberPartial?>($"/guilds/{guildId}/members/@me", guildId); var hres = await QueryCache<GuildMemberPartial?>($"/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 && hres == null) return null;
if (lres == null) if (lres == null)
{ {
@ -163,8 +166,9 @@ public class HttpDiscordCache: IDiscordCache
public async Task<IEnumerable<Channel>> GetGuildChannels(ulong guildId) public async Task<IEnumerable<Channel>> GetGuildChannels(ulong guildId)
{ {
var lres = await _innerCache.GetGuildChannels(guildId);
var hres = await QueryCache<IEnumerable<Channel>>($"/guilds/{guildId}/channels", guildId); var hres = await QueryCache<IEnumerable<Channel>>($"/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 && hres == null) return null;
if (lres == null) if (lres == null)
{ {

View file

@ -22,6 +22,7 @@ public class BotConfig
public bool UseRedisRatelimiter { get; set; } = false; public bool UseRedisRatelimiter { get; set; } = false;
public string? HttpCacheUrl { get; set; } public string? HttpCacheUrl { get; set; }
public bool HttpUseInnerCache { get; set; } = false;
public string? RedisGatewayUrl { get; set; } public string? RedisGatewayUrl { get; set; }

View file

@ -51,7 +51,7 @@ public class BotModule: Module
if (botConfig.HttpCacheUrl != null) if (botConfig.HttpCacheUrl != null)
{ {
var cache = new HttpDiscordCache(c.Resolve<ILogger>(), var cache = new HttpDiscordCache(c.Resolve<ILogger>(),
c.Resolve<HttpClient>(), botConfig.HttpCacheUrl, botConfig.Cluster?.TotalShards ?? 1, botConfig.ClientId); c.Resolve<HttpClient>(), botConfig.HttpCacheUrl, botConfig.Cluster?.TotalShards ?? 1, botConfig.ClientId, botConfig.HttpUseInnerCache);
var metrics = c.Resolve<IMetrics>(); var metrics = c.Resolve<IMetrics>();