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

39
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.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,6 +13,7 @@ namespace LINGYUN.Abp.TextTemplating;
public class TextTemplateContentContributor : ITemplateContentContributor, ITransientDependency public class TextTemplateContentContributor : ITemplateContentContributor, ITransientDependency
{ {
public ILogger<TextTemplateContentContributor> Logger { protected get; set; }
protected AbpTextTemplatingCachingOptions TemplatingCachingOptions { get; } protected AbpTextTemplatingCachingOptions TemplatingCachingOptions { get; }
protected IDistributedCache<TextTemplateContentCacheItem> TextTemplateContentCache { get; } protected IDistributedCache<TextTemplateContentCacheItem> TextTemplateContentCache { get; }
@ -20,39 +23,49 @@ public class TextTemplateContentContributor : ITemplateContentContributor, ITran
{ {
TextTemplateContentCache = textTemplateContentCache; TextTemplateContentCache = textTemplateContentCache;
TemplatingCachingOptions = templatingCachingOptions.Value; TemplatingCachingOptions = templatingCachingOptions.Value;
Logger = NullLogger<TextTemplateContentContributor>.Instance;
} }
public async virtual Task<string> GetOrNullAsync(TemplateContentContributorContext context) public async virtual Task<string> GetOrNullAsync(TemplateContentContributorContext context)
{ {
// 2024/05/27 fixed 内联本地化不需要多语言 return (await GetCacheItemAsync(context)).Content;
var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture; }
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 cacheKey = TextTemplateContentCacheItem.CalculateCacheKey(context.TemplateDefinition.Name, culture);
var cacheItem = await TextTemplateContentCache.GetOrAddAsync(cacheKey, Logger.LogDebug($"TextTemplateContentContributor.GetCacheItemAsync: {cacheKey}");
() => CreateTemplateContentCache(context),
() => CreateTemplateContentCacheOptions());
return cacheItem?.Content; var cacheItem = await TextTemplateContentCache.GetAsync(cacheKey);
}
protected async virtual Task<TextTemplateContentCacheItem> CreateTemplateContentCache(TemplateContentContributorContext context) if (cacheItem != null)
{ {
// 2024/05/27 fixed 内联本地化不需要多语言 Logger.LogDebug($"TextTemplateContent found in the cache: {cacheKey}");
var culture = context.TemplateDefinition.IsInlineLocalized ? null : context.Culture; return cacheItem;
}
Logger.LogDebug($"TextTemplateContent not found in the cache: {cacheKey}");
var repository = context.ServiceProvider.GetRequiredService<ITextTemplateRepository>(); var repository = context.ServiceProvider.GetRequiredService<ITextTemplateRepository>();
var template = await repository.FindByNameAsync(context.TemplateDefinition.Name, culture); var template = await repository.FindByNameAsync(context.TemplateDefinition.Name, culture);
// 2025/06/23 fixed 非内联本地化模板内容为空时,回退到默认文化
if (template == null && !culture.IsNullOrWhiteSpace()) if (template == null && !culture.IsNullOrWhiteSpace())
{ {
template = await repository.FindByNameAsync(context.TemplateDefinition.Name, context.TemplateDefinition.DefaultCultureName); template = await repository.FindByNameAsync(context.TemplateDefinition.Name, context.TemplateDefinition.DefaultCultureName);
} }
return new TextTemplateContentCacheItem( cacheItem = new TextTemplateContentCacheItem(
template?.Name, template?.Name,
template?.Content, template?.Content,
template?.Culture); template?.Culture);
Logger.LogDebug($"TextTemplateContent set cache item: {cacheKey}");
await TextTemplateContentCache.SetAsync(cacheKey, cacheItem, CreateTemplateContentCacheOptions());
return cacheItem;
} }
protected DistributedCacheEntryOptions CreateTemplateContentCacheOptions() protected DistributedCacheEntryOptions CreateTemplateContentCacheOptions()

Loading…
Cancel
Save