Clear tenant cache on name change.

pull/18412/head
maliming 1 year ago
parent 037147ed47
commit 58f31c88a0
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

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

@ -11,12 +11,14 @@ public class TenantConfigurationCacheItemInvalidator_Tests : AbpTenantManagement
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
private readonly ITenantStore _tenantStore;
private readonly ITenantRepository _tenantRepository;
private readonly ITenantManager _tenantManager;
public TenantConfigurationCacheItemInvalidator_Tests()
{
_cache = GetRequiredService<IDistributedCache<TenantConfigurationCacheItem>>();
_tenantStore = GetRequiredService<ITenantStore>();
_tenantRepository = GetRequiredService<ITenantRepository>();
_tenantManager = GetRequiredService<ITenantManager>();
}
[Fact]
@ -81,5 +83,22 @@ public class TenantConfigurationCacheItemInvalidator_Tests : AbpTenantManagement
(_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).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();
}
}

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

Loading…
Cancel
Save