From fbaed545c505082eb708d2c87897843ec56b32a0 Mon Sep 17 00:00:00 2001 From: colin Date: Mon, 3 Nov 2025 13:57:21 +0800 Subject: [PATCH] fix(text-templating): Fix the issue where the changed text template is invalid --- .../AbpTextTemplatingDomainModule.cs | 2 + .../TextTemplateContentContributor.cs | 41 ++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/AbpTextTemplatingDomainModule.cs b/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/AbpTextTemplatingDomainModule.cs index 58e817f5b..00fd9644e 100644 --- a/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/AbpTextTemplatingDomainModule.cs +++ b/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/AbpTextTemplatingDomainModule.cs @@ -34,6 +34,8 @@ public class AbpTextTemplatingDomainModule : AbpModule { options.EtoMappings.Add(); options.EtoMappings.Add(); + + options.AutoEventSelectors.Add(); }); if (context.Services.IsDataMigrationEnvironment()) diff --git a/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/TextTemplateContentContributor.cs b/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/TextTemplateContentContributor.cs index 027a7b148..58c56fa83 100644 --- a/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/TextTemplateContentContributor.cs +++ b/aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/TextTemplateContentContributor.cs @@ -1,5 +1,7 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; @@ -11,6 +13,7 @@ namespace LINGYUN.Abp.TextTemplating; public class TextTemplateContentContributor : ITemplateContentContributor, ITransientDependency { + public ILogger Logger { protected get; set; } protected AbpTextTemplatingCachingOptions TemplatingCachingOptions { get; } protected IDistributedCache TextTemplateContentCache { get; } @@ -20,39 +23,49 @@ public class TextTemplateContentContributor : ITemplateContentContributor, ITran { TextTemplateContentCache = textTemplateContentCache; TemplatingCachingOptions = templatingCachingOptions.Value; + + Logger = NullLogger.Instance; } public async virtual Task GetOrNullAsync(TemplateContentContributorContext context) { - // 2024/05/27 fixed 内联本地化不需要多语言 - var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture; + return (await GetCacheItemAsync(context)).Content; + } + protected async virtual Task GetCacheItemAsync(TemplateContentContributorContext context) + { + var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture; var cacheKey = TextTemplateContentCacheItem.CalculateCacheKey(context.TemplateDefinition.Name, culture); - var cacheItem = await TextTemplateContentCache.GetOrAddAsync(cacheKey, - () => CreateTemplateContentCache(context), - () => CreateTemplateContentCacheOptions()); + Logger.LogDebug($"TextTemplateContentContributor.GetCacheItemAsync: {cacheKey}"); - return cacheItem?.Content; - } + var cacheItem = await TextTemplateContentCache.GetAsync(cacheKey); + + if (cacheItem != null) + { + Logger.LogDebug($"TextTemplateContent found in the cache: {cacheKey}"); + return cacheItem; + } + + Logger.LogDebug($"TextTemplateContent not found in the cache: {cacheKey}"); - protected async virtual Task CreateTemplateContentCache(TemplateContentContributorContext context) - { - // 2024/05/27 fixed 内联本地化不需要多语言 - var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture; var repository = context.ServiceProvider.GetRequiredService(); var template = await repository.FindByNameAsync(context.TemplateDefinition.Name, culture); - - // 2025/06/23 fixed 非内联本地化模板内容为空时,回退到默认文化 if (template == null && !culture.IsNullOrWhiteSpace()) { template = await repository.FindByNameAsync(context.TemplateDefinition.Name, context.TemplateDefinition.DefaultCultureName); } - return new TextTemplateContentCacheItem( + cacheItem = new TextTemplateContentCacheItem( template?.Name, template?.Content, template?.Culture); + + Logger.LogDebug($"TextTemplateContent set cache item: {cacheKey}"); + + await TextTemplateContentCache.SetAsync(cacheKey, cacheItem, CreateTemplateContentCacheOptions()); + + return cacheItem; } protected DistributedCacheEntryOptions CreateTemplateContentCacheOptions()