finish ChannelUpdate responder

This commit is contained in:
sam 2024-08-20 18:18:17 +02:00
parent eb40872254
commit df8af75dd4
14 changed files with 155 additions and 63 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Catalogger.Backend.Extensions;
using LazyCache;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Rest.Core;
@ -7,20 +8,18 @@ namespace Catalogger.Backend.Cache.InMemoryCache;
public class UserCache(IDiscordRestUserAPI userApi)
{
private readonly ConcurrentDictionary<Snowflake, IUser> _cache = new();
private readonly IAppCache _cache = new CachingService();
private int _cacheSize = 0;
public int Size => _cache.Count;
public int Size => _cacheSize;
public async Task<IUser?> GetUserAsync(Snowflake userId)
{
if (_cache.TryGetValue(userId, out var user)) return user;
public async Task<IUser?> GetUserAsync(Snowflake userId) => await _cache.GetOrAddAsync(userId.ToString(),
async () =>
{
var user = await userApi.GetUserAsync(userId).GetOrThrow();
Interlocked.Increment(ref _cacheSize);
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;
public void UpdateUser(IUser user) => _cache.Add(user.ID.ToString(), user);
}