| | | 1 | | using Elsa.Caching; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Workflows.Runtime.Entities; |
| | | 5 | | using Elsa.Workflows.Runtime.Filters; |
| | | 6 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 7 | | using Microsoft.Extensions.Caching.Memory; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime.Stores; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A decorator for <see cref="ITriggerStore"/> that caches trigger records. |
| | | 13 | | /// </summary> |
| | 321 | 14 | | public class CachingTriggerStore(ITriggerStore decoratedStore, ICacheManager cacheManager, IHasher hasher, ITenantAccess |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The token key to evict all cached triggers. |
| | | 18 | | /// </summary> |
| | 1 | 19 | | public static readonly string CacheInvalidationTokenKey = typeof(CachingTriggerStore).FullName!; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public async ValueTask SaveAsync(StoredTrigger record, CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 0 | 24 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 0 | 25 | | await decoratedStore.SaveAsync(record, cancellationToken); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async ValueTask SaveManyAsync(IEnumerable<StoredTrigger> records, CancellationToken cancellationToken = defau |
| | | 30 | | { |
| | 0 | 31 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 0 | 32 | | await decoratedStore.SaveManyAsync(records, cancellationToken); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public async ValueTask<StoredTrigger?> FindAsync(TriggerFilter filter, CancellationToken cancellationToken = default |
| | | 37 | | { |
| | 2 | 38 | | var cacheKey = hasher.Hash(filter); |
| | 4 | 39 | | return (await GetOrCreateAsync(cacheKey, async () => await decoratedStore.FindAsync(filter, cancellationToken))) |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public async ValueTask<IEnumerable<StoredTrigger>> FindManyAsync(TriggerFilter filter, CancellationToken cancellatio |
| | | 44 | | { |
| | 714 | 45 | | var cacheKey = hasher.Hash(filter); |
| | 1238 | 46 | | return (await GetOrCreateAsync(cacheKey, async () => await decoratedStore.FindManyAsync(filter, cancellationToke |
| | 714 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async ValueTask<Page<StoredTrigger>> FindManyAsync(TriggerFilter filter, PageArgs pageArgs, CancellationToken |
| | | 50 | | { |
| | 0 | 51 | | var cacheKey = hasher.Hash(filter); |
| | 0 | 52 | | return (await GetOrCreateAsync(cacheKey, async () => await decoratedStore.FindManyAsync(filter, pageArgs, cancel |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async ValueTask<Page<StoredTrigger>> FindManyAsync<TProp>(TriggerFilter filter, PageArgs pageArgs, StoredTrig |
| | | 56 | | { |
| | 0 | 57 | | var cacheKey = hasher.Hash(filter); |
| | 0 | 58 | | return (await GetOrCreateAsync(cacheKey, async () => await decoratedStore.FindManyAsync(filter, pageArgs, order, |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public async ValueTask ReplaceAsync(IEnumerable<StoredTrigger> removed, IEnumerable<StoredTrigger> added, Cancellati |
| | | 63 | | { |
| | 488 | 64 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 488 | 65 | | await decoratedStore.ReplaceAsync(removed, added, cancellationToken); |
| | 488 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public async ValueTask<long> DeleteManyAsync(TriggerFilter filter, CancellationToken cancellationToken = default) |
| | | 70 | | { |
| | 0 | 71 | | await cacheManager.TriggerTokenAsync(CacheInvalidationTokenKey, cancellationToken); |
| | 0 | 72 | | return await decoratedStore.DeleteManyAsync(filter, cancellationToken); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | private async Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory) |
| | | 76 | | { |
| | 716 | 77 | | var tenantId = tenantAccessor.Tenant?.Id; |
| | 716 | 78 | | var tenantIdPrefix = !string.IsNullOrEmpty(tenantId) ? $"{tenantId}:" : string.Empty; |
| | 716 | 79 | | var internalKey = $"{tenantIdPrefix}{typeof(T).Name}:{key}"; |
| | 716 | 80 | | return await cacheManager.GetOrCreateAsync(internalKey, async entry => |
| | 716 | 81 | | { |
| | 526 | 82 | | var invalidationRequestToken = cacheManager.GetToken(CacheInvalidationTokenKey); |
| | 526 | 83 | | entry.AddExpirationToken(invalidationRequestToken); |
| | 526 | 84 | | entry.SetAbsoluteExpiration(cacheManager.CachingOptions.Value.CacheDuration); |
| | 526 | 85 | | return await factory(); |
| | 1242 | 86 | | }); |
| | 716 | 87 | | } |
| | | 88 | | } |