Renaming CatalogServices to CatalogViewModelService
- I believe this causes some confusion when people see "Service" in the Web project. We have another service in there named BasketViewModelService instead of BasketService anyways. Adding "ViewModel" to the name is a better representation of what the "services" in the Web project represent.
This commit is contained in:
54
src/Web/Services/CachedCatalogViewModelService.cs
Normal file
54
src/Web/Services/CachedCatalogViewModelService.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.eShopWeb.Web.ViewModels;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.Web.Services
|
||||
{
|
||||
public class CachedCatalogViewModelService : ICatalogViewModelService
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly CatalogViewModelService _catalogViewModelService;
|
||||
private static readonly string _brandsKey = "brands";
|
||||
private static readonly string _typesKey = "types";
|
||||
private static readonly string _itemsKeyTemplate = "items-{0}-{1}-{2}-{3}";
|
||||
private static readonly TimeSpan _defaultCacheDuration = TimeSpan.FromSeconds(30);
|
||||
|
||||
public CachedCatalogViewModelService(IMemoryCache cache,
|
||||
CatalogViewModelService catalogViewModelService)
|
||||
{
|
||||
_cache = cache;
|
||||
_catalogViewModelService = catalogViewModelService;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SelectListItem>> GetBrands()
|
||||
{
|
||||
return await _cache.GetOrCreateAsync(_brandsKey, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = _defaultCacheDuration;
|
||||
return await _catalogViewModelService.GetBrands();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId)
|
||||
{
|
||||
string cacheKey = String.Format(_itemsKeyTemplate, pageIndex, itemsPage, brandId, typeId);
|
||||
return await _cache.GetOrCreateAsync(cacheKey, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = _defaultCacheDuration;
|
||||
return await _catalogViewModelService.GetCatalogItems(pageIndex, itemsPage, brandId, typeId);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SelectListItem>> GetTypes()
|
||||
{
|
||||
return await _cache.GetOrCreateAsync(_typesKey, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = _defaultCacheDuration;
|
||||
return await _catalogViewModelService.GetTypes();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user