< Summary

Information
Class: Elsa.Workflows.Management.Services.CachingWorkflowDefinitionService
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/CachingWorkflowDefinitionService.cs
Line coverage
87%
Covered lines: 55
Uncovered lines: 8
Coverable lines: 63
Total lines: 127
Line coverage: 87.3%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/CachingWorkflowDefinitionService.cs

#LineLine coverage
 1using Elsa.Common.Models;
 2using Elsa.Workflows.Management.Entities;
 3using Elsa.Workflows.Management.Filters;
 4using Elsa.Workflows.Models;
 5using JetBrains.Annotations;
 6using Microsoft.Extensions.Caching.Memory;
 7
 8namespace Elsa.Workflows.Management.Services;
 9
 10/// <summary>
 11/// Decorates an <see cref="IWorkflowDefinitionService"/> with caching capabilities.
 12/// </summary>
 13[UsedImplicitly]
 32414public class CachingWorkflowDefinitionService(IWorkflowDefinitionService decoratedService, IWorkflowDefinitionCacheManag
 15{
 16    /// <inheritdoc />
 17    public async Task<WorkflowGraph> MaterializeWorkflowAsync(WorkflowDefinition definition, CancellationToken cancellat
 18    {
 101419        return await decoratedService.MaterializeWorkflowAsync(definition, cancellationToken);
 101420    }
 21
 22    /// <inheritdoc />
 23    public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionId, VersionOptions versionOption
 24    {
 225        var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionId, versionOptions);
 26
 227        return await GetFromCacheAsync(cacheKey,
 228            () => decoratedService.FindWorkflowDefinitionAsync(definitionId, versionOptions, cancellationToken),
 429            x => x.DefinitionId);
 230    }
 31
 32    /// <inheritdoc />
 33    public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(string definitionVersionId, CancellationToken can
 34    {
 035        var cacheKey = cacheManager.CreateWorkflowDefinitionVersionCacheKey(definitionVersionId);
 036        return await GetFromCacheAsync(cacheKey,
 037            () => decoratedService.FindWorkflowDefinitionAsync(definitionVersionId, cancellationToken),
 038            x => x.DefinitionId);
 039    }
 40
 41    /// <inheritdoc />
 42    public Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionHandle handle, CancellationToken canc
 43    {
 044        var filter = new WorkflowDefinitionFilter { DefinitionHandle = handle };
 45
 046        return FindWorkflowDefinitionAsync(filter, cancellationToken);
 47    }
 48
 49    /// <inheritdoc />
 50    public async Task<WorkflowDefinition?> FindWorkflowDefinitionAsync(WorkflowDefinitionFilter filter, CancellationToke
 51    {
 52052        var cacheKey = cacheManager.CreateWorkflowDefinitionFilterCacheKey(filter);
 52053        return await GetFromCacheAsync(cacheKey,
 14054            () => decoratedService.FindWorkflowDefinitionAsync(filter, cancellationToken),
 66055            x => x.DefinitionId);
 52056    }
 57
 58    /// <inheritdoc />
 59    public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionId, VersionOptions versionOptions, Cancell
 60    {
 3561        var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionId, versionOptions);
 3562        return await GetFromCacheAsync(cacheKey,
 1863            () => decoratedService.FindWorkflowGraphAsync(definitionId, versionOptions, cancellationToken),
 5264            x => x.Workflow.Identity.DefinitionId);
 3565    }
 66
 67    /// <inheritdoc />
 68    public async Task<WorkflowGraph?> FindWorkflowGraphAsync(string definitionVersionId, CancellationToken cancellationT
 69    {
 23070        var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(definitionVersionId);
 23071        return await GetFromCacheAsync(cacheKey,
 7772            () => decoratedService.FindWorkflowGraphAsync(definitionVersionId, cancellationToken),
 30773            x => x.Workflow.Identity.DefinitionId);
 23074    }
 75
 76    /// <inheritdoc />
 77    public Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionHandle definitionHandle, CancellationToken canc
 78    {
 18279        var filter = new WorkflowDefinitionFilter { DefinitionHandle = definitionHandle };
 80
 18281        return FindWorkflowGraphAsync(filter, cancellationToken);
 82    }
 83
 84    /// <inheritdoc />
 85    public async Task<WorkflowGraph?> FindWorkflowGraphAsync(WorkflowDefinitionFilter filter, CancellationToken cancella
 86    {
 38987        var cacheKey = cacheManager.CreateWorkflowFilterCacheKey(filter);
 38988        return await GetFromCacheAsync(cacheKey,
 28389            () => decoratedService.FindWorkflowGraphAsync(filter, cancellationToken),
 67290            x => x.Workflow.Identity.DefinitionId);
 38991    }
 92
 93    public async Task<IEnumerable<WorkflowGraph>> FindWorkflowGraphsAsync(WorkflowDefinitionFilter filter, CancellationT
 94    {
 295        var workflowDefinitions = await workflowDefinitionStore.FindManyAsync(filter, cancellationToken);
 296        var workflowGraphs = new List<WorkflowGraph>();
 697        foreach (var workflowDefinition in workflowDefinitions)
 98        {
 199            var cacheKey = cacheManager.CreateWorkflowVersionCacheKey(workflowDefinition.Id);
 1100            var workflowGraph = await GetFromCacheAsync(
 1101                cacheKey,
 0102                async () => await MaterializeWorkflowAsync(workflowDefinition, cancellationToken),
 1103                wf => wf.Workflow.Identity.DefinitionId);
 1104            workflowGraphs.Add(workflowGraph!);
 105        }
 106
 2107        return workflowGraphs;
 2108    }
 109
 110    private async Task<T?> GetFromCacheAsync<T>(string cacheKey, Func<Task<T?>> getObjectFunc, Func<T, string> getChange
 111    {
 1177112        var cache = cacheManager.Cache;
 1177113        return await cache.GetOrCreateAsync(cacheKey, async entry =>
 1177114        {
 520115            entry.SetAbsoluteExpiration(cache.CachingOptions.Value.CacheDuration);
 520116            var obj = await getObjectFunc();
 1177117
 520118            if (obj == null)
 1119                return default;
 1177120
 519121            var changeTokenKeyInput = getChangeTokenKeyFunc(obj);
 519122            var changeTokenKey = cacheManager.CreateWorkflowDefinitionChangeTokenKey(changeTokenKeyInput);
 519123            entry.AddExpirationToken(cache.GetToken(changeTokenKey));
 519124            return obj;
 1697125        });
 1177126    }
 127}