Browse Source

Merge pull request #18412 from abpframework/Clear-Tenant-Cache

Clear the tenant cache when creating/update/deleting.
pull/18654/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
3221e890c4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientCacheOptions.cs
  2. 1
      framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientModule.cs
  3. 118
      framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs
  4. 43
      framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantConfigurationCacheItem.cs
  5. 35
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantCacheItem.cs
  6. 23
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantCacheItemInvalidator.cs
  7. 42
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs
  8. 9
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs
  9. 18
      modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantStore.cs
  10. 59
      modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs
  11. 3
      modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs

3
framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientCacheOptions.cs

@ -4,13 +4,10 @@ namespace Volo.Abp.AspNetCore.Mvc.Client;
public class AbpAspNetCoreMvcClientCacheOptions public class AbpAspNetCoreMvcClientCacheOptions
{ {
public TimeSpan TenantConfigurationCacheAbsoluteExpiration { get; set; }
public TimeSpan ApplicationConfigurationDtoCacheAbsoluteExpiration { get; set; } public TimeSpan ApplicationConfigurationDtoCacheAbsoluteExpiration { get; set; }
public AbpAspNetCoreMvcClientCacheOptions() public AbpAspNetCoreMvcClientCacheOptions()
{ {
TenantConfigurationCacheAbsoluteExpiration = TimeSpan.FromMinutes(5);
ApplicationConfigurationDtoCacheAbsoluteExpiration = TimeSpan.FromSeconds(300); ApplicationConfigurationDtoCacheAbsoluteExpiration = TimeSpan.FromSeconds(300);
} }
} }

1
framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientModule.cs

@ -18,7 +18,6 @@ public class AbpAspNetCoreMvcClientModule : AbpModule
{ {
Configure<AbpAspNetCoreMvcClientCacheOptions>(options => Configure<AbpAspNetCoreMvcClientCacheOptions>(options =>
{ {
options.TenantConfigurationCacheAbsoluteExpiration = TimeSpan.FromSeconds(5);
options.ApplicationConfigurationDtoCacheAbsoluteExpiration = TimeSpan.FromSeconds(5); options.ApplicationConfigurationDtoCacheAbsoluteExpiration = TimeSpan.FromSeconds(5);
}); });
} }

118
framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs

