Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Business/Grand.Business.Customers/Services/VendorService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

Expand All @@ -35,6 +39,7 @@ public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorRev
private readonly IRepository<Vendor> _vendorRepository;
private readonly IRepository<VendorReview> _vendorReviewRepository;
private readonly IMediator _mediator;
private readonly ICacheBase _cacheBase;

#endregion

Expand All @@ -45,9 +50,10 @@ public VendorService(IRepository<Vendor> vendorRepository, IRepository<VendorRev
/// </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>
Expand Down Expand Up @@ -99,6 +105,9 @@ public virtual async Task UpdateVendor(Vendor vendor)

//event notification
await _mediator.EntityUpdated(vendor);

// clear cache
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
}

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

vendor.Deleted = true;
await UpdateVendor(vendor);

// clear cache
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
}


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

//event notification
await _mediator.EntityUpdated(vendor);

// clear cache
await _cacheBase.RemoveAsync(string.Format(CacheKey.VENDOR_BY_ID_KEY, vendor.Id));
}

public virtual async Task UpdateVendorReview(VendorReview vendorReview)
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Grand.Infrastructure/Caching/Constants/VendorCacheKey.cs
Original file line number Diff line number Diff line change
@@ -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}";
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,14 +18,16 @@ public class VendorServiceTests
private 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]
Expand Down