| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using TeleFlow.Framework.States; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Storage.Memory; |
| | | 5 | | |
| | | 6 | | public sealed class MemoryStateStore : IStateStore |
| | | 7 | | { |
| | 36 | 8 | | private readonly ConcurrentDictionary<StateKey, string> _states = new(); |
| | | 9 | | |
| | | 10 | | public ValueTask<string?> GetStateAsync( |
| | | 11 | | StateKey key, |
| | | 12 | | CancellationToken cancellationToken = default) |
| | | 13 | | { |
| | 49 | 14 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 15 | | |
| | 48 | 16 | | _states.TryGetValue(key, out var state); |
| | 48 | 17 | | return ValueTask.FromResult(state); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public ValueTask SetStateAsync( |
| | | 21 | | StateKey key, |
| | | 22 | | string state, |
| | | 23 | | CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 41 | 25 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 40 | 26 | | ArgumentException.ThrowIfNullOrWhiteSpace(state); |
| | | 27 | | |
| | 39 | 28 | | _states[key] = state; |
| | 39 | 29 | | return ValueTask.CompletedTask; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public ValueTask ClearStateAsync( |
| | | 33 | | StateKey key, |
| | | 34 | | CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 13 | 36 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 37 | | |
| | 12 | 38 | | _states.TryRemove(key, out _); |
| | 12 | 39 | | return ValueTask.CompletedTask; |
| | | 40 | | } |
| | | 41 | | } |