Browse Source
* make TODO: Make a generic decorator for any LookupData type * remove useless GetById in ICatalogLookupDataServicemain
committed by
GitHub
6 changed files with 55 additions and 123 deletions
@ -1,59 +0,0 @@ |
|||
using Blazored.LocalStorage; |
|||
using BlazorShared.Interfaces; |
|||
using BlazorShared.Models; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace BlazorAdmin.Services |
|||
{ |
|||
public class CachedCatalogBrandServiceDecorator : ICatalogLookupDataService<CatalogBrand> |
|||
{ |
|||
// TODO: Make a generic decorator for any LookupData type
|
|||
private readonly ILocalStorageService _localStorageService; |
|||
private readonly CatalogLookupDataService<CatalogBrand, CatalogBrandResponse> _catalogBrandService; |
|||
private ILogger<CachedCatalogBrandServiceDecorator> _logger; |
|||
|
|||
public CachedCatalogBrandServiceDecorator(ILocalStorageService localStorageService, |
|||
CatalogLookupDataService<CatalogBrand, CatalogBrandResponse> catalogBrandService, |
|||
ILogger<CachedCatalogBrandServiceDecorator> logger) |
|||
{ |
|||
_localStorageService = localStorageService; |
|||
_catalogBrandService = catalogBrandService; |
|||
_logger = logger; |
|||
|
|||
} |
|||
|
|||
public async Task<CatalogBrand> GetById(int id) |
|||
{ |
|||
return (await List()).FirstOrDefault(x => x.Id == id); |
|||
} |
|||
|
|||
public async Task<List<CatalogBrand>> List() |
|||
{ |
|||
string key = "brands"; |
|||
var cacheEntry = await _localStorageService.GetItemAsync<CacheEntry<List<CatalogBrand>>>(key); |
|||
if (cacheEntry != null) |
|||
{ |
|||
_logger.LogInformation("Loading brands from local storage."); |
|||
// TODO: Get Default Cache Duration from Config
|
|||
if (cacheEntry.DateCreated.AddMinutes(1) > DateTime.UtcNow) |
|||
{ |
|||
return cacheEntry.Value; |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogInformation("Cache expired; removing brands from local storage."); |
|||
await _localStorageService.RemoveItemAsync(key); |
|||
} |
|||
} |
|||
|
|||
var brands = await _catalogBrandService.List(); |
|||
var entry = new CacheEntry<List<CatalogBrand>>(brands); |
|||
await _localStorageService.SetItemAsync(key, entry); |
|||
return brands; |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
using Blazored.LocalStorage; |
|||
using BlazorShared.Interfaces; |
|||
using BlazorShared.Models; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace BlazorAdmin.Services |
|||
{ |
|||
public class CachedCatalogTypeServiceDecorator : ICatalogLookupDataService<CatalogType> |
|||
{ |
|||
private readonly ILocalStorageService _localStorageService; |
|||
private readonly CatalogLookupDataService<CatalogType, CatalogTypeResponse> _catalogTypeService; |
|||
private ILogger<CachedCatalogTypeServiceDecorator> _logger; |
|||
|
|||
public CachedCatalogTypeServiceDecorator(ILocalStorageService localStorageService, |
|||
CatalogLookupDataService<CatalogType, CatalogTypeResponse> catalogTypeService, |
|||
ILogger<CachedCatalogTypeServiceDecorator> logger) |
|||
{ |
|||
_localStorageService = localStorageService; |
|||
_catalogTypeService = catalogTypeService; |
|||
_logger = logger; |
|||
} |
|||
|
|||
public async Task<CatalogType> GetById(int id) |
|||
{ |
|||
return (await List()).FirstOrDefault(x => x.Id == id); |
|||
} |
|||
|
|||
public async Task<List<CatalogType>> List() |
|||
{ |
|||
string key = "types"; |
|||
var cacheEntry = await _localStorageService.GetItemAsync<CacheEntry<List<CatalogType>>>(key); |
|||
if (cacheEntry != null) |
|||
{ |
|||
_logger.LogInformation("Loading types from local storage."); |
|||
if (cacheEntry.DateCreated.AddMinutes(1) > DateTime.UtcNow) |
|||
{ |
|||
return cacheEntry.Value; |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogInformation("Cache expired; removing types from local storage."); |
|||
await _localStorageService.RemoveItemAsync(key); |
|||
} |
|||
} |
|||
|
|||
var types = await _catalogTypeService.List(); |
|||
var entry = new CacheEntry<List<CatalogType>>(types); |
|||
await _localStorageService.SetItemAsync(key, entry); |
|||
return types; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue