Files
Radicade/Server/Chat/RecentImageReferences.cs
T
2026-10-16 23:33:13 -07:00

47 lines
1.1 KiB
C#

using System.Collections.Concurrent;
namespace RadicadeServer.API
{
public static class RecentImageReferences
{
public static readonly TimeSpan Window = TimeSpan.FromMinutes(10);
private sealed record Lookup(long ImageId, DateTime At);
private static readonly ConcurrentDictionary<long, Lookup> ByPlayer = new();
public static void Remember(long accountId, long imageId)
=> Remember(accountId, imageId, DateTime.UtcNow);
public static void Remember(long accountId, long imageId, DateTime at)
{
if (accountId <= 0 || imageId <= 0) return;
Prune(at);
ByPlayer[accountId] = new Lookup(imageId, at);
}
public static bool TryGet(long accountId, out long imageId)
{
imageId = 0;
if (accountId <= 0) return false;
if (!ByPlayer.TryGetValue(accountId, out var lookup)) return false;
if (DateTime.UtcNow - lookup.At > Window) return false;
imageId = lookup.ImageId;
return true;
}
public static void Clear() => ByPlayer.Clear();
private static void Prune(DateTime now)
{
foreach (var pair in ByPlayer)
{
if (now - pair.Value.At > Window)
ByPlayer.TryRemove(pair.Key, out _);
}
}
}
}