Class DistributedCacheEventStreamStore
- Namespace
- ModelContextProtocol.Server
- Assembly
- ModelContextProtocol.dll
An ISseEventStreamStore implementation backed by IDistributedCache.
public sealed class DistributedCacheEventStreamStore : ISseEventStreamStore
- Inheritance
-
DistributedCacheEventStreamStore
- Implements
- Inherited Members
Remarks
This implementation stores SSE events in a distributed cache, enabling resumability across multiple server instances. Event IDs are encoded with session, stream, and sequence information to allow efficient retrieval of events after a given point.
The writer maintains in-memory state for sequence number generation, as there is guaranteed to be only one writer per stream. Readers may be created from separate processes.
Constructors
DistributedCacheEventStreamStore(IDistributedCache, DistributedCacheEventStreamStoreOptions?, ILogger<DistributedCacheEventStreamStore>?)
Initializes a new instance of the DistributedCacheEventStreamStore class.
public DistributedCacheEventStreamStore(IDistributedCache cache, DistributedCacheEventStreamStoreOptions? options = null, ILogger<DistributedCacheEventStreamStore>? logger = null)
Parameters
cacheIDistributedCacheThe distributed cache to use for storage.
optionsDistributedCacheEventStreamStoreOptionsOptional configuration options for the store.
loggerILogger<DistributedCacheEventStreamStore>Optional logger for diagnostic output.
Methods
CreateStreamAsync(SseEventStreamOptions, CancellationToken)
Creates a new SSE event stream with the specified options.
public ValueTask<ISseEventStreamWriter> CreateStreamAsync(SseEventStreamOptions options, CancellationToken cancellationToken = default)
Parameters
optionsSseEventStreamOptionsThe configuration options for the new stream.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- ValueTask<ISseEventStreamWriter>
A writer for the newly created event stream.
GetStreamReaderAsync(string, CancellationToken)
Gets a reader for an existing event stream based on the last event ID.
public ValueTask<ISseEventStreamReader?> GetStreamReaderAsync(string lastEventId, CancellationToken cancellationToken = default)
Parameters
lastEventIdstringThe ID of the last event received by the client, used to resume from that point.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- ValueTask<ISseEventStreamReader>
A reader for the event stream, or
nullif no matching stream is found.