Browse Source

Tenant cache problem fixed

pull/19288/head
ahmetfarukulu 3 years ago
parent
commit
38583ac0c1
  1. 16
      framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantChangedEvent.cs
  2. 10
      modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs
  3. 18
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs
  4. 12
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs

16
framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantChangedEvent.cs

@ -0,0 +1,16 @@
using System;
namespace Volo.Abp.MultiTenancy;
[Serializable]
public class TenantChangedEvent
{
public Guid? Id { get; set; }
public string? NormalizedName { get; set; }
public TenantChangedEvent(Guid? id = null, string? normalizedName = null)
{
Id = id;
NormalizedName = normalizedName;
}
}

10
modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus.Local;
using Volo.Abp.MultiTenancy;
using Volo.Abp.ObjectExtending;
@ -17,17 +18,20 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
protected ITenantRepository TenantRepository { get; }
protected ITenantManager TenantManager { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected ILocalEventBus LocalEventBus { get; }
public TenantAppService(
ITenantRepository tenantRepository,
ITenantManager tenantManager,
IDataSeeder dataSeeder,
IDistributedEventBus distributedEventBus)
IDistributedEventBus distributedEventBus,
ILocalEventBus localEventBus)
{
DataSeeder = dataSeeder;
TenantRepository = tenantRepository;
TenantManager = tenantManager;
DistributedEventBus = distributedEventBus;
LocalEventBus = localEventBus;
}
public virtual async Task<TenantDto> GetAsync(Guid id)
@ -132,6 +136,10 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
public virtual async Task UpdateDefaultConnectionStringAsync(Guid id, string defaultConnectionString)
{
var tenant = await TenantRepository.GetAsync(id);
if (tenant.FindDefaultConnectionString() != defaultConnectionString)
{
await LocalEventBus.PublishAsync(new TenantChangedEvent(tenant.Id, tenant.NormalizedName));
}
tenant.SetDefaultConnectionString(defaultConnectionString);
await TenantRepository.UpdateAsync(tenant);
}

18
modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs

@ -4,14 +4,16 @@ using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TenantManagement;
[LocalEventHandlerOrder(-1)]
public class TenantConfigurationCacheItemInvalidator :
ILocalEventHandler<EntityChangedEventData<Tenant>>,
ILocalEventHandler<EntityDeletedEventData<Tenant>>, ITransientDependency
ILocalEventHandler<TenantChangedEvent>,
ITransientDependency
{
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
@ -22,12 +24,17 @@ public class TenantConfigurationCacheItemInvalidator :
public virtual async Task HandleEventAsync(EntityChangedEventData<Tenant> eventData)
{
if (eventData is EntityCreatedEventData<Tenant>)
{
return;
}
await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.NormalizedName);
}
public virtual async Task HandleEventAsync(EntityDeletedEventData<Tenant> eventData)
public virtual async Task HandleEventAsync(TenantChangedEvent eventData)
{
await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.NormalizedName);
await ClearCacheAsync(eventData.Id, eventData.NormalizedName);
}
protected virtual async Task ClearCacheAsync(Guid? id, string normalizedName)
@ -37,6 +44,7 @@ public class TenantConfigurationCacheItemInvalidator :
{
TenantConfigurationCacheItem.CalculateCacheKey(id, null),
TenantConfigurationCacheItem.CalculateCacheKey(null, normalizedName),
TenantConfigurationCacheItem.CalculateCacheKey(id, normalizedName),
});
}
}
}

12
modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TenantManagement;
@ -9,14 +10,17 @@ namespace Volo.Abp.TenantManagement;
public class TenantManager : DomainService, ITenantManager
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
protected ITenantNormalizer TenantNormalizer { get; }
protected ILocalEventBus LocalEventBus { get; }
public TenantManager(ITenantRepository tenantRepository, IDistributedCache<TenantConfigurationCacheItem> cache, ITenantNormalizer tenantNormalizer)
public TenantManager(
ITenantRepository tenantRepository,
ITenantNormalizer tenantNormalizer,
ILocalEventBus localEventBus)
{
TenantRepository = tenantRepository;
Cache = cache;
TenantNormalizer = tenantNormalizer;
LocalEventBus = localEventBus;
}
public virtual async Task<Tenant> CreateAsync(string name)
@ -36,7 +40,7 @@ public class TenantManager : DomainService, ITenantManager
var normalizedName = TenantNormalizer.NormalizeName(name);
await ValidateNameAsync(normalizedName, tenant.Id);
await Cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.NormalizedName));
await LocalEventBus.PublishAsync(new TenantChangedEvent(tenant.Id, tenant.NormalizedName));
tenant.SetName(name);
tenant.SetNormalizedName(normalizedName);
}

Loading…
Cancel
Save