| | | 1 | | using Elsa.Caching; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Workflows.Management.Entities; |
| | | 5 | | using Elsa.Workflows.Management.Filters; |
| | | 6 | | using Elsa.Workflows.Management.Models; |
| | | 7 | | using Microsoft.Extensions.Caching.Memory; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Management.Stores; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A decorator for <see cref="IWorkflowDefinitionStore"/> that caches workflow definitions. |
| | | 13 | | /// </summary> |
| | 324 | 14 | | public class CachingWorkflowDefinitionStore(IWorkflowDefinitionStore decoratedStore, ICacheManager cacheManager, IHasher |
| | | 15 | | { |
| | 1 | 16 | | private static readonly string CacheInvalidationTokenKey = typeof(CachingWorkflowDefinitionStore).FullName!; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public async Task<WorkflowDefinition?> FindAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToke |
| | | 20 | | { |
| | 1097 | 21 | | var cacheKey = hasher.Hash(filter); |
| | 2182 | 22 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, cancellationToken)); |
| | 1097 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task<WorkflowDefinition?> FindAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder< |
| | | 27 | | { |
| | 6 | 28 | | var cacheKey = hasher.Hash(filter, order); |
| | 12 | 29 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, order, cancellationToken)); |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, Cancel |
| | | 34 | | { |
| | 0 | 35 | | var cacheKey = hasher.Hash(filter, pageArgs); |
| | 0 | 36 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, pageArgs, cancellationToken) |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public async Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinit |
| | | 41 | | { |
| | 1 | 42 | | var cacheKey = hasher.Hash(filter, order, pageArgs); |
| | 2 | 43 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, order, pageArgs, cancellatio |
| | 1 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, CancellationToken |
| | | 48 | | { |
| | 1206 | 49 | | var cacheKey = hasher.Hash(filter); |
| | 2358 | 50 | | return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, cancellationTok |
| | 1206 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, Workflow |
| | | 55 | | { |
| | 0 | 56 | | var cacheKey = hasher.Hash(filter, order); |
| | 0 | 57 | | return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, order, cancella |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, PageArgs page |
| | | 62 | | { |
| | 0 | 63 | | var cacheKey = hasher.Hash(filter, pageArgs); |
| | 0 | 64 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, pageArgs, cancellationT |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, Wor |
| | | 69 | | { |
| | 0 | 70 | | var cacheKey = hasher.Hash(filter, order, pageArgs); |
| | 0 | 71 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, pageArgs, cancel |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, Cancel |
| | | 76 | | { |
| | 3 | 77 | | var cacheKey = hasher.Hash(filter); |
| | 5 | 78 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, cancellationToken)))!; |
| | 3 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filt |
| | | 83 | | { |
| | 0 | 84 | | var cacheKey = hasher.Hash(filter, order); |
| | 0 | 85 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, cancellationToke |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | public async Task<WorkflowDefinition?> FindLastVersionAsync(WorkflowDefinitionFilter filter, CancellationToken cance |
| | | 90 | | { |
| | 9 | 91 | | var cacheKey = hasher.Hash(filter); |
| | 16 | 92 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindLastVersionAsync(filter, cancellationToken)); |
| | 9 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public async Task SaveAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default) |
| | | 97 | | { |
| | 11 | 98 | | await decoratedStore.SaveAsync(definition, cancellationToken); |
| | 11 | 99 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 11 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc /> |
| | | 103 | | public async Task SaveManyAsync(IEnumerable<WorkflowDefinition> definitions, CancellationToken cancellationToken = d |
| | | 104 | | { |
| | 574 | 105 | | await decoratedStore.SaveManyAsync(definitions, cancellationToken); |
| | 574 | 106 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 574 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | | 110 | | public async Task<long> DeleteAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 111 | | { |
| | 4 | 112 | | var result = await decoratedStore.DeleteAsync(filter, cancellationToken); |
| | 4 | 113 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 4 | 114 | | return result; |
| | 4 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | | 118 | | public async Task<bool> AnyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 119 | | { |
| | 0 | 120 | | var cacheKey = hasher.Hash(nameof(AnyAsync), filter); |
| | 0 | 121 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.AnyAsync(filter, cancellationToken)); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <inheritdoc /> |
| | | 125 | | public async Task<long> CountDistinctAsync(CancellationToken cancellationToken = default) |
| | | 126 | | { |
| | 0 | 127 | | var cacheKey = hasher.Hash(nameof(CountDistinctAsync)); |
| | 0 | 128 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.CountDistinctAsync(cancellationToken)); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public async Task<bool> GetIsNameUnique(string name, string? definitionId = default, CancellationToken cancellationT |
| | | 133 | | { |
| | 0 | 134 | | var cacheKey = hasher.Hash(nameof(GetIsNameUnique), name, definitionId); |
| | 0 | 135 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.GetIsNameUnique(name, definitionId, cancellationTok |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | private async Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory) |
| | | 139 | | { |
| | 2322 | 140 | | var tenantId = tenantAccessor.Tenant?.Id; |
| | 2322 | 141 | | var tenantIdPrefix = !string.IsNullOrEmpty(tenantId) ? $"{tenantId}:" : string.Empty; |
| | 2322 | 142 | | var internalKey = $"{tenantIdPrefix}{typeof(T).Name}:{key}"; |
| | 2322 | 143 | | return await cacheManager.GetOrCreateAsync(internalKey, async entry => |
| | 2322 | 144 | | { |
| | 2253 | 145 | | var invalidationRequestToken = cacheManager.GetToken(CacheInvalidationTokenKey); |
| | 2253 | 146 | | entry.AddExpirationToken(invalidationRequestToken); |
| | 2253 | 147 | | entry.SetSlidingExpiration(cacheManager.CachingOptions.Value.CacheDuration); |
| | 2253 | 148 | | return await factory(); |
| | 4575 | 149 | | }); |
| | 2322 | 150 | | } |
| | | 151 | | } |