Files
eShopOnWeb/src/Web/Services/CachedCatalogViewModelService.cs
Viswanatha Swamy 1598d0bbe1 Swamy/remove unused usings and reorganize usings (#489)
* Removed and Reordered the using statements

* Removed and Reordered the usings inside Web Project

* Removed and Reordered the usings inside PublicApi project

* Removed Unused usings and reorganized usings inside Infrastructure project
2020-12-03 08:05:28 -05:00

52 lines
2.0 KiB
C#

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.eShopWeb.Web.Extensions;
using Microsoft.eShopWeb.Web.ViewModels;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Web.Services
{
public class CachedCatalogViewModelService : ICatalogViewModelService
{
private readonly IMemoryCache _cache;
private readonly CatalogViewModelService _catalogViewModelService;
public CachedCatalogViewModelService(IMemoryCache cache,
CatalogViewModelService catalogViewModelService)
{
_cache = cache;
_catalogViewModelService = catalogViewModelService;
}
public async Task<IEnumerable<SelectListItem>> GetBrands()
{
return await _cache.GetOrCreateAsync(CacheHelpers.GenerateBrandsCacheKey(), async entry =>
{
entry.SlidingExpiration = CacheHelpers.DefaultCacheDuration;
return await _catalogViewModelService.GetBrands();
});
}
public async Task<CatalogIndexViewModel> GetCatalogItems(int pageIndex, int itemsPage, int? brandId, int? typeId)
{
var cacheKey = CacheHelpers.GenerateCatalogItemCacheKey(pageIndex, Constants.ITEMS_PER_PAGE, brandId, typeId);
return await _cache.GetOrCreateAsync(cacheKey, async entry =>
{
entry.SlidingExpiration = CacheHelpers.DefaultCacheDuration;
return await _catalogViewModelService.GetCatalogItems(pageIndex, itemsPage, brandId, typeId);
});
}
public async Task<IEnumerable<SelectListItem>> GetTypes()
{
return await _cache.GetOrCreateAsync(CacheHelpers.GenerateTypesCacheKey(), async entry =>
{
entry.SlidingExpiration = CacheHelpers.DefaultCacheDuration;
return await _catalogViewModelService.GetTypes();
});
}
}
}