Browse Source

Merge pull request #695 from colinin/upt-5.3.4

cache key does not require a duplicate tenant id
pull/712/head
yx lin 3 years ago
committed by GitHub
parent
commit
546cdebd4a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent.QQ/LINGYUN/Abp/Tencent/QQ/AbpTencentQQCacheItem.cs
  2. 6
      aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent.QQ/LINGYUN/Abp/Tencent/QQ/AbpTencentQQOptionsManager.cs
  3. 13
      aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent/LINGYUN/Abp/Tencent/AbstractTencentCloudClientFactory.cs
  4. 10
      aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent/LINGYUN/Abp/Tencent/TencentCloudClientCacheItem.cs
  5. 20
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/ConnectionStringInvalidator.cs
  6. 16
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantCacheItemInvalidator.cs

6
aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent.QQ/LINGYUN/Abp/Tencent/QQ/AbpTencentQQCacheItem.cs

@ -4,7 +4,7 @@ namespace LINGYUN.Abp.Tencent.QQ;
public class AbpTencentQQCacheItem public class AbpTencentQQCacheItem
{ {
public const string CacheKeyFormat = "pn:{0},pk:{1}"; public const string CacheKeyFormat = "pn:tenant-cloud,n:qq";
public string AppId { get; set; } public string AppId { get; set; }
public string AppKey { get; set; } public string AppKey { get; set; }
public bool IsMobile { get; set; } public bool IsMobile { get; set; }
@ -23,8 +23,8 @@ public class AbpTencentQQCacheItem
IsMobile = isMobile; IsMobile = isMobile;
} }
public static string CalculateCacheKey(Guid? tenantId = null) public static string CalculateCacheKey()
{ {
return string.Format(CacheKeyFormat, tenantId.HasValue ? tenantId.Value.ToString() : "global", "tenant-qq"); return CacheKeyFormat;
} }
} }

6
aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent.QQ/LINGYUN/Abp/Tencent/QQ/AbpTencentQQOptionsManager.cs

