add all requirements for implementing XAiMcpClient ...
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace xAiService.Services
|
||||
{
|
||||
public class XAiMcpSessionStore
|
||||
{
|
||||
//
|
||||
private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(15);
|
||||
private readonly ConcurrentDictionary<(Guid ServerId, string UserId), SessionEntry> sessions = new();
|
||||
|
||||
public string Get(
|
||||
Guid serverId,
|
||||
string userId
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!sessions.TryGetValue((serverId, userId), out var entry))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
// Expriration Check ...
|
||||
if (DateTime.UtcNow - entry.LastAccessAt > Ttl)
|
||||
{
|
||||
//
|
||||
sessions.TryRemove((serverId, userId), out _);
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
entry.LastAccessAt = DateTime.UtcNow;
|
||||
return entry.SessionId;
|
||||
}
|
||||
|
||||
public void Set(
|
||||
Guid serverId,
|
||||
string userId,
|
||||
string sessionId
|
||||
)
|
||||
{
|
||||
//
|
||||
sessions[(serverId, userId)] = new SessionEntry
|
||||
{
|
||||
SessionId = sessionId,
|
||||
LastAccessAt = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
public void Remove(
|
||||
Guid serverId,
|
||||
string userId
|
||||
) => sessions.TryRemove((serverId, userId), out _);
|
||||
|
||||
public int CleanupExpired()
|
||||
{
|
||||
//
|
||||
var count = 0;
|
||||
var now = DateTime.UtcNow;
|
||||
foreach (var kv in sessions)
|
||||
{
|
||||
//
|
||||
if (now - kv.Value.LastAccessAt > Ttl)
|
||||
{
|
||||
if (sessions.TryRemove(kv.Key, out _))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return count;
|
||||
}
|
||||
|
||||
private class SessionEntry
|
||||
{
|
||||
public string SessionId { get; set; }
|
||||
public DateTime LastAccessAt { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user