using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace VanadiumGuard.Server.Sessions
{
///
public sealed class InMemorySessionStore : ISessionStore
{
private readonly ConcurrentDictionary _bySessionId =
new ConcurrentDictionary(StringComparer.Ordinal);
private readonly ConcurrentDictionary _gameSessionToSessionId =
new ConcurrentDictionary(StringComparer.Ordinal);
public void Add(GuardSession session)
{
_bySessionId[session.SessionId] = session;
if (!string.IsNullOrEmpty(session.GameSessionId))
_gameSessionToSessionId[session.GameSessionId] = session.SessionId;
}
public GuardSession? Get(string sessionId)
{
return _bySessionId.TryGetValue(sessionId, out GuardSession? session) ? session : null;
}
public GuardSession? GetByGameSession(string gameSessionId)
{
if (!_gameSessionToSessionId.TryGetValue(gameSessionId, out string? sessionId))
return null;
return Get(sessionId);
}
public void Remove(string sessionId)
{
if (!_bySessionId.TryRemove(sessionId, out GuardSession? session))
return;
if (!string.IsNullOrEmpty(session.GameSessionId))
_gameSessionToSessionId.TryRemove(session.GameSessionId, out _);
}
public IReadOnlyList All()
{
return _bySessionId.Values.ToList();
}
}
}