Browse Source

Merge pull request #1358 from colinin/fix-text-template-cache

fix(text-templating): Fix the issue where the changed text template i…
pull/1361/head
yx lin 3 months ago
committed by GitHub
parent
commit
1dcbe9bcdd
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/AbpTextTemplatingDomainModule.cs
  2. 41
      aspnet-core/modules/text-templating/LINGYUN.Abp.TextTemplating.Domain/LINGYUN/Abp/TextTemplating/TextTemplateContentContributor.cs

2
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<TextTemplate, TextTemplateEto>();
options.EtoMappings.Add<TextTemplateDefinition, TextTemplateDefinitionEto>();
options.AutoEventSelectors.Add<TextTemplate>();
});
if (context.Services.IsDataMigrationEnvironment())

41
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<TextTemplateContentContributor> Logger { protected get; set; }
protected AbpTextTemplatingCachingOptions TemplatingCachingOptions { get; }
protected IDistributedCache<TextTemplateContentCacheItem> TextTemplateContentCache { get; }
@ -20,39 +23,49 @@ public class TextTemplateContentContributor : ITemplateContentContributor, ITran
{
TextTemplateContentCache = textTemplateContentCache;
TemplatingCachingOptions = templatingCachingOptions.Value;
Logger = NullLogger<TextTemplateContentContributor>.Instance;
}
public async virtual Task<string> GetOrNullAsync(TemplateContentContributorContext context)
{
// 2024/05/27 fixed 内联本地化不需要多语言
var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture;
return (await GetCacheItemAsync(context)).Content;
}
protected async virtual Task<TextTemplateContentCacheItem> 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<TextTemplateContentCacheItem> CreateTemplateContentCache(TemplateContentContributorContext context)
{
// 2024/05/27 fixed 内联本地化不需要多语言
var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture;
var repository = context.ServiceProvider.GetRequiredService<ITextTemplateRepository>();
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()

Loading…
Cancel
Save