14 changed files with 171 additions and 15 deletions
@ -0,0 +1,21 @@ |
|||
using System; |
|||
|
|||
namespace LINGYUN.Abp.TextTemplating; |
|||
|
|||
public class AbpTextTemplatingCachingOptions |
|||
{ |
|||
/// <summary>
|
|||
/// 文本模板缓存最小过期时间
|
|||
/// </summary>
|
|||
public TimeSpan? MinimumCacheDuration { get; set; } |
|||
/// <summary>
|
|||
/// 文本模板缓存绝对过期时间
|
|||
/// </summary>
|
|||
public TimeSpan? MaximumCacheDuration { get; set; } |
|||
|
|||
public AbpTextTemplatingCachingOptions() |
|||
{ |
|||
MinimumCacheDuration = TimeSpan.FromHours(1); |
|||
MaximumCacheDuration = TimeSpan.FromDays(30); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
namespace LINGYUN.Abp.TextTemplating; |
|||
|
|||
public class TemplateContentCacheItem |
|||
{ |
|||
private const string CacheKeyFormat = "pn:template-content,n:{0},c:{1}"; |
|||
|
|||
public string Name { get; set; } |
|||
public string Culture { get; set; } |
|||
public string Content { get; set; } |
|||
public TemplateContentCacheItem() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public TemplateContentCacheItem( |
|||
string name, |
|||
string content, |
|||
string culture = null) |
|||
{ |
|||
Name = name; |
|||
Content = content; |
|||
Culture = culture; |
|||
} |
|||
|
|||
public static string CalculateCacheKey( |
|||
string name, |
|||
string culture = null) |
|||
{ |
|||
return string.Format( |
|||
CacheKeyFormat, |
|||
name, |
|||
culture); |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.EventBus; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace LINGYUN.Abp.TextTemplating; |
|||
|
|||
public class TextTemplateCacheItemInvalidator : |
|||
ILocalEventHandler<EntityChangedEventData<TextTemplate>>, |
|||
ILocalEventHandler<EntityDeletedEventData<TextTemplate>>, |
|||
ITransientDependency |
|||
{ |
|||
protected ICurrentTenant CurrentTenant { get; } |
|||
|
|||
protected IDistributedCache<TemplateContentCacheItem> Cache { get; } |
|||
|
|||
public TextTemplateCacheItemInvalidator(IDistributedCache<TemplateContentCacheItem> cache, ICurrentTenant currentTenant) |
|||
{ |
|||
Cache = cache; |
|||
CurrentTenant = currentTenant; |
|||
} |
|||
|
|||
public async virtual Task HandleEventAsync(EntityChangedEventData<TextTemplate> eventData) |
|||
{ |
|||
await RemoveCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
public async virtual Task HandleEventAsync(EntityDeletedEventData<TextTemplate> eventData) |
|||
{ |
|||
await RemoveCacheItemAsync(eventData.Entity); |
|||
} |
|||
|
|||
protected async virtual Task RemoveCacheItemAsync(TextTemplate template) |
|||
{ |
|||
var cacheKey = CalculateCacheKey( |
|||
template.Name, |
|||
template.Culture |
|||
); |
|||
|
|||
using (CurrentTenant.Change(template.TenantId)) |
|||
{ |
|||
await Cache.RemoveAsync(cacheKey); |
|||
} |
|||
} |
|||
|
|||
protected virtual string CalculateCacheKey(string name, string culture = null) |
|||
{ |
|||
return TemplateContentCacheItem.CalculateCacheKey(name, culture); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue