Skip to content

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.

Added caching to the GetCustomerBySystemName method in CustomerService

support3817945121c2
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerService.cs
index f120d9c2b..4b2965bd7 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerService.cs
@@ -11,6 +11,8 @@ using Grand.Infrastructure.Extensions;
using Grand.SharedKernel;
using MediatR;
using System.Linq.Expressions;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Caching.Constants;
namespace Grand.Business.Customers.Services
{
@@ -24,6 +26,7 @@ namespace Grand.Business.Customers.Services
private readonly IRepository<Customer> _customerRepository;
private readonly IUserFieldService _userFieldService;
private readonly IMediator _mediator;
+ private readonly ICacheBase _cacheBase;
#endregion
@@ -32,11 +35,13 @@ namespace Grand.Business.Customers.Services
public CustomerService(
IRepository<Customer> customerRepository,
IUserFieldService userFieldService,
- IMediator mediator)
+ IMediator mediator,
+ ICacheBase cacheBase)
{
_customerRepository = customerRepository;
_userFieldService = userFieldService;
_mediator = mediator;
+ _cacheBase = cacheBase;
}
#endregion
@@ -215,12 +220,14 @@ namespace Grand.Business.Customers.Services
/// </summary>
/// <param name="systemName">System name</param>
/// <returns>Customer</returns>
- public virtual async Task<Customer> GetCustomerBySystemName(string systemName)
+ public virtual Task<Customer> GetCustomerBySystemName(string systemName)
{
if (string.IsNullOrWhiteSpace(systemName))
return null;
- return await _customerRepository.GetOneAsync(x => x.SystemName == systemName);
+ var key = string.Format(CacheKey.CUSTOMER_BY_SYSTEMNAME_BY_KEY, systemName);
+
+ return _cacheBase.GetAsync(key, () => _customerRepository.GetOneAsync(x => x.SystemName == systemName));
}
/// <summary>
diff --git a/src/Core/Grand.Infrastructure/Caching/Constants/CustomerCacheKey.cs b/src/Core/Grand.Infrastructure/Caching/Constants/CustomerCacheKey.cs
index 53ed2c603..a74277a43 100644
--- a/src/Core/Grand.Infrastructure/Caching/Constants/CustomerCacheKey.cs
+++ b/src/Core/Grand.Infrastructure/Caching/Constants/CustomerCacheKey.cs
@@ -63,6 +63,14 @@
/// </remarks>
public static string CUSTOMERGROUPSPRODUCTS_ROLE_KEY => "Grand.customergroupproducts.role-{0}";
+ /// <summary>
+ /// Key for caching
+ /// </summary>
+ /// <remarks>
+ /// {0} : ident
+ /// </remarks>
+ public static string CUSTOMER_BY_SYSTEMNAME_BY_KEY => "Grand.customer.systemname-{0}";
+
#region Customer activity
/// <summary>
diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs
index c082843b1..c0cf7fcfa 100644
--- a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs
+++ b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs
@@ -10,6 +10,7 @@ using Grand.Domain.Orders;
using MediatR;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
+using Grand.Infrastructure.Caching;
namespace Grand.Business.Customers.Tests.Services
{
@@ -19,6 +20,7 @@ namespace Grand.Business.Customers.Tests.Services
private IRepository<Customer> _repository;
private Mock<IMediator> _mediatorMock;
private IUserFieldService _userFieldService;
+ private MemoryCacheBase _cacheBase;
private CustomerService _customerService;
[TestInitialize()]
public void TestInitialize()
@@ -27,7 +29,7 @@ namespace Grand.Business.Customers.Tests.Services
_mediatorMock = new Mock<IMediator>();
_userFieldService = new UserFieldService(new MongoDBRepositoryTest<UserFieldBaseEntity>());
- _customerService = new CustomerService(_repository, _userFieldService, _mediatorMock.Object);
+ _customerService = new CustomerService(_repository, _userFieldService, _mediatorMock.Object, _cacheBase);
}