| | | 1 | | using Elsa.Common.Services; |
| | | 2 | | using Elsa.KeyValues.Contracts; |
| | | 3 | | using Elsa.KeyValues.Entities; |
| | | 4 | | using Elsa.KeyValues.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.KeyValues.Stores; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Stores key value records in memory. |
| | | 10 | | /// </summary> |
| | | 11 | | public class MemoryKeyValueStore : IKeyValueStore |
| | | 12 | | { |
| | | 13 | | private readonly MemoryStore<SerializedKeyValuePair> _store; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="MemoryKeyValueStore"/> class. |
| | | 17 | | /// </summary> |
| | 0 | 18 | | public MemoryKeyValueStore(MemoryStore<SerializedKeyValuePair> store) |
| | | 19 | | { |
| | 0 | 20 | | _store = store; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public Task SaveAsync(SerializedKeyValuePair keyValuePair, CancellationToken cancellationToken) |
| | | 25 | | { |
| | 0 | 26 | | _store.Save(keyValuePair, kv => kv.Id); |
| | 0 | 27 | | return Task.CompletedTask; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task<SerializedKeyValuePair?> FindAsync(KeyValueFilter filter, CancellationToken cancellationToken) |
| | | 32 | | { |
| | 0 | 33 | | var result = _store.Query(filter.Apply).FirstOrDefault(); |
| | 0 | 34 | | return Task.FromResult(result); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public Task<IEnumerable<SerializedKeyValuePair>> FindManyAsync(KeyValueFilter filter, CancellationToken cancellation |
| | | 39 | | { |
| | 0 | 40 | | var result = _store.Query(filter.Apply); |
| | 0 | 41 | | return Task.FromResult(result); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public Task DeleteAsync(string key, CancellationToken cancellationToken) |
| | | 46 | | { |
| | 0 | 47 | | _store.DeleteWhere(x => x.Key == key); |
| | 0 | 48 | | return Task.CompletedTask; |
| | | 49 | | } |
| | | 50 | | } |