diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs index 9c463059..15df94f0 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs @@ -8,6 +8,7 @@ using EasyAbp.EShop.Products.Options; using EasyAbp.EShop.Products.ProductInventories; using EasyAbp.EShop.Products.Products.CacheItems; using EasyAbp.EShop.Stores.Stores; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Application.Dtos; using Volo.Abp.Caching; @@ -28,8 +29,8 @@ namespace EasyAbp.EShop.Products.Products protected override string CrossStorePolicyName { get; set; } = ProductsPermissions.Products.CrossStore; private readonly IProductManager _productManager; - private readonly IDistributedCache _cache; private readonly EShopProductsOptions _options; + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IProductInventoryProviderResolver _productInventoryProviderResolver; private readonly IProductViewCacheKeyProvider _productViewCacheKeyProvider; private readonly IProductRepository _repository; @@ -37,14 +38,14 @@ namespace EasyAbp.EShop.Products.Products public ProductAppService( IProductManager productManager, IOptions options, - IDistributedCache cache, + IServiceScopeFactory serviceScopeFactory, IProductInventoryProviderResolver productInventoryProviderResolver, IProductViewCacheKeyProvider productViewCacheKeyProvider, IProductRepository repository) : base(repository) { _productManager = productManager; - _cache = cache; _options = options.Value; + _serviceScopeFactory = serviceScopeFactory; _productInventoryProviderResolver = productInventoryProviderResolver; _productViewCacheKeyProvider = productViewCacheKeyProvider; _repository = repository; @@ -87,14 +88,19 @@ namespace EasyAbp.EShop.Products.Products await LoadDtosExtraDataAsync(items, Clock.Now); await LoadDtosProductGroupDisplayNameAsync(items); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); return dto; } - protected virtual async Task ClearProductViewCacheAsync(Guid storeId) + protected virtual async Task ClearProductViewCacheOnCurrentUowCompletedAsync(Guid storeId) { - await _cache.RemoveAsync(await _productViewCacheKeyProvider.GetCacheKeyAsync(storeId)); + UnitOfWorkManager.Current.OnCompleted(async () => + { + using var scope = _serviceScopeFactory.CreateScope(); + var cache = scope.ServiceProvider.GetRequiredService>(); + await cache.RemoveAsync(await _productViewCacheKeyProvider.GetCacheKeyAsync(storeId)); + }); } public override async Task UpdateAsync(Guid id, CreateUpdateProductDto input) @@ -123,7 +129,7 @@ namespace EasyAbp.EShop.Products.Products await LoadDtosExtraDataAsync(items, Clock.Now); await LoadDtosProductGroupDisplayNameAsync(items); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); return dto; } @@ -351,7 +357,7 @@ namespace EasyAbp.EShop.Products.Products await _productManager.DeleteAsync(product); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); } protected virtual void CheckProductIsNotStatic(Product product) @@ -383,7 +389,7 @@ namespace EasyAbp.EShop.Products.Products await LoadDtosExtraDataAsync(items, Clock.Now); await LoadDtosProductGroupDisplayNameAsync(items); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); return dto; } @@ -410,7 +416,7 @@ namespace EasyAbp.EShop.Products.Products await LoadDtosExtraDataAsync(items, Clock.Now); await LoadDtosProductGroupDisplayNameAsync(items); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); return dto; } @@ -434,7 +440,7 @@ namespace EasyAbp.EShop.Products.Products await LoadDtosExtraDataAsync(items, Clock.Now); await LoadDtosProductGroupDisplayNameAsync(items); - UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); }); + await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId); return dto; } diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs index a8741b05..2f209472 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs +++ b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs @@ -2,8 +2,6 @@ using EasyAbp.EShop.Orders.Orders; using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Volo.Abp; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus.Distributed; @@ -13,27 +11,27 @@ namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans; public class FlashSaleOrderCanceledEventHandler : IDistributedEventHandler, ITransientDependency { + protected IServiceScopeFactory ServiceScopeFactory { get; } protected IFlashSaleResultRepository FlashSaleResultRepository { get; } - protected IUnitOfWorkManager UnitOfWorkManager { get; } - protected IAbpApplication AbpApplication { get; } - public FlashSaleOrderCanceledEventHandler( + IServiceScopeFactory serviceScopeFactory, IFlashSaleResultRepository flashSaleResultRepository, - IUnitOfWorkManager unitOfWorkManager, - IAbpApplication abpApplication) + IUnitOfWorkManager unitOfWorkManager) { + ServiceScopeFactory = serviceScopeFactory; FlashSaleResultRepository = flashSaleResultRepository; UnitOfWorkManager = unitOfWorkManager; - AbpApplication = abpApplication; } [UnitOfWork(true)] public virtual async Task HandleEventAsync(OrderCanceledEto eventData) { var flashSaleResult = await FlashSaleResultRepository - .SingleOrDefaultAsync(x => x.Status != FlashSaleResultStatus.Failed && x.StoreId == eventData.Order.StoreId && x.OrderId == eventData.Order.Id); + .SingleOrDefaultAsync(x => + x.Status != FlashSaleResultStatus.Failed && x.StoreId == eventData.Order.StoreId && + x.OrderId == eventData.Order.Id); if (flashSaleResult == null) { return; @@ -45,10 +43,10 @@ public class FlashSaleOrderCanceledEventHandler : IDistributedEventHandler { - using var scope = AbpApplication.ServiceProvider.CreateScope(); + using var scope = ServiceScopeFactory.CreateScope(); var flashSaleCurrentResultCache = scope.ServiceProvider.GetRequiredService(); // remove the cache so the user can try to order again. await flashSaleCurrentResultCache.RemoveAsync(flashSaleResult.PlanId, flashSaleResult.UserId); }); } -} +} \ No newline at end of file diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs index 59c07c13..36fdb969 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs +++ b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs @@ -4,7 +4,6 @@ using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos; using EasyAbp.EShop.Products.Products; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Volo.Abp; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; using Volo.Abp.ObjectMapping; @@ -17,18 +16,18 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler { protected ILogger Logger { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } - protected IAbpApplication AbpApplication { get; } + protected IServiceScopeFactory ServiceScopeFactory { get; } protected IFlashSaleResultRepository FlashSaleResultRepository { get; } public FlashSaleOrderCreationResultEventHandler( ILogger logger, IUnitOfWorkManager unitOfWorkManager, - IAbpApplication abpApplication, + IServiceScopeFactory serviceScopeFactory, IFlashSaleResultRepository flashSaleResultRepository) { Logger = logger; UnitOfWorkManager = unitOfWorkManager; - AbpApplication = abpApplication; + ServiceScopeFactory = serviceScopeFactory; FlashSaleResultRepository = flashSaleResultRepository; } @@ -52,7 +51,7 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler UnitOfWorkManager.Current.OnCompleted(async () => { - using var scope = AbpApplication.ServiceProvider.CreateScope(); + using var scope = ServiceScopeFactory.CreateScope(); var flashSaleInventoryManager = scope.ServiceProvider.GetRequiredService(); var flashSaleCurrentResultCache = scope.ServiceProvider.GetRequiredService(); diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs index d8320613..33d0f3fc 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs +++ b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities.Events; @@ -8,15 +9,19 @@ using Volo.Abp.Uow; namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans; -public class FlashSalePlanCacheInvalidator : ILocalEventHandler>, ITransientDependency +public class FlashSalePlanCacheInvalidator : ILocalEventHandler>, + ITransientDependency { + protected IServiceScopeFactory ServiceScopeFactory { get; } protected IDistributedCache DistributedCache { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } public FlashSalePlanCacheInvalidator( + IServiceScopeFactory serviceScopeFactory, IDistributedCache distributedCache, IUnitOfWorkManager unitOfWorkManager) { + ServiceScopeFactory = serviceScopeFactory; DistributedCache = distributedCache; UnitOfWorkManager = unitOfWorkManager; } @@ -27,7 +32,12 @@ public class FlashSalePlanCacheInvalidator : ILocalEventHandler { - await DistributedCache.RemoveAsync(eventData.Entity.Id); + using var scope = ServiceScopeFactory.CreateScope(); + + var distributedCache = + scope.ServiceProvider.GetRequiredService>(); + + await distributedCache.RemoveAsync(eventData.Entity.Id); }); } -} +} \ No newline at end of file diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs index 27524548..6f4ac57b 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs +++ b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs @@ -22,10 +22,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler Logger { get; } protected IAbpDistributedLock AbpDistributedLock { get; } protected IDistributedEventBus DistributedEventBus { get; } - protected IAbpApplication AbpApplication { get; } protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; } protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; } protected IFlashSaleResultRepository FlashSaleResultRepository { get; } @@ -34,10 +34,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler logger, IAbpDistributedLock abpDistributedLock, IDistributedEventBus distributedEventBus, - IAbpApplication abpApplication, IFlashSaleInventoryManager flashSaleInventoryManager, IFlashSaleCurrentResultCache flashSaleCurrentResultCache, IFlashSaleResultRepository flashSaleResultRepository) @@ -45,10 +45,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler { - using var scope = AbpApplication.ServiceProvider.CreateScope(); + using var scope = ServiceScopeFactory.CreateScope(); var flashSaleInventoryManager = scope.ServiceProvider.GetRequiredService(); diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs index 7c9d368c..43235400 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs +++ b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.EventBus.Distributed; @@ -13,13 +14,16 @@ public class ProductCacheInvalidator : { protected IProductCache ProductCache { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } + protected IServiceScopeFactory ServiceScopeFactory { get; } public ProductCacheInvalidator( IProductCache productCache, - IUnitOfWorkManager unitOfWorkManager) + IUnitOfWorkManager unitOfWorkManager, + IServiceScopeFactory serviceScopeFactory) { ProductCache = productCache; UnitOfWorkManager = unitOfWorkManager; + ServiceScopeFactory = serviceScopeFactory; } public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) @@ -28,7 +32,9 @@ public class ProductCacheInvalidator : UnitOfWorkManager.Current?.OnCompleted(async () => { - await ProductCache.RemoveAsync(eventData.Entity.Id); + using var scope = ServiceScopeFactory.CreateScope(); + var productCache = scope.ServiceProvider.GetRequiredService(); + await productCache.RemoveAsync(eventData.Entity.Id); }); } @@ -38,7 +44,9 @@ public class ProductCacheInvalidator : UnitOfWorkManager.Current?.OnCompleted(async () => { - await ProductCache.RemoveAsync(eventData.Entity.Id); + using var scope = ServiceScopeFactory.CreateScope(); + var productCache = scope.ServiceProvider.GetRequiredService(); + await productCache.RemoveAsync(eventData.Entity.Id); }); } -} +} \ No newline at end of file