@ -1,10 +1,8 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Pages.Abp.MultiTenancy.ClientProxies; using Pages.Abp.MultiTenancy.ClientProxies;
using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -16,13 +14,13 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
{ {
protected AbpTenantClientProxy TenantAppService { get; } protected AbpTenantClientProxy TenantAppService { get; }
protected IHttpContextAccessor HttpContextAccessor { get; } protected IHttpContextAccessor HttpContextAccessor { get; }
protected IDistributedCache<TenantConfiguration> Cache { get; } protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
protected AbpAspNetCoreMvcClientCacheOptions Options { get; } protected AbpAspNetCoreMvcClientCacheOptions Options { get; }
public MvcRemoteTenantStore( public MvcRemoteTenantStore(
AbpTenantClientProxy tenantAppService, AbpTenantClientProxy tenantAppService,
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IDistributedCache<TenantConfiguration> cache, IDistributedCache<TenantConfigurationCacheItem> cache,
IOptions<AbpAspNetCoreMvcClientCacheOptions> options) IOptions<AbpAspNetCoreMvcClientCacheOptions> options)
{ {
TenantAppService = tenantAppService; TenantAppService = tenantAppService;
@ -33,129 +31,101 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
public async Task<TenantConfiguration?> FindAsync(string name) public async Task<TenantConfiguration?> FindAsync(string name)
{ {
var cacheKey = CreateCacheKey(name); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
{ {
return tenantConfiguration; return tenantConfigurationInHttpContext?.Value;
} }
tenantConfiguration = (await Cache.GetOrAddAsync( var tenantConfiguration = await Cache.GetAsync(cacheKey);
cacheKey, if (tenantConfiguration == null)
async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name))!, {
() => new DistributedCacheEntryOptions await TenantAppService.FindTenantByNameAsync(name);
{ tenantConfiguration = await Cache.GetAsync(cacheKey);
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration }
}
))!;
if (httpContext != null) if (httpContext != null)
{ {
httpContext.Items[cacheKey] = tenantConfiguration; httpContext.Items[cacheKey] = tenantConfiguration;
} }
return tenantConfiguration; return tenantConfiguration?.Value;
} }
public async Task<TenantConfiguration?> FindAsync(Guid id) public async Task<TenantConfiguration?> FindAsync(Guid id)
{ {
var cacheKey = CreateCacheKey(id); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
{ {
return tenantConfiguration; return tenantConfigurationInHttpContext?.Value;
} }
tenantConfiguration = (await Cache.GetOrAddAsync( var tenantConfiguration = await Cache.GetAsync(cacheKey);
cacheKey, if (tenantConfiguration == null)
async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id))!, {
() => new DistributedCacheEntryOptions await TenantAppService.FindTenantByIdAsync(id);
{ tenantConfiguration = await Cache.GetAsync(cacheKey);
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration }
}
))!;
if (httpContext != null) if (httpContext != null)
{ {
httpContext.Items[cacheKey] = tenantConfiguration; httpContext.Items[cacheKey] = tenantConfiguration;
} }
return tenantConfiguration; return tenantConfiguration?.Value;
} }
public TenantConfiguration Find(string name) public TenantConfiguration? Find(string name)
{ {
var cacheKey = CreateCacheKey(name); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
{ {
return tenantConfiguration; return tenantConfigurationInHttpContext?.Value;
} }
tenantConfiguration = Cache.GetOrAdd( var tenantConfiguration = Cache.Get(cacheKey);
cacheKey, if (tenantConfiguration == null)
() => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name))!), {
() => new DistributedCacheEntryOptions AsyncHelper.RunSync(async () => await TenantAppService.FindTenantByNameAsync(name));
{ tenantConfiguration = Cache.Get(cacheKey);
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration }
}
)!;
if (httpContext != null) if (httpContext != null)
{ {
httpContext.Items[cacheKey] = tenantConfiguration; httpContext.Items[cacheKey] = tenantConfiguration;
} }
return tenantConfiguration; return tenantConfiguration?.Value;
} }
public TenantConfiguration Find(Guid id) public TenantConfiguration? Find(Guid id)
{ {
var cacheKey = CreateCacheKey(id); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
{ {
return tenantConfiguration; return tenantConfigurationInHttpContext?.Value;
} }
tenantConfiguration = Cache.GetOrAdd( var tenantConfiguration = Cache.Get(cacheKey);
cacheKey, if (tenantConfiguration == null)
() => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id))!),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration
}
)!;
if (httpContext != null)
{ {
httpContext.Items[cacheKey] = tenantConfiguration; AsyncHelper.RunSync(async () => await TenantAppService.FindTenantByIdAsync(id));
tenantConfiguration = Cache.Get(cacheKey);
} }
return tenantConfiguration; if (httpContext != null)
}
protected virtual TenantConfiguration? CreateTenantConfiguration(FindTenantResultDto tenantResultDto)
{
if (!tenantResultDto.Success || tenantResultDto.TenantId == null)
{ {
return null; httpContext.Items[cacheKey] = tenantConfiguration;
} }
return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name!); return tenantConfiguration?.Value;
}
protected virtual string CreateCacheKey(string tenantName)
{
return $"RemoteTenantStore_Name_{tenantName}";
}
protected virtual string CreateCacheKey(Guid tenantId)
{
return $"RemoteTenantStore_Id_{tenantId:N}";
} }
} }

43
framework/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantConfigurationCacheItem.cs

@ -0,0 +1,43 @@
using System;
namespace Volo.Abp.MultiTenancy;
[Serializable]
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
private const string CacheKeyFormat = "i:{0},n:{1}";
public TenantConfiguration? Value { get; set; }
public TenantConfigurationCacheItem()
{
}
public TenantConfigurationCacheItem(TenantConfiguration? value)
{
Value = value;
}
public static string CalculateCacheKey(Guid? id, string? name)
{
if (id == null && name.IsNullOrWhiteSpace())
{
throw new AbpException("Both id and name can't be invalid.");
}
return string.Format(CacheKeyFormat,
id?.ToString() ?? "null",
(name.IsNullOrWhiteSpace() ? "null" : name));
}
public static string CalculateCacheKey(Guid id)
{
return string.Format(CacheKeyFormat, id.ToString(), "null" );
}
public static string CalculateCacheKey(string name)
{
return string.Format(CacheKeyFormat, "null", name);
}
}