@ -3,7 +3,6 @@ using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Options; using Volo.Abp.Options;
using Volo.Abp.Settings; using Volo.Abp.Settings;
@ -12,17 +11,14 @@ namespace LINGYUN.Abp.Tencent.QQ;
public class AbpTencentQQOptionsManager : AbpDynamicOptionsManager<AbpTencentQQOptions> public class AbpTencentQQOptionsManager : AbpDynamicOptionsManager<AbpTencentQQOptions>
{ {
protected IMemoryCache TencentCache { get; } protected IMemoryCache TencentCache { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
public AbpTencentQQOptionsManager( public AbpTencentQQOptionsManager(
IMemoryCache tencentCache, IMemoryCache tencentCache,
ICurrentTenant currentTenant,
ISettingProvider settingProvider, ISettingProvider settingProvider,
IOptionsFactory<AbpTencentQQOptions> factory) IOptionsFactory<AbpTencentQQOptions> factory)
: base(factory) : base(factory)
{ {
TencentCache = tencentCache; TencentCache = tencentCache;
CurrentTenant = currentTenant;
SettingProvider = settingProvider; SettingProvider = settingProvider;
} }
@ -37,7 +33,7 @@ public class AbpTencentQQOptionsManager : AbpDynamicOptionsManager<AbpTencentQQO
protected async virtual Task<AbpTencentQQCacheItem> GetCacheItemAsync() protected async virtual Task<AbpTencentQQCacheItem> GetCacheItemAsync()
{ {
var cacheKey = AbpTencentQQCacheItem.CalculateCacheKey(CurrentTenant.Id); var cacheKey = AbpTencentQQCacheItem.CalculateCacheKey();
var cacheItem = await TencentCache.GetOrCreateAsync( var cacheItem = await TencentCache.GetOrCreateAsync(
cacheKey, cacheKey,

13
aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent/LINGYUN/Abp/Tencent/AbstractTencentCloudClientFactory.cs

@ -2,7 +2,6 @@
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Settings; using Volo.Abp.Settings;
namespace LINGYUN.Abp.Tencent; namespace LINGYUN.Abp.Tencent;
@ -10,16 +9,13 @@ namespace LINGYUN.Abp.Tencent;
public abstract class AbstractTencentCloudClientFactory<TClient> public abstract class AbstractTencentCloudClientFactory<TClient>
{ {
protected IMemoryCache ClientCache { get; } protected IMemoryCache ClientCache { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
public AbstractTencentCloudClientFactory( public AbstractTencentCloudClientFactory(
IMemoryCache clientCache, IMemoryCache clientCache,
ICurrentTenant currentTenant,
ISettingProvider settingProvider) ISettingProvider settingProvider)
{ {
ClientCache = clientCache; ClientCache = clientCache;
CurrentTenant = currentTenant;
SettingProvider = settingProvider; SettingProvider = settingProvider;
} }
@ -35,12 +31,13 @@ public abstract class AbstractTencentCloudClientFactory<TClient>
protected async virtual Task<TencentCloudClientCacheItem> GetClientCacheItemAsync() protected async virtual Task<TencentCloudClientCacheItem> GetClientCacheItemAsync()
{ {
return await ClientCache.GetOrCreateAsync( return await ClientCache.GetOrCreateAsync(
TencentCloudClientCacheItem.CalculateCacheKey(CurrentTenant), TencentCloudClientCacheItem.CalculateCacheKey("client"),
async (_) => async (_) =>
{ {
var secretId = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretId); var secretId = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretId);
var secretKey = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretKey); var secretKey = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretKey);
var endpoint = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.EndPoint); var endpoint = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.EndPoint);
var durationSecond = await SettingProvider.GetAsync(TencentCloudSettingNames.DurationSecond, 3600);
Check.NotNullOrWhiteSpace(secretId, TencentCloudSettingNames.SecretId); Check.NotNullOrWhiteSpace(secretId, TencentCloudSettingNames.SecretId);
Check.NotNullOrWhiteSpace(secretKey, TencentCloudSettingNames.SecretKey); Check.NotNullOrWhiteSpace(secretKey, TencentCloudSettingNames.SecretKey);
@ -56,6 +53,7 @@ public abstract class AbstractTencentCloudClientFactory<TClient>
SecretKey = secretKey, SecretKey = secretKey,
// 连接区域 // 连接区域
EndPoint = endpoint, EndPoint = endpoint,
DurationSecond = durationSecond,
HttpMethod = method, HttpMethod = method,
WebProxy = webProxy, WebProxy = webProxy,
Timeout = timeout, Timeout = timeout,
@ -67,16 +65,13 @@ public abstract class AbstractTencentCloudClientFactory<TClient>
public abstract class AbstractTencentCloudClientFactory<TClient, TConfiguration> public abstract class AbstractTencentCloudClientFactory<TClient, TConfiguration>
{ {
protected IMemoryCache ClientCache { get; } protected IMemoryCache ClientCache { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
public AbstractTencentCloudClientFactory( public AbstractTencentCloudClientFactory(
IMemoryCache clientCache, IMemoryCache clientCache,
ICurrentTenant currentTenant,
ISettingProvider settingProvider) ISettingProvider settingProvider)
{ {
ClientCache = clientCache; ClientCache = clientCache;
CurrentTenant = currentTenant;
SettingProvider = settingProvider; SettingProvider = settingProvider;
} }
@ -92,7 +87,7 @@ public abstract class AbstractTencentCloudClientFactory<TClient, TConfiguration>
protected async virtual Task<TencentCloudClientCacheItem> GetClientCacheItemAsync() protected async virtual Task<TencentCloudClientCacheItem> GetClientCacheItemAsync()
{ {
return await ClientCache.GetOrCreateAsync( return await ClientCache.GetOrCreateAsync(
TencentCloudClientCacheItem.CalculateCacheKey(CurrentTenant), TencentCloudClientCacheItem.CalculateCacheKey("client"),
async (_) => async (_) =>
{ {
var secretId = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretId); var secretId = await SettingProvider.GetOrNullAsync(TencentCloudSettingNames.SecretId);

10
aspnet-core/modules/cloud-tencent/LINGYUN.Abp.Tencent/LINGYUN/Abp/Tencent/TencentCloudClientCacheItem.cs

@ -1,10 +1,8 @@
using Volo.Abp.MultiTenancy; namespace LINGYUN.Abp.Tencent;
namespace LINGYUN.Abp.Tencent;
public class TencentCloudClientCacheItem public class TencentCloudClientCacheItem
{ {
public const string CacheKeyFormat = "pn:{0},n:tenant-cloud"; public const string CacheKeyFormat = "pn:tenant-cloud,n:{0}";
public string SecretId { get; set; } public string SecretId { get; set; }
public string SecretKey { get; set; } public string SecretKey { get; set; }
public string EndPoint { get; set; } public string EndPoint { get; set; }
@ -14,8 +12,8 @@ public class TencentCloudClientCacheItem
public int Timeout { get; set; } public int Timeout { get; set; }
public int DurationSecond { get; set; } public int DurationSecond { get; set; }
public static string CalculateCacheKey(ICurrentTenant currentTenant) public static string CalculateCacheKey(string provider)
{ {
return string.Format(CacheKeyFormat, currentTenant.IsAvailable ? currentTenant.GetId().ToString() : "global"); return string.Format(CacheKeyFormat, provider);
} }
} }

20
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/ConnectionStringInvalidator.cs

@ -20,16 +20,24 @@ public class ConnectionStringInvalidator :
public async virtual Task HandleEventAsync(ConnectionStringCreatedEventData eventData) public async virtual Task HandleEventAsync(ConnectionStringCreatedEventData eventData)
{ {
// 需要考虑三种情形下的缓存键 // 需要考虑三种情形下的缓存键
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(null, eventData.TenantName), considerUow: true); var keys = new string[]
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.TenantId, null), considerUow: true); {
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.TenantId, eventData.TenantName), considerUow: true); TenantCacheItem.CalculateCacheKey(null, eventData.TenantName),
TenantCacheItem.CalculateCacheKey(eventData.TenantId, null),
TenantCacheItem.CalculateCacheKey(eventData.TenantId, eventData.TenantName),
};
await Cache.RemoveManyAsync(keys, considerUow: true);
} }
public async virtual Task HandleEventAsync(ConnectionStringDeletedEventData eventData) public async virtual Task HandleEventAsync(ConnectionStringDeletedEventData eventData)
{ {
// 需要考虑三种情形下的缓存键 // 需要考虑三种情形下的缓存键
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(null, eventData.TenantName), considerUow: true); var keys = new string[]
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.TenantId, null), considerUow: true); {
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.TenantId, eventData.TenantName), considerUow: true); TenantCacheItem.CalculateCacheKey(null, eventData.TenantName),
TenantCacheItem.CalculateCacheKey(eventData.TenantId, null),
TenantCacheItem.CalculateCacheKey(eventData.TenantId, eventData.TenantName),
};
await Cache.RemoveManyAsync(keys, considerUow: true);
} }
} }

16
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantCacheItemInvalidator.cs

@ -18,11 +18,15 @@ public class TenantCacheItemInvalidator : ILocalEventHandler<EntityChangedEventD
public async virtual Task HandleEventAsync(EntityChangedEventData<Tenant> eventData) public async virtual Task HandleEventAsync(EntityChangedEventData<Tenant> eventData)
{ {
// 需要考虑三种情形下的缓存键 var keys = new string[]
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(null, eventData.Entity.Name), considerUow: true); {
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.Entity.Id, null), considerUow: true); TenantCacheItem.CalculateCacheKey(null, eventData.Entity.Name),
await Cache.RemoveAsync(TenantCacheItem.CalculateCacheKey(eventData.Entity.Id, eventData.Entity.Name), considerUow: true); TenantCacheItem.CalculateCacheKey(eventData.Entity.Id, null),
// 同时移除租户版本缓存 TenantCacheItem.CalculateCacheKey(eventData.Entity.Id, eventData.Entity.Name),
await Cache.RemoveAsync(EditionCacheItem.CalculateCacheKey(eventData.Entity.Id), considerUow: true);
// 同时移除租户版本缓存
EditionCacheItem.CalculateCacheKey(eventData.Entity.Id)
};
await Cache.RemoveManyAsync(keys, considerUow: true);
} }
} }

Loading…
Cancel
Save