Browse Source

configurable webhook caching

pull/765/head
cKey 3 years ago
parent
commit
26632e5633
  1. 8
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DynamicWebhookDefinitionStore.cs
  2. 31
      aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookManagementOptions.cs

8
aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/DynamicWebhookDefinitionStore.cs

@ -22,7 +22,7 @@ public class DynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ITr
protected IDynamicWebhookDefinitionStoreCache StoreCache { get; } protected IDynamicWebhookDefinitionStoreCache StoreCache { get; }
protected IDistributedCache DistributedCache { get; } protected IDistributedCache DistributedCache { get; }
protected IAbpDistributedLock DistributedLock { get; } protected IAbpDistributedLock DistributedLock { get; }
public WebhookManagementOptions WebhookManagementOptions { get; } protected WebhookManagementOptions WebhookManagementOptions { get; }
protected AbpDistributedCacheOptions CacheOptions { get; } protected AbpDistributedCacheOptions CacheOptions { get; }
public DynamicWebhookDefinitionStore( public DynamicWebhookDefinitionStore(
@ -90,7 +90,7 @@ public class DynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ITr
protected virtual async Task EnsureCacheIsUptoDateAsync() protected virtual async Task EnsureCacheIsUptoDateAsync()
{ {
if (StoreCache.LastCheckTime.HasValue && if (StoreCache.LastCheckTime.HasValue &&
DateTime.Now.Subtract(StoreCache.LastCheckTime.Value).TotalSeconds < 30) DateTime.Now.Subtract(StoreCache.LastCheckTime.Value) < WebhookManagementOptions.WebhooksCacheRefreshInterval)
{ {
/* We get the latest webhook with a small delay for optimization */ /* We get the latest webhook with a small delay for optimization */
return; return;
@ -129,7 +129,7 @@ public class DynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ITr
} }
await using (var commonLockHandle = await DistributedLock await using (var commonLockHandle = await DistributedLock
.TryAcquireAsync(GetCommonDistributedLockKey(), TimeSpan.FromMinutes(2))) .TryAcquireAsync(GetCommonDistributedLockKey(), WebhookManagementOptions.WebhooksCacheStampTimeOut))
{ {
if (commonLockHandle == null) if (commonLockHandle == null)
{ {
@ -152,7 +152,7 @@ public class DynamicWebhookDefinitionStore : IDynamicWebhookDefinitionStore, ITr
stampInDistributedCache, stampInDistributedCache,
new DistributedCacheEntryOptions new DistributedCacheEntryOptions
{ {
SlidingExpiration = TimeSpan.FromDays(30) //TODO: Make it configurable? SlidingExpiration = WebhookManagementOptions.WebhooksCacheStampExpiration
} }
); );
} }

31
aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookManagementOptions.cs

@ -1,12 +1,39 @@
namespace LINGYUN.Abp.WebhooksManagement; using System;
namespace LINGYUN.Abp.WebhooksManagement;
public class WebhookManagementOptions public class WebhookManagementOptions
{ {
/// <summary>
/// Default: true.
/// </summary>
public bool SaveStaticWebhooksToDatabase { get; set; } public bool SaveStaticWebhooksToDatabase { get; set; }
/// <summary>
/// Default: false.
/// </summary>
public bool IsDynamicWebhookStoreEnabled { get; set; } public bool IsDynamicWebhookStoreEnabled { get; set; }
/// <summary>
/// 缓存刷新时间
/// default: 30 seconds
/// </summary>
public TimeSpan WebhooksCacheRefreshInterval { get; set; }
/// <summary>
/// 申请时间戳超时时间
/// default: 2 minutes
/// </summary>
public TimeSpan WebhooksCacheStampTimeOut { get; set; }
/// <summary>
/// 时间戳过期时间
/// default: 30 minutes
/// </summary>
public TimeSpan WebhooksCacheStampExpiration { get; set; }
public WebhookManagementOptions() public WebhookManagementOptions()
{ {
IsDynamicWebhookStoreEnabled = true; IsDynamicWebhookStoreEnabled = true;
SaveStaticWebhooksToDatabase = true; SaveStaticWebhooksToDatabase = true;
WebhooksCacheRefreshInterval = TimeSpan.FromSeconds(30);
WebhooksCacheStampTimeOut = TimeSpan.FromMinutes(2);
// 30分钟过期重新刷新缓存
WebhooksCacheStampExpiration = TimeSpan.FromMinutes(30);
} }
} }

Loading…
Cancel
Save