erp-platform/api/modules/Kurs.Languages/Kurs.Languages.Domain/Languages/LanguageTextCacheItemInvalidatorDistributed.cs
Sedat ÖZTÜRK e1a9562b22 init project
2025-05-06 09:45:49 +03:00

77 lines
2.5 KiB
C#

using Kurs.Languages.Entities;
using System;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
namespace Kurs.Languages;
public class LanguageTextCacheItemInvalidatorDistributed :
IDistributedEventHandler<EntityCreatedEto<LanguageTextEto>>,
IDistributedEventHandler<EntityUpdatedEto<LanguageTextEto>>,
IDistributedEventHandler<EntityDeletedEto<LanguageTextEto>>,
ITransientDependency
{
private readonly IRepository<LanguageText, Guid> languageTextRepository;
private readonly IDistributedCache<LanguageTextCacheItem> cache;
public LanguageTextCacheItemInvalidatorDistributed(
IRepository<LanguageText, Guid> languageTextRepository,
IDistributedCache<LanguageTextCacheItem> cache)
{
this.languageTextRepository = languageTextRepository;
this.cache = cache;
}
public async Task HandleEventAsync(EntityCreatedEto<LanguageTextEto> eventData)
{
await InvalidateCacheAsync(eventData.Entity);
}
public async Task HandleEventAsync(EntityUpdatedEto<LanguageTextEto> eventData)
{
await InvalidateCacheAsync(eventData.Entity);
}
public async Task HandleEventAsync(EntityDeletedEto<LanguageTextEto> eventData)
{
await InvalidateCacheAsync(eventData.Entity);
}
private async Task InvalidateCacheAsync(LanguageTextEto eventData)
{
var key = LanguageTextCacheItem.CalculateCacheKey(eventData.ResourceName, eventData.CultureName);
await cache.RemoveAsync(key);
}
}
public class LanguageTextEto
{
public string CultureName { get; set; }
public string ResourceName { get; set; }
}
// For distributed event bus
//private void ConfigureDistributedEventBus()
//{
// Configure<AbpDistributedEntityEventOptions>(options =>
// {
// //Enable for all entities
// //options.AutoEventSelectors.AddAll();
// //Enable for a single entity
// options.AutoEventSelectors.Add<LanguageText>();
// options.EtoMappings.Add<LanguageText, LanguageTextEto>();
// //Enable for all entities in a namespace (and child namespaces)
// //options.AutoEventSelectors.AddNamespace("MyProject.Products");
// //Custom predicate expression that should return true to select a type
// //options.AutoEventSelectors.Add(type => type.Namespace.StartsWith("MyProject."));
// });
//}