forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated data chapter with EF, caching, NoSQL
Added caching support and some diagnostic code.
- Loading branch information
Showing
8 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
Binary file modified
BIN
+254 KB
(110%)
docs/Architecting and Developing Modern Web Applications with ASP.NET Core and Azure.pdf
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.eShopWeb.ViewModels; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
|
||
namespace Microsoft.eShopWeb.Services | ||
{ | ||
public class CachedCatalogService : ICatalogService | ||
{ | ||
private readonly IMemoryCache _cache; | ||
private readonly CatalogService _catalogService; | ||
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 CachedCatalogService(IMemoryCache cache, | ||
CatalogService catalogService) | ||
{ | ||
_cache = cache; | ||
_catalogService = catalogService; | ||
} | ||
|
||
public async Task<IEnumerable<SelectListItem>> GetBrands() | ||
{ | ||
return await _cache.GetOrCreateAsync(_brandsKey, async entry => | ||
{ | ||
entry.SlidingExpiration = _defaultCacheDuration; | ||
return await _catalogService.GetBrands(); | ||
}); | ||
} | ||
|
||
public async Task<Catalog> 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 _catalogService.GetCatalogItems(pageIndex, itemsPage, brandID, typeId); | ||
}); | ||
} | ||
|
||
public async Task<IEnumerable<SelectListItem>> GetTypes() | ||
{ | ||
return await _cache.GetOrCreateAsync(_typesKey, async entry => | ||
{ | ||
entry.SlidingExpiration = _defaultCacheDuration; | ||
return await _catalogService.GetTypes(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters