move classes around, name caches more consistently, add more caches
This commit is contained in:
parent
e86b37ce2a
commit
e17dcf90a1
30 changed files with 443 additions and 51 deletions
63
Catalogger.Backend/Cache/InMemoryCache/RoleCache.cs
Normal file
63
Catalogger.Backend/Cache/InMemoryCache/RoleCache.cs
Normal 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!);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue