Browse Source

feat: Workspace change synchronization event

pull/1421/head
colin 1 week ago
parent
commit
855c98282f
  1. 42
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationExpiredHandler.cs
  2. 58
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/DynamicWorkspaceDefinitionStoreCacheInvalidator.cs

42
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationExpiredHandler.cs

@ -0,0 +1,42 @@
using LINGYUN.Abp.AIManagement.Workspaces;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.Specifications;
using Volo.Abp.Timing;
namespace LINGYUN.Abp.AIManagement.Chats;
public class ConversationExpiredHandler :
ILocalEventHandler<EntityDeletedEventData<WorkspaceDefinitionRecord>>,
ITransientDependency
{
private readonly IClock _clock;
private readonly IConversationRecordRepository _conversationRecordRepository;
public ConversationExpiredHandler(
IClock clock,
IConversationRecordRepository conversationRecordRepository)
{
_clock = clock;
_conversationRecordRepository = conversationRecordRepository;
}
public async virtual Task HandleEventAsync(EntityDeletedEventData<WorkspaceDefinitionRecord> eventData)
{
var specification = new ExpressionSpecification<ConversationRecord>(
x => x.Workspace == eventData.Entity.Name);
var totalCount = await _conversationRecordRepository.GetCountAsync(specification);
var conversations = await _conversationRecordRepository.GetListAsync(specification,
maxResultCount: totalCount);
foreach (var conversation in conversations )
{
conversation.ExpiredAt = _clock.Now;
}
await _conversationRecordRepository.UpdateManyAsync(conversations);
}
}

58
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/DynamicWorkspaceDefinitionStoreCacheInvalidator.cs

@ -0,0 +1,58 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.Threading;
using Volo.Abp.Timing;
namespace LINGYUN.Abp.AIManagement.Workspaces;
public class DynamicWorkspaceDefinitionStoreCacheInvalidator :
ILocalEventHandler<EntityChangedEventData<WorkspaceDefinitionRecord>>,
ITransientDependency
{
private readonly IDynamicWorkspaceDefinitionStoreInMemoryCache _storeCache;
private readonly IClock _clock;
private readonly IDistributedCache _distributedCache;
private readonly AbpDistributedCacheOptions _cacheOptions;
public DynamicWorkspaceDefinitionStoreCacheInvalidator(
IClock clock,
IDistributedCache distributedCache,
IDynamicWorkspaceDefinitionStoreInMemoryCache storeCache,
IOptions<AbpDistributedCacheOptions> cacheOptions)
{
_storeCache = storeCache;
_clock = clock;
_distributedCache = distributedCache;
_cacheOptions = cacheOptions.Value;
}
public async virtual Task HandleEventAsync(EntityChangedEventData<WorkspaceDefinitionRecord> eventData)
{
await RemoveStampInDistributedCacheAsync();
}
protected async virtual Task RemoveStampInDistributedCacheAsync()
{
using (await _storeCache.SyncSemaphore.LockAsync())
{
var cacheKey = GetCommonStampCacheKey();
await _distributedCache.RemoveAsync(cacheKey);
_storeCache.CacheStamp = Guid.NewGuid().ToString();
_storeCache.LastCheckTime = _clock.Now.AddMinutes(-5);
}
}
protected virtual string GetCommonStampCacheKey()
{
return $"{_cacheOptions.KeyPrefix}_AbpInMemoryWorkspaceCacheStamp";
}
}
Loading…
Cancel
Save