Skip to content

Commit 74ad7d6

Browse files
KrzysztofPajakszjanikowski
authored andcommitted
Add caching support to VendorService (grandnode#600)
1 parent bc69b1e commit 74ad7d6

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/Business/Grand.Business.Customers/Services/VendorService.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using Grand.Business.Core.Interfaces.Customers;
22
using Grand.Data;
33
using Grand.Domain;
4+
using Grand.Domain.Catalog;
45
using Grand.Domain.Vendors;
6+
using Grand.Infrastructure.Caching;
7+
using Grand.Infrastructure.Caching.Constants;
58
using Grand.Infrastructure.Extensions;
69
using MediatR;
710

@@ -21,10 +24,11 @@ public class VendorService : IVendorService
2124
/// <param name="vendorReviewRepository">Vendor review repository</param>
2225
/// <param name="mediator">Mediator</param>
2326
public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorReview> vendorReviewRepository,
24-
IMediator mediator)
27+
ICacheBase cacheBase, IMediator mediator)
2528
{
2629
_vendorRepository = vendorRepository;
2730
_vendorReviewRepository = vendorReviewRepository;
31+
_cacheBase = cacheBase;
2832
_mediator = mediator;
2933
}
3034

@@ -35,6 +39,7 @@ public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorRev
3539
private readonly IRepository<Vendor> _vendorRepository;
3640
private readonly IRepository<VendorReview> _vendorReviewRepository;
3741
private readonly IMediator _mediator;
42+
private readonly ICacheBase _cacheBase;
3843

3944
#endregion
4045

@@ -45,9 +50,10 @@ public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorRev
4550
/// </summary>
4651
/// <param name="vendorId">Vendor identifier</param>
4752
/// <returns>Vendor</returns>
48-
public virtual Task<Vendor> GetVendorById(string vendorId)
53+
public virtual async Task<Vendor> GetVendorById(string vendorId)
4954
{
50-
return _vendorRepository.GetByIdAsync(vendorId);
55+
var key = string.Format(CacheKey.VENDOR_BY_ID_KEY, vendorId);
56+
return await _cacheBase.GetAsync(key, () => _vendorRepository.GetByIdAsync(vendorId));
5157
}
5258

5359
/// <summary>
@@ -99,6 +105,9 @@ public virtual async Task UpdateVendor(Vendor vendor)
99105

100106
//event notification
101107
await _mediator.EntityUpdated(vendor);
108+
109+
// clear cache
110+
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
102111
}
103112

104113
/// <summary>
@@ -111,6 +120,9 @@ public virtual async Task DeleteVendor(Vendor vendor)
111120

112121
vendor.Deleted = true;
113122
await UpdateVendor(vendor);
123+
124+
// clear cache
125+
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
114126
}
115127

116128

@@ -250,6 +262,9 @@ public virtual async Task UpdateVendorReviewTotals(Vendor vendor)
250262

251263
//event notification
252264
await _mediator.EntityUpdated(vendor);
265+
266+
// clear cache
267+
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
253268
}
254269

255270
public virtual async Task UpdateVendorReview(VendorReview vendorReview)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Grand.Infrastructure.Caching.Constants;
2+
3+
public static partial class CacheKey
4+
{
5+
/// <summary>
6+
/// Key for caching
7+
/// </summary>
8+
/// <remarks>
9+
/// {0} : vendor ID
10+
/// </remarks>
11+
public static string VENDOR_BY_ID_KEY => "Grand.vendor.id-{0}";
12+
}

src/Tests/Grand.Business.Customers.Tests/Services/VendorServiceTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using Grand.Business.Customers.Services;
22
using Grand.Data;
33
using Grand.Domain.Vendors;
4+
using Grand.Infrastructure.Caching;
5+
using Grand.Infrastructure.Configuration;
46
using Grand.Infrastructure.Events;
7+
using Grand.Infrastructure.Tests.Caching;
58
using MediatR;
69
using Microsoft.VisualStudio.TestTools.UnitTesting;
710
using Moq;
@@ -15,14 +18,16 @@ public class VendorServiceTests
1518
private Mock<IRepository<Vendor>> _repoMock;
1619
private Mock<IRepository<VendorReview>> _vendorReviewRepositoryMock;
1720
private VendorService _vendorService;
18-
21+
private MemoryCacheBase _cacheBase;
1922
[TestInitialize]
2023
public void Init()
2124
{
25+
_mediatorMock = new Mock<IMediator>();
2226
_repoMock = new Mock<IRepository<Vendor>>();
2327
_vendorReviewRepositoryMock = new Mock<IRepository<VendorReview>>();
24-
_mediatorMock = new Mock<IMediator>();
25-
_vendorService = new VendorService(_repoMock.Object, _vendorReviewRepositoryMock.Object, _mediatorMock.Object);
28+
_cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object,
29+
new CacheConfig { DefaultCacheTimeMinutes = 1 });
30+
_vendorService = new VendorService(_repoMock.Object, _vendorReviewRepositoryMock.Object, _cacheBase, _mediatorMock.Object);
2631
}
2732

2833
[TestMethod]

0 commit comments

Comments
 (0)