35
modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantCacheItem.cs

@ -1,35 +0,0 @@
using System;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TenantManagement;
[Serializable]
[IgnoreMultiTenancy]
public class TenantCacheItem
{
private const string CacheKeyFormat = "i:{0},n:{1}";
public TenantConfiguration Value { get; set; }
public TenantCacheItem()
{
}
public TenantCacheItem(TenantConfiguration value)
{
Value = value;
}
public static string CalculateCacheKey(Guid? id, string name)
{
if (id == null && name.IsNullOrWhiteSpace())
{
throw new AbpException("Both id and name can't be invalid.");
}
return string.Format(CacheKeyFormat,
id?.ToString() ?? "null",
(name.IsNullOrWhiteSpace() ? "null" : name));
}
}

23
modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantCacheItemInvalidator.cs

@ -1,23 +0,0 @@
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
namespace Volo.Abp.TenantManagement;
public class TenantCacheItemInvalidator : ILocalEventHandler<EntityChangedEventData<Tenant>>, ITransientDependency
{
protected IDistributedCache<TenantCacheItem> Cache { get; }
public TenantCacheItemInvalidator(IDistributedCache<TenantCacheItem> cache)
{
Cache = cache;
}
public virtual async Task HandleEventAsync(EntityChangedEventData<Tenant> eventData)
{
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.Entity.Id, null), considerUow: true);
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(null, eventData.Entity.Name), considerUow: true);
}
}

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

@ -0,0 +1,42 @@
using System;
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 Volo.Abp.TenantManagement;
public class TenantConfigurationCacheItemInvalidator :
ILocalEventHandler<EntityChangedEventData<Tenant>>,
ILocalEventHandler<EntityDeletedEventData<Tenant>>, ITransientDependency
{
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
public TenantConfigurationCacheItemInvalidator(IDistributedCache<TenantConfigurationCacheItem> cache)
{
Cache = cache;
}
public virtual async Task HandleEventAsync(EntityChangedEventData<Tenant> eventData)
{
await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.Name);
}
public virtual async Task HandleEventAsync(EntityDeletedEventData<Tenant> eventData)
{
await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.Name);
}
protected virtual async Task ClearCacheAsync(Guid? id, string name)
{
await Cache.RemoveManyAsync(
new[]
{
TenantConfigurationCacheItem.CalculateCacheKey(id, null),
TenantConfigurationCacheItem.CalculateCacheKey(null, name),
});
}
}

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

@ -1,17 +1,21 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TenantManagement; namespace Volo.Abp.TenantManagement;
public class TenantManager : DomainService, ITenantManager public class TenantManager : DomainService, ITenantManager
{ {
protected ITenantRepository TenantRepository { get; } protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
public TenantManager(ITenantRepository tenantRepository) public TenantManager(ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> cache)
{ {
TenantRepository = tenantRepository; TenantRepository = tenantRepository;
Cache = cache;
} }
public virtual async Task<Tenant> CreateAsync(string name) public virtual async Task<Tenant> CreateAsync(string name)
@ -28,6 +32,7 @@ public class TenantManager : DomainService, ITenantManager
Check.NotNull(name, nameof(name)); Check.NotNull(name, nameof(name));
await ValidateNameAsync(name, tenant.Id); await ValidateNameAsync(name, tenant.Id);
await Cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name));
tenant.SetName(name); tenant.SetName(name);
} }

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

