Skip to content

Commit

Permalink
Clear tenant cache on name change.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 8, 2023
1 parent 037147e commit 58f31c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -28,6 +32,7 @@ public virtual async Task ChangeNameAsync(Tenant tenant, string name)
Check.NotNull(name, nameof(name));

await ValidateNameAsync(name, tenant.Id);
await Cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name));
tenant.SetName(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -81,5 +83,22 @@ public async Task Cache_Should_Invalidator_When_Tenant_Changed()

(_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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ private async Task AddTenantsAsync()

var volosoft = await _tenantManager.CreateAsync("volosoft");
await _tenantRepository.InsertAsync(volosoft);

var abp = await _tenantManager.CreateAsync("abp");
await _tenantRepository.InsertAsync(abp);
}
}

0 comments on commit 58f31c8

Please sign in to comment.