< Summary

Information
Class: Elsa.Workflows.Management.Stores.CachingWorkflowDefinitionStore
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Stores/CachingWorkflowDefinitionStore.cs
Line coverage
63%
Covered lines: 41
Uncovered lines: 24
Coverable lines: 65
Total lines: 151
Line coverage: 63%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%11100%
FindAsync()100%11100%
FindAsync()100%11100%
FindManyAsync()100%210%
FindManyAsync()100%11100%
FindManyAsync()100%11100%
<FindManyAsync()100%11100%
FindManyAsync()100%210%
<FindManyAsync()100%210%
FindSummariesAsync()100%210%
FindSummariesAsync()100%210%
FindSummariesAsync()100%11100%
FindSummariesAsync()100%210%
FindLastVersionAsync()100%11100%
SaveAsync()100%11100%
SaveManyAsync()100%11100%
DeleteAsync()100%11100%
AnyAsync()100%210%
CountDistinctAsync()100%210%
GetIsNameUnique()100%210%
GetOrCreateAsync()75%44100%
<GetOrCreateAsync()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Stores/CachingWorkflowDefinitionStore.cs

#LineLine coverage
 1using Elsa.Caching;
 2using Elsa.Common.Models;
 3using Elsa.Common.Multitenancy;
 4using Elsa.Workflows.Management.Entities;
 5using Elsa.Workflows.Management.Filters;
 6using Elsa.Workflows.Management.Models;
 7using Microsoft.Extensions.Caching.Memory;
 8
 9namespace Elsa.Workflows.Management.Stores;
 10
 11/// <summary>
 12/// A decorator for <see cref="IWorkflowDefinitionStore"/> that caches workflow definitions.
 13/// </summary>
 32414public class CachingWorkflowDefinitionStore(IWorkflowDefinitionStore decoratedStore, ICacheManager cacheManager, IHasher
 15{
 116    private static readonly string CacheInvalidationTokenKey = typeof(CachingWorkflowDefinitionStore).FullName!;
 17
 18    /// <inheritdoc />
 19    public async Task<WorkflowDefinition?> FindAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToke
 20    {
 109721        var cacheKey = hasher.Hash(filter);
 218222        return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, cancellationToken));
 109723    }
 24
 25    /// <inheritdoc />
 26    public async Task<WorkflowDefinition?> FindAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder<
 27    {
 628        var cacheKey = hasher.Hash(filter, order);
 1229        return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindAsync(filter, order, cancellationToken));
 630    }
 31
 32    /// <inheritdoc />
 33    public async Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, Cancel
 34    {
 035        var cacheKey = hasher.Hash(filter, pageArgs);
 036        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, pageArgs, cancellationToken)
 037    }
 38
 39    /// <inheritdoc />
 40    public async Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinit
 41    {
 142        var cacheKey = hasher.Hash(filter, order, pageArgs);
 243        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindManyAsync(filter, order, pageArgs, cancellatio
 144    }
 45
 46    /// <inheritdoc />
 47    public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, CancellationToken 
 48    {
 120649        var cacheKey = hasher.Hash(filter);
 235850        return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, cancellationTok
 120651    }
 52
 53    /// <inheritdoc />
 54    public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, Workflow
 55    {
 056        var cacheKey = hasher.Hash(filter, order);
 057        return (await GetOrCreateAsync(cacheKey, async () => (await decoratedStore.FindManyAsync(filter, order, cancella
 058    }
 59
 60    /// <inheritdoc />
 61    public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, PageArgs page
 62    {
 063        var cacheKey = hasher.Hash(filter, pageArgs);
 064        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, pageArgs, cancellationT
 065    }
 66
 67    /// <inheritdoc />
 68    public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, Wor
 69    {
 070        var cacheKey = hasher.Hash(filter, order, pageArgs);
 071        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, pageArgs, cancel
 072    }
 73
 74    /// <inheritdoc />
 75    public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, Cancel
 76    {
 377        var cacheKey = hasher.Hash(filter);
 578        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, cancellationToken)))!;
 379    }
 80
 81    /// <inheritdoc />
 82    public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filt
 83    {
 084        var cacheKey = hasher.Hash(filter, order);
 085        return (await GetOrCreateAsync(cacheKey, () => decoratedStore.FindSummariesAsync(filter, order, cancellationToke
 086    }
 87
 88    /// <inheritdoc />
 89    public async Task<WorkflowDefinition?> FindLastVersionAsync(WorkflowDefinitionFilter filter, CancellationToken cance
 90    {
 991        var cacheKey = hasher.Hash(filter);
 1692        return await GetOrCreateAsync(cacheKey, () => decoratedStore.FindLastVersionAsync(filter, cancellationToken));
 993    }
 94
 95    /// <inheritdoc />
 96    public async Task SaveAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default)
 97    {
 1198        await decoratedStore.SaveAsync(definition, cancellationToken);
 1199        await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken);
 11100    }
 101
 102    /// <inheritdoc />
 103    public async Task SaveManyAsync(IEnumerable<WorkflowDefinition> definitions, CancellationToken cancellationToken = d
 104    {
 574105        await decoratedStore.SaveManyAsync(definitions, cancellationToken);
 574106        await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken);
 574107    }
 108
 109    /// <inheritdoc />
 110    public async Task<long> DeleteAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default)
 111    {
 4112        var result = await decoratedStore.DeleteAsync(filter, cancellationToken);
 4113        await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken);
 4114        return result;
 4115    }
 116
 117    /// <inheritdoc />
 118    public async Task<bool> AnyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default)
 119    {
 0120        var cacheKey = hasher.Hash(nameof(AnyAsync), filter);
 0121        return await GetOrCreateAsync(cacheKey, () => decoratedStore.AnyAsync(filter, cancellationToken));
 0122    }
 123
 124    /// <inheritdoc />
 125    public async Task<long> CountDistinctAsync(CancellationToken cancellationToken = default)
 126    {
 0127        var cacheKey = hasher.Hash(nameof(CountDistinctAsync));
 0128        return await GetOrCreateAsync(cacheKey, () => decoratedStore.CountDistinctAsync(cancellationToken));
 0129    }
 130
 131    /// <inheritdoc />
 132    public async Task<bool> GetIsNameUnique(string name, string? definitionId = default, CancellationToken cancellationT
 133    {
 0134        var cacheKey = hasher.Hash(nameof(GetIsNameUnique), name, definitionId);
 0135        return await GetOrCreateAsync(cacheKey, () => decoratedStore.GetIsNameUnique(name, definitionId, cancellationTok
 0136    }
 137
 138    private async Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory)
 139    {
 2322140        var tenantId = tenantAccessor.Tenant?.Id;
 2322141        var tenantIdPrefix = !string.IsNullOrEmpty(tenantId) ? $"{tenantId}:" : string.Empty;
 2322142        var internalKey = $"{tenantIdPrefix}{typeof(T).Name}:{key}";
 2322143        return await cacheManager.GetOrCreateAsync(internalKey, async entry =>
 2322144        {
 2253145            var invalidationRequestToken = cacheManager.GetToken(CacheInvalidationTokenKey);
 2253146            entry.AddExpirationToken(invalidationRequestToken);
 2253147            entry.SetSlidingExpiration(cacheManager.CachingOptions.Value.CacheDuration);
 2253148            return await factory();
 4575149        });
 2322150    }
 151}