diff --git a/Web2Gateway/G2To3Service.cs b/Web2Gateway/G2To3Service.cs index 0d05efb..8cf1211 100644 --- a/Web2Gateway/G2To3Service.cs +++ b/Web2Gateway/G2To3Service.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Caching.Memory; + namespace Web2Gateway; @@ -10,11 +12,12 @@ public record WellKnown() public sealed class G2To3Service { private readonly ChiaService _chiaService; + private readonly IMemoryCache _memoryCache; private readonly ILogger _logger; private readonly IConfiguration _configuration; - public G2To3Service(ChiaService chiaService, ILogger logger, IConfiguration configuration) => - (_chiaService, _logger, _configuration) = (chiaService, logger, configuration); + public G2To3Service(ChiaService chiaService, IMemoryCache memoryCache, ILogger logger, IConfiguration configuration) => + (_chiaService, _memoryCache, _logger, _configuration) = (chiaService, memoryCache, logger, configuration); public WellKnown GetWellKnown() { @@ -31,7 +34,14 @@ public WellKnown GetWellKnown() try { - return await dataLayer.GetKeys(storeId, null, cancellationToken); + var keys = await _memoryCache.GetOrCreateAsync($"{storeId}", async entry => + { + entry.SlidingExpiration = TimeSpan.FromMinutes(15); + _logger.LogInformation("Getting keys for {StoreId}", storeId); + return await dataLayer.GetKeys(storeId, null, cancellationToken); + }); + + return keys; } catch { @@ -39,6 +49,27 @@ public WellKnown GetWellKnown() } } + public async Task GetValue(string storeId, string key, CancellationToken cancellationToken) + { + var dataLayer = _chiaService.GetDataLayer(cancellationToken) ?? throw new Exception("DataLayer not available"); + + try + { + var value = await _memoryCache.GetOrCreateAsync($"{storeId}-{key}", async entry => + { + entry.SlidingExpiration = TimeSpan.FromMinutes(15); + _logger.LogInformation("Getting value for {StoreId} {Key}", storeId, key); + return await dataLayer.GetValue(storeId, key, null, cancellationToken); + }); + + return value; + } + catch + { + return null; // 404 in the api + } + } + public async Task GetValueAsHtml(string storeId, CancellationToken cancellationToken) { var hexKey = HexUtils.ToHex("index.html"); @@ -70,18 +101,4 @@ public async Task GetValuesAsBytes(string storeId, dynamic json, Cancell return Convert.FromHexString(resultHex); } - - public async Task GetValue(string storeId, string key, CancellationToken cancellationToken) - { - var dataLayer = _chiaService.GetDataLayer(cancellationToken) ?? throw new Exception("DataLayer not available"); - - try - { - return await dataLayer.GetValue(storeId, key, null, cancellationToken); - } - catch - { - return null; // 404 in the api - } - } } diff --git a/Web2Gateway/Program.cs b/Web2Gateway/Program.cs index c48ae52..2776551 100644 --- a/Web2Gateway/Program.cs +++ b/Web2Gateway/Program.cs @@ -27,10 +27,12 @@ .AddConsole(); builder.Services.AddControllers(); + builder.Services.AddSingleton() .AddSingleton() .AddEndpointsApiExplorer() - .AddSwaggerGen(); + .AddSwaggerGen() + .AddMemoryCache(); var logger = LoggerFactory.Create(config => {