Skip to content

Commit

Permalink
Merge pull request #48 from EasyAbp/on-completed
Browse files Browse the repository at this point in the history
Use `IServiceScopeFactory` on UOW completed
  • Loading branch information
gdlcf88 authored Aug 3, 2023
2 parents 580dd0d + 8d365a8 commit 43b1f15
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
Expand All @@ -12,17 +13,20 @@ public class AssetScheduleCacheInvalidator : ILocalEventHandler<EntityChangedEve
ITransientDependency
{
protected IDistributedCache<AssetScheduleCacheItem> DistributedCache { get; }

protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceScopeFactory ServiceScopeFactory { get; }

public AssetScheduleCacheInvalidator(IDistributedCache<AssetScheduleCacheItem> distributedCache,
public AssetScheduleCacheInvalidator(
IDistributedCache<AssetScheduleCacheItem> distributedCache,
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager)
IUnitOfWorkManager unitOfWorkManager,
IServiceScopeFactory serviceScopeFactory)
{
DistributedCache = distributedCache;
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
ServiceScopeFactory = serviceScopeFactory;
}

public virtual async Task HandleEventAsync(EntityChangedEventData<AssetSchedule> eventData)
Expand All @@ -31,7 +35,14 @@ public virtual async Task HandleEventAsync(EntityChangedEventData<AssetSchedule>
eventData.Entity.AssetId,
eventData.Entity.PeriodSchemeId,
CurrentTenant.Id);

await DistributedCache.RemoveAsync(key);
UnitOfWorkManager.Current?.OnCompleted(() => DistributedCache.RemoveAsync(key));

UnitOfWorkManager.Current?.OnCompleted(async () =>
{
using var scope = ServiceScopeFactory.CreateScope();
var cache = scope.ServiceProvider.GetRequiredService<IDistributedCache<AssetScheduleCacheItem>>();
await cache.RemoveAsync(key);
});
}
}

0 comments on commit 43b1f15

Please sign in to comment.