Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add caching support to VendorService (#600)
Krzysztof Pajak34bda1c8b584
diff --git a/src/Business/Grand.Business.Customers/Services/VendorService.cs b/src/Business/Grand.Business.Customers/Services/VendorService.csindex 8dbecf37a..e1a8211c7 100644--- a/src/Business/Grand.Business.Customers/Services/VendorService.cs+++ b/src/Business/Grand.Business.Customers/Services/VendorService.cs@@ -1,7 +1,10 @@using Grand.Business.Core.Interfaces.Customers;using Grand.Data;using Grand.Domain;+using Grand.Domain.Catalog;using Grand.Domain.Vendors;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Caching.Constants;using Grand.Infrastructure.Extensions;using MediatR;@@ -21,10 +24,11 @@ public class VendorService : IVendorService/// <param name="vendorReviewRepository">Vendor review repository</param>/// <param name="mediator">Mediator</param>public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorReview> vendorReviewRepository,- IMediator mediator)+ ICacheBase cacheBase, IMediator mediator){_vendorRepository = vendorRepository;_vendorReviewRepository = vendorReviewRepository;+ _cacheBase = cacheBase;_mediator = mediator;}@@ -35,6 +39,7 @@ public class VendorService : IVendorServiceprivate readonly IRepository<Vendor> _vendorRepository;private readonly IRepository<VendorReview> _vendorReviewRepository;private readonly IMediator _mediator;+ private readonly ICacheBase _cacheBase;#endregion@@ -45,9 +50,10 @@ public class VendorService : IVendorService/// </summary>/// <param name="vendorId">Vendor identifier</param>/// <returns>Vendor</returns>- public virtual Task<Vendor> GetVendorById(string vendorId)+ public virtual async Task<Vendor> GetVendorById(string vendorId){- return _vendorRepository.GetByIdAsync(vendorId);+ var key = string.Format(CacheKey.VENDOR_BY_ID_KEY, vendorId);+ return await _cacheBase.GetAsync(key, () => _vendorRepository.GetByIdAsync(vendorId));}/// <summary>@@ -99,6 +105,9 @@ public class VendorService : IVendorService//event notificationawait _mediator.EntityUpdated(vendor);++ // clear cache+ await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));}/// <summary>@@ -111,6 +120,9 @@ public class VendorService : IVendorServicevendor.Deleted = true;await UpdateVendor(vendor);++ // clear cache+ await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));}@@ -250,6 +262,9 @@ public class VendorService : IVendorService//event notificationawait _mediator.EntityUpdated(vendor);++ // clear cache+ await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));}public virtual async Task UpdateVendorReview(VendorReview vendorReview)diff --git a/src/Core/Grand.Infrastructure/Caching/Constants/VendorCacheKey.cs b/src/Core/Grand.Infrastructure/Caching/Constants/VendorCacheKey.csnew file mode 100644index 000000000..0728299f6--- /dev/null+++ b/src/Core/Grand.Infrastructure/Caching/Constants/VendorCacheKey.cs@@ -0,0 +1,12 @@+namespace Grand.Infrastructure.Caching.Constants;++public static partial class CacheKey+{+ /// <summary>+ /// Key for caching+ /// </summary>+ /// <remarks>+ /// {0} : vendor ID+ /// </remarks>+ public static string VENDOR_BY_ID_KEY => "Grand.vendor.id-{0}";+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.csindex a5e86be5f..a8a927ba9 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs@@ -1,7 +1,10 @@using Grand.Business.Customers.Services;using Grand.Data;using Grand.Domain.Vendors;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Configuration;using Grand.Infrastructure.Events;+using Grand.Infrastructure.Tests.Caching;using MediatR;using Microsoft.VisualStudio.TestTools.UnitTesting;using Moq;@@ -15,14 +18,16 @@ public class VendorServiceTestsprivate Mock<IRepository<Vendor>> _repoMock;private Mock<IRepository<VendorReview>> _vendorReviewRepositoryMock;private VendorService _vendorService;-+ private MemoryCacheBase _cacheBase;[TestInitialize]public void Init(){+ _mediatorMock = new Mock<IMediator>();_repoMock = new Mock<IRepository<Vendor>>();_vendorReviewRepositoryMock = new Mock<IRepository<VendorReview>>();- _mediatorMock = new Mock<IMediator>();- _vendorService = new VendorService(_repoMock.Object, _vendorReviewRepositoryMock.Object, _mediatorMock.Object);+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object,+ new CacheConfig { DefaultCacheTimeMinutes = 1 });+ _vendorService = new VendorService(_repoMock.Object, _vendorReviewRepositoryMock.Object, _cacheBase, _mediatorMock.Object);}[TestMethod]