Skip to content

Commit

Permalink
Feature/generic cached decorator (dotnet-architecture#604)
Browse files Browse the repository at this point in the history
* make TODO: Make a generic decorator for any LookupData type

* remove useless GetById in ICatalogLookupDataService
  • Loading branch information
michelcedric authored Oct 31, 2021
1 parent ed63faa commit 68beb21
Showing 6 changed files with 55 additions and 123 deletions.
59 changes: 0 additions & 59 deletions src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs

This file was deleted.

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 src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/BlazorAdmin/Services/CatalogLookupDataService.cs
Original file line number Diff line number Diff line change
@@ -31,11 +31,6 @@ public CatalogLookupDataService(HttpClient httpClient,
_apiUrl = baseUrlConfiguration.ApiBase;
}

public async Task<TLookupData> GetById(int id)
{
return (await List()).FirstOrDefault(x => x.Id == id);
}

public async Task<List<TLookupData>> List()
{
var endpointName = typeof(TLookupData).GetCustomAttribute<EndpointAttribute>().Name;
4 changes: 2 additions & 2 deletions src/BlazorAdmin/ServicesConfiguration.cs
Original file line number Diff line number Diff line change
@@ -9,9 +9,9 @@ public static class ServicesConfiguration
{
public static IServiceCollection AddBlazorServices(this IServiceCollection services)
{
services.AddScoped<ICatalogLookupDataService<CatalogBrand>, CachedCatalogBrandServiceDecorator>();
services.AddScoped<ICatalogLookupDataService<CatalogBrand>, CachedCatalogLookupDataServiceDecorator<CatalogBrand,CatalogBrandResponse>>();
services.AddScoped<CatalogLookupDataService<CatalogBrand, CatalogBrandResponse>>();
services.AddScoped<ICatalogLookupDataService<CatalogType>, CachedCatalogTypeServiceDecorator>();
services.AddScoped<ICatalogLookupDataService<CatalogType>, CachedCatalogLookupDataServiceDecorator<CatalogType,CatalogTypeResponse>>();
services.AddScoped<CatalogLookupDataService<CatalogType, CatalogTypeResponse>>();
services.AddScoped<ICatalogItemService, CachedCatalogItemServiceDecorator>();
services.AddScoped<CatalogItemService>();
1 change: 0 additions & 1 deletion src/BlazorShared/Interfaces/ICatalogLookupDataService.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,5 @@ namespace BlazorShared.Interfaces
public interface ICatalogLookupDataService<TLookupData> where TLookupData : LookupData
{
Task<List<TLookupData>> List();
Task<TLookupData> GetById(int id);
}
}

0 comments on commit 68beb21

Please sign in to comment.