feat: async cache

this breaks logging bot permissions to Sentry.

we haven't had a need to check those recently (permissions issues were because of broken cache), so this is fine for now
this should be re-added in the future though
This commit is contained in:
spiral
2021-11-17 20:41:02 -05:00
parent 45258d519e
commit e7f36eb31f
24 changed files with 134 additions and 126 deletions

View File

@@ -18,13 +18,13 @@ namespace Myriad.Cache
public ValueTask RemoveUser(ulong userId);
public ValueTask RemoveRole(ulong guildId, ulong roleId);
public bool TryGetGuild(ulong guildId, out Guild guild);
public bool TryGetChannel(ulong channelId, out Channel channel);
public bool TryGetDmChannel(ulong userId, out Channel channel);
public bool TryGetUser(ulong userId, out User user);
public bool TryGetRole(ulong roleId, out Role role);
public Task<bool> TryGetGuild(ulong guildId, out Guild guild);
public Task<bool> TryGetChannel(ulong channelId, out Channel channel);
public Task<bool> TryGetDmChannel(ulong userId, out Channel channel);
public Task<bool> TryGetUser(ulong userId, out User user);
public Task<bool> TryGetRole(ulong roleId, out Role role);
public IAsyncEnumerable<Guild> GetAllGuilds();
public IEnumerable<Channel> GetGuildChannels(ulong guildId);
public Task<IEnumerable<Channel>> GetGuildChannels(ulong guildId);
}
}

View File

@@ -124,34 +124,34 @@ namespace Myriad.Cache
return default;
}
public bool TryGetGuild(ulong guildId, out Guild guild)
public Task<bool> TryGetGuild(ulong guildId, out Guild guild)
{
if (_guilds.TryGetValue(guildId, out var cg))
{
guild = cg.Guild;
return true;
return Task.FromResult(true);
}
guild = null!;
return false;
return Task.FromResult(false);
}
public bool TryGetChannel(ulong channelId, out Channel channel) =>
_channels.TryGetValue(channelId, out channel!);
public Task<bool> TryGetChannel(ulong channelId, out Channel channel) =>
Task.FromResult(_channels.TryGetValue(channelId, out channel!));
public bool TryGetDmChannel(ulong userId, out Channel channel)
public Task<bool> TryGetDmChannel(ulong userId, out Channel channel)
{
channel = default!;
if (!_dmChannels.TryGetValue(userId, out var channelId))
return false;
return Task.FromResult(false);
return TryGetChannel(channelId, out channel);
}
public bool TryGetUser(ulong userId, out User user) =>
_users.TryGetValue(userId, out user!);
public Task<bool> TryGetUser(ulong userId, out User user) =>
Task.FromResult(_users.TryGetValue(userId, out user!));
public bool TryGetRole(ulong roleId, out Role role) =>
_roles.TryGetValue(roleId, out role!);
public Task<bool> TryGetRole(ulong roleId, out Role role) =>
Task.FromResult(_roles.TryGetValue(roleId, out role!));
public IAsyncEnumerable<Guild> GetAllGuilds()
{
@@ -160,12 +160,12 @@ namespace Myriad.Cache
.ToAsyncEnumerable();
}
public IEnumerable<Channel> GetGuildChannels(ulong guildId)
public Task<IEnumerable<Channel>> GetGuildChannels(ulong guildId)
{
if (!_guilds.TryGetValue(guildId, out var guild))
throw new ArgumentException("Guild not found", nameof(guildId));
return guild.Channels.Keys.Select(c => _channels[c]);
return Task.FromResult(guild.Channels.Keys.Select(c => _channels[c]));
}
private CachedGuild SaveGuildRaw(Guild guild) =>