@ -13,13 +13,13 @@ public class TenantStore : ITenantStore, ITransientDependency
protected ITenantRepository TenantRepository { get; } protected ITenantRepository TenantRepository { get; }
protected IObjectMapper<AbpTenantManagementDomainModule> ObjectMapper { get; } protected IObjectMapper<AbpTenantManagementDomainModule> ObjectMapper { get; }
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected IDistributedCache<TenantCacheItem> Cache { get; } protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
public TenantStore( public TenantStore(
ITenantRepository tenantRepository, ITenantRepository tenantRepository,
IObjectMapper<AbpTenantManagementDomainModule> objectMapper, IObjectMapper<AbpTenantManagementDomainModule> objectMapper,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IDistributedCache<TenantCacheItem> cache) IDistributedCache<TenantConfigurationCacheItem> cache)
{ {
TenantRepository = tenantRepository; TenantRepository = tenantRepository;
ObjectMapper = objectMapper; ObjectMapper = objectMapper;
@ -49,7 +49,7 @@ public class TenantStore : ITenantStore, ITransientDependency
return (GetCacheItem(id, null)).Value; return (GetCacheItem(id, null)).Value;
} }
protected virtual async Task<TenantCacheItem> GetCacheItemAsync(Guid? id, string name) protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemAsync(Guid? id, string name)
{ {
var cacheKey = CalculateCacheKey(id, name); var cacheKey = CalculateCacheKey(id, name);
@ -80,16 +80,16 @@ public class TenantStore : ITenantStore, ITransientDependency
throw new AbpException("Both id and name can't be invalid."); throw new AbpException("Both id and name can't be invalid.");
} }
protected virtual async Task<TenantCacheItem> SetCacheAsync(string cacheKey, [CanBeNull] Tenant tenant) protected virtual async Task<TenantConfigurationCacheItem> SetCacheAsync(string cacheKey, [CanBeNull] Tenant tenant)
{ {
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null; var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantCacheItem(tenantConfiguration); var cacheItem = new TenantConfigurationCacheItem(tenantConfiguration);
await Cache.SetAsync(cacheKey, cacheItem, considerUow: true); await Cache.SetAsync(cacheKey, cacheItem, considerUow: true);
return cacheItem; return cacheItem;
} }
[Obsolete("Use GetCacheItemAsync method.")] [Obsolete("Use GetCacheItemAsync method.")]
protected virtual TenantCacheItem GetCacheItem(Guid? id, string name) protected virtual TenantConfigurationCacheItem GetCacheItem(Guid? id, string name)
{ {
var cacheKey = CalculateCacheKey(id, name); var cacheKey = CalculateCacheKey(id, name);
@ -121,16 +121,16 @@ public class TenantStore : ITenantStore, ITransientDependency
} }
[Obsolete("Use SetCacheAsync method.")] [Obsolete("Use SetCacheAsync method.")]
protected virtual TenantCacheItem SetCache(string cacheKey, [CanBeNull] Tenant tenant) protected virtual TenantConfigurationCacheItem SetCache(string cacheKey, [CanBeNull] Tenant tenant)
{ {
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null; var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantCacheItem(tenantConfiguration); var cacheItem = new TenantConfigurationCacheItem(tenantConfiguration);
Cache.Set(cacheKey, cacheItem, considerUow: true); Cache.Set(cacheKey, cacheItem, considerUow: true);
return cacheItem; return cacheItem;
} }
protected virtual string CalculateCacheKey(Guid? id, string name) protected virtual string CalculateCacheKey(Guid? id, string name)
{ {
return TenantCacheItem.CalculateCacheKey(id, name); return TenantConfigurationCacheItem.CalculateCacheKey(id, name);
} }
} }

59
modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs

