| | | 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> |
| | 436 | 14 | | public class CachingWorkflowDefinitionStore(IWorkflowDefinitionStore decoratedStore, ICacheManager cacheManager, IHasher |
| | | 15 | | { |
| | 2 | 16 | | private static readonly string CacheInvalidationTokenKey = typeof(CachingWorkflowDefinitionStore).FullName!; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the local reconciliation token for a tenant's workflow-definition view. |
| | | 20 | | /// </summary> |
| | 9299 | 21 | | internal static string GetTenantReconciliationTokenKey(string tenantId) => $"{CacheInvalidationTokenKey}:Reconcile:{ |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public async Task<WorkflowDefinition?> FindAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToke |
| | | 25 | | { |
| | 3328 | 26 | | var cacheKey = hasher.Hash(filter); |
| | 6654 | 27 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, cancellationToken)); |
| | 3328 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async Task<WorkflowDefinition?> FindAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder< |
| | | 32 | | { |
| | 11 | 33 | | var cacheKey = hasher.Hash(filter, order); |
| | 22 | 34 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, order, cancellationToken)); |
| | 11 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, Cancel |
| | | 39 | | { |
| | 0 | 40 | | var cacheKey = hasher.Hash(filter, pageArgs); |
| | 0 | 41 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, pageArgs, cancellationToken) |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinit |
| | | 46 | | { |
| | 2 | 47 | | var cacheKey = hasher.Hash(filter, order, pageArgs); |
| | 4 | 48 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, order, pageArgs, cancellatio |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, CancellationToken |
| | | 53 | | { |
| | 5857 | 54 | | var cacheKey = hasher.Hash(filter); |
| | 11604 | 55 | | return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, cancellationTok |
| | 5857 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, Workflow |
| | | 60 | | { |
| | 0 | 61 | | var cacheKey = hasher.Hash(filter, order); |
| | 0 | 62 | | return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, order, cancella |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, PageArgs page |
| | | 67 | | { |
| | 0 | 68 | | var cacheKey = hasher.Hash(filter, pageArgs); |
| | 0 | 69 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, pageArgs, cancellationT |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, Wor |
| | | 74 | | { |
| | 0 | 75 | | var cacheKey = hasher.Hash(filter, order, pageArgs); |
| | 0 | 76 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, pageArgs, cancel |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, Cancel |
| | | 81 | | { |
| | 16 | 82 | | var cacheKey = hasher.Hash(filter); |
| | 23 | 83 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, cancellationToken)))!; |
| | 16 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filt |
| | | 88 | | { |
| | 0 | 89 | | var cacheKey = hasher.Hash(filter, order); |
| | 0 | 90 | | return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, cancellationToke |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | | 94 | | public async Task<WorkflowDefinition?> FindLastVersionAsync(WorkflowDefinitionFilter filter, CancellationToken cance |
| | | 95 | | { |
| | 21 | 96 | | var cacheKey = hasher.Hash(filter); |
| | 38 | 97 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindLastVersionAsync(filter, cancellationToken)); |
| | 21 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | public async Task SaveAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default) |
| | | 102 | | { |
| | 27 | 103 | | await decoratedStore.SaveAsync(definition, cancellationToken); |
| | 27 | 104 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 27 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <inheritdoc /> |
| | | 108 | | public async Task<WorkflowDefinitionUpdateResult> TryUpdateLatestAsync( |
| | | 109 | | WorkflowDefinitionFilter filter, |
| | | 110 | | Func<WorkflowDefinition, bool> matchesExpected, |
| | | 111 | | Func<WorkflowDefinition, WorkflowDefinition> update, |
| | | 112 | | CancellationToken cancellationToken = default) |
| | | 113 | | { |
| | 0 | 114 | | var result = await decoratedStore.TryUpdateLatestAsync(filter, matchesExpected, update, cancellationToken); |
| | | 115 | | |
| | 0 | 116 | | if (result.Outcome == WorkflowDefinitionUpdateOutcome.Updated) |
| | 0 | 117 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | | 118 | | |
| | 0 | 119 | | return result; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | | 123 | | public async Task SaveManyAsync(IEnumerable<WorkflowDefinition> definitions, CancellationToken cancellationToken = d |
| | | 124 | | { |
| | 2760 | 125 | | await decoratedStore.SaveManyAsync(definitions, cancellationToken); |
| | 2760 | 126 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 2760 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | | 130 | | public async Task<long> DeleteAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 131 | | { |
| | 7 | 132 | | var result = await decoratedStore.DeleteAsync(filter, cancellationToken); |
| | 7 | 133 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 7 | 134 | | return result; |
| | 7 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <inheritdoc /> |
| | | 138 | | public async Task<bool> AnyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 139 | | { |
| | 0 | 140 | | var cacheKey = hasher.Hash(nameof(AnyAsync), filter); |
| | 0 | 141 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.AnyAsync(filter, cancellationToken)); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <inheritdoc /> |
| | | 145 | | public async Task<long> CountDistinctAsync(CancellationToken cancellationToken = default) |
| | | 146 | | { |
| | 0 | 147 | | var cacheKey = hasher.Hash(nameof(CountDistinctAsync)); |
| | 0 | 148 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.CountDistinctAsync(cancellationToken)); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public async Task<bool> GetIsNameUnique(string name, string? definitionId = null, CancellationToken cancellationToke |
| | | 153 | | { |
| | 0 | 154 | | var cacheKey = hasher.Hash(nameof(GetIsNameUnique), name, definitionId); |
| | 0 | 155 | | return await GetOrCreateAsync(cacheKey, () => decoratedStore.GetIsNameUnique(name, definitionId, cancellationTok |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | private async Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory) |
| | | 159 | | { |
| | 9235 | 160 | | var tenantId = tenantAccessor.TenantId; |
| | 9235 | 161 | | var tenantIdPrefix = !string.IsNullOrEmpty(tenantId) ? $"{tenantId}:" : string.Empty; |
| | 9235 | 162 | | var internalKey = $"{tenantIdPrefix}{typeof(T).Name}:{key}"; |
| | 9235 | 163 | | return await cacheManager.FindOrCreateAsync(internalKey, async entry => |
| | 9235 | 164 | | { |
| | 9110 | 165 | | entry.AddExpirationToken(cacheManager.GetToken(CacheInvalidationTokenKey)); |
| | 9110 | 166 | | entry.AddExpirationToken(cacheManager.GetToken(GetTenantReconciliationTokenKey(tenantId))); |
| | 9110 | 167 | | entry.SetSlidingExpiration(cacheManager.CachingOptions.Value.CacheDuration); |
| | 9110 | 168 | | return await factory(); |
| | 18345 | 169 | | }); |
| | 9235 | 170 | | } |
| | | 171 | | |
| | | 172 | | } |