Skip to content

Commit

Permalink
Use IServiceScopeFactory on UOW completed
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Aug 3, 2023
1 parent 580dd0d commit 8d365a8
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 8d365a8

Please sign in to comment.