-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathInMemorySessionStore.cs
More file actions
87 lines (75 loc) · 2.95 KB
/
InMemorySessionStore.cs
File metadata and controls
87 lines (75 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.Concurrent;
namespace ModelContextProtocol.AspNetCore;
/// <summary>
/// A simple in-memory implementation of <see cref="ISessionStore"/> for testing and development.
/// </summary>
/// <remarks>
/// This implementation stores session metadata in a <see cref="ConcurrentDictionary{TKey, TValue}"/>.
/// It is NOT suitable for production use in distributed scenarios since the data is not shared
/// across server instances. Use a database-backed implementation for production deployments.
/// </remarks>
public sealed class InMemorySessionStore : ISessionStore
{
private readonly ConcurrentDictionary<string, SessionMetadata> _sessions = new();
private readonly TimeProvider _timeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="InMemorySessionStore"/> class.
/// </summary>
/// <param name="timeProvider">Optional time provider for testing. Defaults to <see cref="TimeProvider.System"/>.</param>
public InMemorySessionStore(TimeProvider? timeProvider = null)
{
_timeProvider = timeProvider ?? TimeProvider.System;
}
/// <summary>
/// Gets the current number of sessions in the store.
/// </summary>
public int Count => _sessions.Count;
/// <inheritdoc />
public Task SaveAsync(SessionMetadata metadata, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(metadata);
_sessions[metadata.SessionId] = metadata;
return Task.CompletedTask;
}
/// <inheritdoc />
public Task<SessionMetadata?> GetAsync(string sessionId, CancellationToken cancellationToken = default)
{
_sessions.TryGetValue(sessionId, out var metadata);
return Task.FromResult(metadata);
}
/// <inheritdoc />
public Task UpdateActivityAsync(string sessionId, DateTime lastActivityUtc, CancellationToken cancellationToken = default)
{
if (_sessions.TryGetValue(sessionId, out var metadata))
{
metadata.LastActivityUtc = lastActivityUtc;
}
return Task.CompletedTask;
}
/// <inheritdoc />
public Task<bool> RemoveAsync(string sessionId, CancellationToken cancellationToken = default)
{
return Task.FromResult(_sessions.TryRemove(sessionId, out _));
}
/// <inheritdoc />
public Task<int> PruneIdleSessionsAsync(TimeSpan idleTimeout, CancellationToken cancellationToken = default)
{
var cutoff = _timeProvider.GetUtcNow().DateTime - idleTimeout;
var removed = 0;
foreach (var kvp in _sessions)
{
if (kvp.Value.LastActivityUtc < cutoff)
{
if (_sessions.TryRemove(kvp.Key, out _))
{
removed++;
}
}
}
return Task.FromResult(removed);
}
/// <summary>
/// Clears all sessions from the store. Useful for test cleanup.
/// </summary>
public void Clear() => _sessions.Clear();
}