| | | 1 | | using Elsa.Expressions.Models; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Expressions.Liquid.Contracts; |
| | | 4 | | using Elsa.Expressions.Liquid.Notifications; |
| | | 5 | | using Elsa.Expressions.Liquid.Options; |
| | | 6 | | using Elsa.Mediator.Contracts; |
| | | 7 | | using Fluid; |
| | | 8 | | using Microsoft.Extensions.Caching.Memory; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Expressions.Liquid.Services; |
| | | 12 | | |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public class LiquidTemplateManager : ILiquidTemplateManager |
| | | 15 | | { |
| | | 16 | | private readonly LiquidParser _parser; |
| | | 17 | | private readonly IMemoryCache _memoryCache; |
| | | 18 | | private readonly INotificationSender _notificationSender; |
| | | 19 | | private readonly FluidOptions _options; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Constructor. |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public LiquidTemplateManager(LiquidParser parser, IMemoryCache memoryCache, INotificationSender notificationSender, |
| | | 25 | | { |
| | 0 | 26 | | _parser = parser; |
| | 0 | 27 | | _memoryCache = memoryCache; |
| | 0 | 28 | | _notificationSender = notificationSender; |
| | 0 | 29 | | _options = options.Value; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async Task<string?> RenderAsync(string template, ExpressionExecutionContext expressionExecutionContext, Cance |
| | | 34 | | { |
| | 0 | 35 | | if (string.IsNullOrWhiteSpace(template)) |
| | 0 | 36 | | return default!; |
| | | 37 | | |
| | 0 | 38 | | var result = GetCachedTemplate(template); |
| | 0 | 39 | | var templateContext = await CreateTemplateContextAsync(expressionExecutionContext, cancellationToken); |
| | 0 | 40 | | var encoder = _options.Encoder; |
| | 0 | 41 | | templateContext.AddFilters(_options, expressionExecutionContext.ServiceProvider); |
| | | 42 | | |
| | 0 | 43 | | return await result.RenderAsync(templateContext, encoder); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private IFluidTemplate GetCachedTemplate(string source) |
| | | 47 | | { |
| | 0 | 48 | | var result = _memoryCache.GetOrCreate( |
| | 0 | 49 | | source, |
| | 0 | 50 | | e => |
| | 0 | 51 | | { |
| | 0 | 52 | | if (!TryParse(source, out var parsed, out var error)) |
| | 0 | 53 | | { |
| | 0 | 54 | | error = "{% raw %}\n" + error + "\n{% endraw %}"; |
| | 0 | 55 | | TryParse(error, out parsed, out error); |
| | 0 | 56 | | |
| | 0 | 57 | | e.SetSlidingExpiration(TimeSpan.FromMilliseconds(100)); |
| | 0 | 58 | | return parsed; |
| | 0 | 59 | | } |
| | 0 | 60 | | |
| | 0 | 61 | | // TODO: add signal based cache invalidation. |
| | 0 | 62 | | e.SetSlidingExpiration(TimeSpan.FromSeconds(30)); |
| | 0 | 63 | | return parsed; |
| | 0 | 64 | | }); |
| | 0 | 65 | | return result!; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | 0 | 69 | | public bool Validate(string template, out string error) => TryParse(template, out _, out error); |
| | | 70 | | |
| | 0 | 71 | | private bool TryParse(string template, out IFluidTemplate result, out string error) => _parser.TryParse(template, ou |
| | | 72 | | |
| | | 73 | | private async Task<TemplateContext> CreateTemplateContextAsync(ExpressionExecutionContext expressionExecutionContext |
| | | 74 | | { |
| | 0 | 75 | | var context = new TemplateContext(expressionExecutionContext, new TemplateOptions()); |
| | 0 | 76 | | await _notificationSender.SendAsync(new RenderingLiquidTemplate(context, expressionExecutionContext), cancellati |
| | 0 | 77 | | context.SetValue("ExpressionExecutionContext", expressionExecutionContext); |
| | 0 | 78 | | return context; |
| | 0 | 79 | | } |
| | | 80 | | } |