@ -6,17 +6,19 @@ using Xunit;
namespace Volo.Abp.TenantManagement; namespace Volo.Abp.TenantManagement;
public class TenantCacheItemInvalidator_Tests : AbpTenantManagementDomainTestBase public class TenantConfigurationCacheItemInvalidator_Tests : AbpTenantManagementDomainTestBase
{ {
private readonly IDistributedCache<TenantCacheItem> _cache; private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
private readonly ITenantStore _tenantStore; private readonly ITenantStore _tenantStore;
private readonly ITenantRepository _tenantRepository; private readonly ITenantRepository _tenantRepository;
private readonly ITenantManager _tenantManager;
public TenantCacheItemInvalidator_Tests() public TenantConfigurationCacheItemInvalidator_Tests()
{ {
_cache = GetRequiredService<IDistributedCache<TenantCacheItem>>(); _cache = GetRequiredService<IDistributedCache<TenantConfigurationCacheItem>>();
_tenantStore = GetRequiredService<ITenantStore>(); _tenantStore = GetRequiredService<ITenantStore>();
_tenantRepository = GetRequiredService<ITenantRepository>(); _tenantRepository = GetRequiredService<ITenantRepository>();
_tenantManager = GetRequiredService<ITenantManager>();
} }
[Fact] [Fact]
@ -25,27 +27,27 @@ public class TenantCacheItemInvalidator_Tests : AbpTenantManagementDomainTestBas
var acme = await _tenantRepository.FindByNameAsync("acme"); var acme = await _tenantRepository.FindByNameAsync("acme");
acme.ShouldNotBeNull(); acme.ShouldNotBeNull();
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull();
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.Name))).ShouldBeNull();
await _tenantStore.FindAsync(acme.Id); await _tenantStore.FindAsync(acme.Id);
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull();
await _tenantStore.FindAsync(acme.Name); await _tenantStore.FindAsync(acme.Name);
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldNotBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.Name))).ShouldNotBeNull();
var volosoft = _tenantRepository.FindByName("volosoft"); var volosoft = _tenantRepository.FindByName("volosoft");
volosoft.ShouldNotBeNull(); volosoft.ShouldNotBeNull();
(_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull();
(_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull();
_tenantStore.Find(volosoft.Id); _tenantStore.Find(volosoft.Id);
(_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldNotBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldNotBeNull();
_tenantStore.Find(volosoft.Name); _tenantStore.Find(volosoft.Name);
(_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldNotBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldNotBeNull();
} }
[Fact] [Fact]
@ -58,13 +60,13 @@ public class TenantCacheItemInvalidator_Tests : AbpTenantManagementDomainTestBas
await _tenantStore.FindAsync(acme.Id); await _tenantStore.FindAsync(acme.Id);
await _tenantStore.FindAsync(acme.Name); await _tenantStore.FindAsync(acme.Name);
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldNotBeNull();
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldNotBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.Name))).ShouldNotBeNull();
await _tenantRepository.DeleteAsync(acme); await _tenantRepository.DeleteAsync(acme);
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(acme.Id, null))).ShouldBeNull();
(await _cache.GetAsync(TenantCacheItem.CalculateCacheKey(null, acme.Name))).ShouldBeNull(); (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, acme.Name))).ShouldBeNull();
var volosoft = await _tenantRepository.FindByNameAsync("volosoft"); var volosoft = await _tenantRepository.FindByNameAsync("volosoft");
@ -74,12 +76,29 @@ public class TenantCacheItemInvalidator_Tests : AbpTenantManagementDomainTestBas
_tenantStore.Find(volosoft.Id); _tenantStore.Find(volosoft.Id);
_tenantStore.Find(volosoft.Name); _tenantStore.Find(volosoft.Name);
(_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldNotBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldNotBeNull();
(_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldNotBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldNotBeNull();
await _tenantRepository.DeleteAsync(volosoft); await _tenantRepository.DeleteAsync(volosoft);
(_cache.Get(TenantCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull();
(_cache.Get(TenantCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull();
var abp = await _tenantRepository.FindByNameAsync("abp");
abp.ShouldNotBeNull();
// Find will cache tenant.
await _tenantStore.FindAsync(abp.Id);
await _tenantStore.FindAsync(abp.Name);
(await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(abp.Id, null))).ShouldNotBeNull();
(await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, abp.Name))).ShouldNotBeNull();
await _tenantManager.ChangeNameAsync(abp, "abp2");
await _tenantRepository.UpdateAsync(abp);
(await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(abp.Id, null))).ShouldBeNull();
(await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, "abp"))).ShouldBeNull();
(await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, "abp2"))).ShouldBeNull();
} }
} }

3
modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs

@ -32,5 +32,8 @@ public class AbpTenantManagementTestDataBuilder : ITransientDependency
var volosoft = await _tenantManager.CreateAsync("volosoft"); var volosoft = await _tenantManager.CreateAsync("volosoft");
await _tenantRepository.InsertAsync(volosoft); await _tenantRepository.InsertAsync(volosoft);
var abp = await _tenantManager.CreateAsync("abp");
await _tenantRepository.InsertAsync(abp);
} }
} }

Loading…
Cancel
Save