mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
47 lines
1.1 KiB
C#
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 _);
|
|
}
|
|
}
|
|
}
|
|
}
|