move classes around, name caches more consistently, add more caches

This commit is contained in:
sam 2024-08-19 16:12:28 +02:00
parent e86b37ce2a
commit e17dcf90a1
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
30 changed files with 443 additions and 51 deletions

View file

@ -0,0 +1,59 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class ChannelCache
{
private readonly ConcurrentDictionary<Snowflake, IChannel> _channels = new();
private readonly ConcurrentDictionary<Snowflake, HashSet<Snowflake>> _guildChannels = new();
public int Size => _channels.Count;
public void Set(IChannel channel, Snowflake? guildId = null)
{
_channels[channel.ID] = channel;
if (guildId == null)
{
if (!channel.GuildID.TryGet(out var snowflake)) return;
guildId = snowflake;
}
// Add to set of guild channels
_guildChannels.AddOrUpdate(guildId.Value,
_ => [channel.ID],
(_, l) =>
{
l.Add(channel.ID);
return l;
});
}
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IChannel? channel) =>
_channels.TryGetValue(id, out channel);
public void Remove(Snowflake? guildId, Snowflake id, out IChannel? channel)
{
_channels.Remove(id, out channel);
if (guildId == null) return;
// Remove from set of guild channels
_guildChannels.AddOrUpdate(guildId.Value, _ => [], (_, s) =>
{
s.Remove(id);
return s;
});
}
/// <summary>
/// Gets all of a guild's cached channels.
/// </summary>
/// <param name="guildId">The guild to get the channels of</param>
/// <returns>A list of cached channels</returns>
public IEnumerable<IChannel> GuildChannels(Snowflake guildId) =>
!_guildChannels.TryGetValue(guildId, out var channelIds)
? []
: channelIds.Select(id => _channels.GetValueOrDefault(id))
.Where(c => c != null).Select(c => c!);
}

View file

@ -0,0 +1,17 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class GuildCache
{
private readonly ConcurrentDictionary<Snowflake, IGuild> _guilds = new();
public int Size => _guilds.Count;
public void Set(IGuild guild) => _guilds[guild.ID] = guild;
public bool Remove(Snowflake id, [NotNullWhen(true)] out IGuild? guild) => _guilds.Remove(id, out guild);
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IGuild? guild) => _guilds.TryGetValue(id, out guild);
}

View file

@ -0,0 +1,20 @@
using System.Collections.Concurrent;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class InMemoryInviteCache : IInviteCache
{
private readonly ConcurrentDictionary<Snowflake, IEnumerable<IInvite>> _invites = new();
public Task<IEnumerable<IInvite>> TryGetAsync(Snowflake guildId) => _invites.TryGetValue(guildId, out var invites)
? Task.FromResult(invites)
: Task.FromResult<IEnumerable<IInvite>>([]);
public Task SetAsync(Snowflake guildId, IEnumerable<IInvite> invites)
{
_invites[guildId] = invites;
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,46 @@
using System.Collections.Concurrent;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class InMemoryMemberCache : IMemberCache
{
private readonly ConcurrentDictionary<(Snowflake, Snowflake), IGuildMember> _members = new();
private readonly ConcurrentDictionary<Snowflake, byte> _guilds = new();
#pragma warning disable CS8619 // Nullability of reference types in value doesn't match target type.
public Task<IGuildMember?> TryGetAsync(Snowflake guildId, Snowflake userId) =>
_members.TryGetValue((guildId, userId), out var member)
? Task.FromResult(member)
: Task.FromResult<IGuildMember?>(null);
#pragma warning restore CS8619 // Nullability of reference types in value doesn't match target type.
public Task SetAsync(Snowflake guildId, IGuildMember member)
{
if (!member.User.IsDefined())
throw new CataloggerError("Member with undefined User passed to RedisMemberCache.SetAsync");
_members[(guildId, member.User.Value.ID)] = member;
return Task.CompletedTask;
}
public async Task SetManyAsync(Snowflake guildId, IReadOnlyList<IGuildMember> members)
{
foreach (var member in members)
await SetAsync(guildId, member);
}
public Task<bool> IsGuildCachedAsync(Snowflake guildId) => Task.FromResult(_guilds.ContainsKey(guildId));
public Task MarkAsCachedAsync(Snowflake guildId)
{
_guilds[guildId] = 1;
return Task.CompletedTask;
}
public Task MarkAsUncachedAsync(Snowflake guildId)
{
_guilds.Remove(guildId, out _);
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,21 @@
using System.Collections.Concurrent;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class InMemoryWebhookCache : IWebhookCache
{
private readonly ConcurrentDictionary<ulong, Webhook> _cache = new();
public Task<Webhook?> GetWebhookAsync(ulong channelId)
{
return _cache.TryGetValue(channelId, out var webhook)
? Task.FromResult<Webhook?>(webhook)
: Task.FromResult<Webhook?>(null);
}
public Task SetWebhookAsync(ulong channelId, Webhook webhook)
{
_cache[channelId] = webhook;
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,63 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class RoleCache
{
private readonly ConcurrentDictionary<Snowflake, IRole> _roles = new();
private readonly ConcurrentDictionary<Snowflake, HashSet<Snowflake>> _guildRoles = new();
public int Size => _roles.Count;
public void Set(IRole role, Snowflake guildId)
{
_roles[role.ID] = role;
// Add to set of guild channels
_guildRoles.AddOrUpdate(guildId,
_ => [role.ID],
(_, l) =>
{
l.Add(role.ID);
return l;
});
}
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IRole? role) =>
_roles.TryGetValue(id, out role);
public void Remove(Snowflake guildId, Snowflake id, out IRole? role)
{
_roles.Remove(id, out role);
// Remove from set of guild channels
_guildRoles.AddOrUpdate(guildId, _ => [], (_, s) =>
{
s.Remove(id);
return s;
});
}
public void RemoveGuild(Snowflake guildId)
{
if (!_guildRoles.TryGetValue(guildId, out var roleIds)) return;
foreach (var id in roleIds)
{
_roles.Remove(id, out _);
}
_guildRoles.Remove(guildId, out _);
}
/// <summary>
/// Gets all of a guild's cached roles.
/// </summary>
/// <param name="guildId">The guild to get the roles of</param>
/// <returns>A list of cached roles</returns>
public IEnumerable<IRole> GuildRoles(Snowflake guildId) =>
!_guildRoles.TryGetValue(guildId, out var roleIds)
? []
: roleIds.Select(id => _roles.GetValueOrDefault(id))
.Where(r => r != null).Select(r => r!);
}

View file

@ -0,0 +1,26 @@
using System.Collections.Concurrent;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache.InMemoryCache;
public class UserCache(IDiscordRestUserAPI userApi)
{
private readonly ConcurrentDictionary<Snowflake, IUser> _cache = new();
public int Size => _cache.Count;
public async Task<IUser?> GetUserAsync(Snowflake userId)
{
if (_cache.TryGetValue(userId, out var user)) return user;
var res = await userApi.GetUserAsync(userId);
if (!res.IsSuccess) return null;
_cache[userId] = res.Entity;
return res.Entity;
}
public void UpdateUser(IUser user) => _cache[user.ID] = user;
}