| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Http.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a caching implementation of the IHttpWorkflowLookupService that retrieves workflows using HTTP workflow l |
| | | 9 | | /// </summary> |
| | | 10 | | [UsedImplicitly] |
| | 215 | 11 | | public class CachingHttpWorkflowLookupService( |
| | 215 | 12 | | IHttpWorkflowLookupService decoratedService, |
| | 215 | 13 | | IHttpWorkflowsCacheManager cacheManager, |
| | 215 | 14 | | ITenantAccessor tenantAccessor) : IHttpWorkflowLookupService |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task<HttpWorkflowLookupResult?> FindWorkflowAsync(string bookmarkHash, CancellationToken cancellationTo |
| | | 18 | | { |
| | 215 | 19 | | var tenant = tenantAccessor.Tenant; |
| | 215 | 20 | | var tenantId = tenant?.Id; |
| | 215 | 21 | | var tenantIdPrefix = !string.IsNullOrEmpty(tenantId) ? $"{tenantId}:" : string.Empty; |
| | 215 | 22 | | var key = $"{tenantIdPrefix}http-workflow:{bookmarkHash}"; |
| | 215 | 23 | | var cache = cacheManager.Cache; |
| | 215 | 24 | | return await cache.GetOrCreateAsync(key, async entry => |
| | 215 | 25 | | { |
| | 209 | 26 | | var cachingOptions = cache.CachingOptions.Value; |
| | 209 | 27 | | entry.SetSlidingExpiration(cachingOptions.CacheDuration); |
| | 209 | 28 | | entry.AddExpirationToken(cache.GetToken(cacheManager.GetTriggerChangeTokenKey(bookmarkHash))); |
| | 215 | 29 | | |
| | 209 | 30 | | var result = await decoratedService.FindWorkflowAsync(bookmarkHash, cancellationToken); |
| | 215 | 31 | | |
| | 209 | 32 | | if (result == null) |
| | 2 | 33 | | return null; |
| | 215 | 34 | | |
| | 207 | 35 | | if(result.WorkflowGraph == null) |
| | 0 | 36 | | return result; |
| | 215 | 37 | | |
| | 207 | 38 | | var workflowGraph = result.WorkflowGraph!; |
| | 207 | 39 | | var changeTokenKey = cacheManager.GetWorkflowChangeTokenKey(workflowGraph.Workflow.Identity.DefinitionId); |
| | 207 | 40 | | var changeToken = cache.GetToken(changeTokenKey); |
| | 207 | 41 | | entry.AddExpirationToken(changeToken); |
| | 215 | 42 | | |
| | 207 | 43 | | return result; |
| | 424 | 44 | | }); |
| | 215 | 45 | | } |
| | | 46 | | } |