Catalogger.NET/Catalogger.Backend/Cache/UserCacheService.cs

26 lines
735 B
C#
Raw Normal View History

2024-08-13 13:08:50 +02:00
using System.Collections.Concurrent;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Rest.Core;
namespace Catalogger.Backend.Cache;
public class UserCacheService(IDiscordRestUserAPI userApi)
{
private readonly ConcurrentDictionary<Snowflake, IUser> _cache = new();
2024-08-16 17:04:24 +02:00
public int Size => _cache.Count;
2024-08-13 13:08:50 +02:00
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;
}