| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Management.Filters; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using Microsoft.Extensions.Caching.Memory; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Management.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Decorates an <see cref="IWorkflowDefinitionService"/> with caching capabilities. |
| | | 12 | | /// </summary> |
| | | 13 | | [UsedImplicitly] |
| | 324 | 14 | | public class CachingWorkflowDefinitionService(IWorkflowDefinitionService decoratedService, IWorkflowDefinitionCacheManag |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task<WorkflowGraph> MaterializeWorkflowAsync(WorkflowDefinition definition, CancellationToken cancellat |
| | | 18 | | { |
| | 1014 | 19 | | return await decoratedService.MaterializeWorkflowAsync(definition, cancellationToken); |
| | 1014 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionId, VersionOptions versionOption |
| | | 24 | | { |
| | 2 | 25 | | var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionId, versionOptions); |
| | | 26 | | |
| | 2 | 27 | | return await GetFromCacheAsync(cacheKey, |
| | 2 | 28 | | () => decoratedService.FindWorkflowDefinitionAsync(definitionId, versionOptions, cancellationToken), |
| | 4 | 29 | | x => x.DefinitionId); |
| | 2 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionVersionId, CancellationToken can |
| | | 34 | | { |
| | 0 | 35 | | var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionVersionId); |
| | 0 | 36 | | return await GetFromCacheAsync(cacheKey, |
| | 0 | 37 | | () => decoratedService.FindWorkflowDefinitionAsync(definitionVersionId, cancellationToken), |
| | 0 | 38 | | x => x.DefinitionId); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionHandle handle, CancellationToken canc |
| | | 43 | | { |
| | 0 | 44 | | var filter = new WorkflowDefinitionFilter { DefinitionHandle = handle }; |
| | | 45 | | |
| | 0 | 46 | | return FindWorkflowDefinitionAsync(filter, cancellationToken); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionFilter filter, CancellationToke |
| | | 51 | | { |
| | 520 | 52 | | var cacheKey = cacheManager.CreateWorkflowDefinitionFilterCacheKey(filter); |
| | 520 | 53 | | return await GetFromCacheAsync(cacheKey, |
| | 140 | 54 | | () => decoratedService.FindWorkflowDefinitionAsync(filter, cancellationToken), |
| | 660 | 55 | | x => x.DefinitionId); |
| | 520 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionId, VersionOptions versionOptions, Cancell |
| | | 60 | | { |
| | 35 | 61 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionId, versionOptions); |
| | 35 | 62 | | return await GetFromCacheAsync(cacheKey, |
| | 18 | 63 | | () => decoratedService.FindWorkflowGraphAsync(definitionId, versionOptions, cancellationToken), |
| | 52 | 64 | | x => x.Workflow.Identity.DefinitionId); |
| | 35 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionVersionId, CancellationToken cancellationT |
| | | 69 | | { |
| | 230 | 70 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionVersionId); |
| | 230 | 71 | | return await GetFromCacheAsync(cacheKey, |
| | 77 | 72 | | () => decoratedService.FindWorkflowGraphAsync(definitionVersionId, cancellationToken), |
| | 307 | 73 | | x => x.Workflow.Identity.DefinitionId); |
| | 230 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionHandle definitionHandle, CancellationToken canc |
| | | 78 | | { |
| | 182 | 79 | | var filter = new WorkflowDefinitionFilter { DefinitionHandle = definitionHandle }; |
| | | 80 | | |
| | 182 | 81 | | return FindWorkflowGraphAsync(filter, cancellationToken); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public async Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionFilter filter, CancellationToken cancella |
| | | 86 | | { |
| | 389 | 87 | | var cacheKey = cacheManager.CreateWorkflowFilterCacheKey(filter); |
| | 389 | 88 | | return await GetFromCacheAsync(cacheKey, |
| | 283 | 89 | | () => decoratedService.FindWorkflowGraphAsync(filter, cancellationToken), |
| | 672 | 90 | | x => x.Workflow.Identity.DefinitionId); |
| | 389 | 91 | | } |
| | | 92 | | |
| | | 93 | | public async Task<IEnumerable<WorkflowGraph>> FindWorkflowGraphsAsync(WorkflowDefinitionFilter filter, CancellationT |
| | | 94 | | { |
| | 2 | 95 | | var workflowDefinitions = await workflowDefinitionStore.FindManyAsync(filter, cancellationToken); |
| | 2 | 96 | | var workflowGraphs = new List<WorkflowGraph>(); |
| | 6 | 97 | | foreach (var workflowDefinition in workflowDefinitions) |
| | | 98 | | { |
| | 1 | 99 | | var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(workflowDefinition.Id); |
| | 1 | 100 | | var workflowGraph = await GetFromCacheAsync( |
| | 1 | 101 | | cacheKey, |
| | 0 | 102 | | async () => await MaterializeWorkflowAsync(workflowDefinition, cancellationToken), |
| | 1 | 103 | | wf => wf.Workflow.Identity.DefinitionId); |
| | 1 | 104 | | workflowGraphs.Add(workflowGraph!); |
| | | 105 | | } |
| | | 106 | | |
| | 2 | 107 | | return workflowGraphs; |
| | 2 | 108 | | } |
| | | 109 | | |
| | | 110 | | private async Task<T?> GetFromCacheAsync<T>(string cacheKey, Func<Task<T?>> getObjectFunc, Func<T, string> getChange |
| | | 111 | | { |
| | 1177 | 112 | | var cache = cacheManager.Cache; |
| | 1177 | 113 | | return await cache.GetOrCreateAsync(cacheKey, async entry => |
| | 1177 | 114 | | { |
| | 520 | 115 | | entry.SetAbsoluteExpiration(cache.CachingOptions.Value.CacheDuration); |
| | 520 | 116 | | var obj = await getObjectFunc(); |
| | 1177 | 117 | | |
| | 520 | 118 | | if (obj == null) |
| | 1 | 119 | | return default; |
| | 1177 | 120 | | |
| | 519 | 121 | | var changeTokenKeyInput = getChangeTokenKeyFunc(obj); |
| | 519 | 122 | | var changeTokenKey = cacheManager.CreateWorkflowDefinitionChangeTokenKey(changeTokenKeyInput); |
| | 519 | 123 | | entry.AddExpirationToken(cache.GetToken(changeTokenKey)); |
| | 519 | 124 | | return obj; |
| | 1697 | 125 | | }); |
| | 1177 | 126 | | } |
| | | 127 | | } |