Skip to content

Commit

Permalink
Clear the tenant cache when creating/update/deleting.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 8, 2023
1 parent 535443c commit bf44f64
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency

public async Task<TenantConfiguration?> FindAsync(string name)
{
var cacheKey = CreateCacheKey(name);
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext;

if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
Expand All @@ -60,7 +60,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency

public async Task<TenantConfiguration?> FindAsync(Guid id)
{
var cacheKey = CreateCacheKey(id);
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext;

if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
Expand All @@ -87,7 +87,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency

public TenantConfiguration Find(string name)
{
var cacheKey = CreateCacheKey(name);
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext;

if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
Expand All @@ -114,7 +114,7 @@ public TenantConfiguration Find(string name)

public TenantConfiguration Find(Guid id)
{
var cacheKey = CreateCacheKey(id);
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext;

if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
Expand Down Expand Up @@ -148,14 +148,4 @@ public TenantConfiguration Find(Guid id)

return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name!);
}

protected virtual string CreateCacheKey(string tenantName)
{
return $"RemoteTenantStore_Name_{tenantName}";
}

protected virtual string CreateCacheKey(Guid tenantId)
{
return $"RemoteTenantStore_Id_{tenantId:N}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy;

public static class TenantConfigurationCacheHelper
{
public static string CreateCacheKey(string tenantName)
{
return $"RemoteTenantStore_Name_{tenantName}";
}

public static string CreateCacheKey(Guid tenantId)
{
return $"RemoteTenantStore_Id_{tenantId:N}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ProjectReference Include="..\Volo.Abp.TenantManagement.Domain.Shared\Volo.Abp.TenantManagement.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo.Abp.Ddd.Application.Contracts.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Authorization.Abstractions\Volo.Abp.Authorization.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AspNetCore.Mvc.Contracts\Volo.Abp.AspNetCore.Mvc.Contracts.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Volo.Abp.Application;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Authorization;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectExtending;
Expand All @@ -10,7 +11,8 @@ namespace Volo.Abp.TenantManagement;
[DependsOn(
typeof(AbpDddApplicationContractsModule),
typeof(AbpTenantManagementDomainSharedModule),
typeof(AbpAuthorizationAbstractionsModule)
typeof(AbpAuthorizationAbstractionsModule),
typeof(AbpAspNetCoreMvcContractsModule)
)]
public class AbpTenantManagementApplicationContractsModule : AbpModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
using Volo.Abp.Caching;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
Expand All @@ -17,17 +19,20 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
protected ITenantRepository TenantRepository { get; }
protected ITenantManager TenantManager { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected IDistributedCache<TenantConfiguration> TenantConfigurationCache { get; }

public TenantAppService(
ITenantRepository tenantRepository,
ITenantManager tenantManager,
IDataSeeder dataSeeder,
IDistributedEventBus distributedEventBus)
IDistributedEventBus distributedEventBus,
IDistributedCache<TenantConfiguration> tenantConfigurationCache)
{
DataSeeder = dataSeeder;
TenantRepository = tenantRepository;
TenantManager = tenantManager;
DistributedEventBus = distributedEventBus;
TenantConfigurationCache = tenantConfigurationCache;
}

public virtual async Task<TenantDto> GetAsync(Guid id)
Expand Down Expand Up @@ -91,6 +96,9 @@ public virtual async Task<TenantDto> CreateAsync(TenantCreateDto input)
);
}

await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));

return ObjectMapper.Map<Tenant, TenantDto>(tenant);
}

Expand All @@ -106,6 +114,9 @@ public virtual async Task<TenantDto> UpdateAsync(Guid id, TenantUpdateDto input)

await TenantRepository.UpdateAsync(tenant);

await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));

return ObjectMapper.Map<Tenant, TenantDto>(tenant);
}

Expand All @@ -119,6 +130,9 @@ public virtual async Task DeleteAsync(Guid id)
}

await TenantRepository.DeleteAsync(tenant);

await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));
}

[Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]
Expand Down

0 comments on commit bf44f64

Please sign in to comment.