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.
Feature/generic cached decorator (dotnet-architecture#604)
* make TODO: Make a generic decorator for any LookupData type * remove useless GetById in ICatalogLookupDataService
- Loading branch information
1 parent
ed63faa
commit 68beb21
Showing
6 changed files
with
55 additions
and
123 deletions.
There are no files selected for viewing
59 changes: 0 additions & 59 deletions
59
src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/BlazorAdmin/Services/CachedCatalogLookupDataServiceDecorator .cs
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,53 @@ | ||
using Blazored.LocalStorage; | ||
using BlazorShared.Interfaces; | ||
using BlazorShared.Models; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace BlazorAdmin.Services | ||
{ | ||
public class CachedCatalogLookupDataServiceDecorator<TLookupData, TReponse> | ||
: ICatalogLookupDataService<TLookupData> | ||
where TLookupData : LookupData | ||
where TReponse : ILookupDataResponse<TLookupData> | ||
{ | ||
private readonly ILocalStorageService _localStorageService; | ||
private readonly CatalogLookupDataService<TLookupData, TReponse> _catalogTypeService; | ||
private ILogger<CachedCatalogLookupDataServiceDecorator<TLookupData, TReponse>> _logger; | ||
|
||
public CachedCatalogLookupDataServiceDecorator(ILocalStorageService localStorageService, | ||
CatalogLookupDataService<TLookupData, TReponse> catalogTypeService, | ||
ILogger<CachedCatalogLookupDataServiceDecorator<TLookupData, TReponse>> logger) | ||
{ | ||
_localStorageService = localStorageService; | ||
_catalogTypeService = catalogTypeService; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<List<TLookupData>> List() | ||
{ | ||
string key = typeof(TLookupData).Name; | ||
var cacheEntry = await _localStorageService.GetItemAsync<CacheEntry<List<TLookupData>>>(key); | ||
if (cacheEntry != null) | ||
{ | ||
_logger.LogInformation($"Loading {key} from local storage."); | ||
if (cacheEntry.DateCreated.AddMinutes(1) > DateTime.UtcNow) | ||
{ | ||
return cacheEntry.Value; | ||
} | ||
else | ||
{ | ||
_logger.LogInformation($"Cache expired; removing {key} from local storage."); | ||
await _localStorageService.RemoveItemAsync(key); | ||
} | ||
} | ||
|
||
var types = await _catalogTypeService.List(); | ||
var entry = new CacheEntry<List<TLookupData>>(types); | ||
await _localStorageService.SetItemAsync(key, entry); | ||
return types; | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs
This file was deleted.
Oops, something went wrong.
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