Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
add memory cachine
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Nov 19, 2023
1 parent 55399c6 commit 316ddaa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
51 changes: 34 additions & 17 deletions Web2Gateway/G2To3Service.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.Extensions.Caching.Memory;

namespace Web2Gateway;


Expand All @@ -10,11 +12,12 @@ public record WellKnown()
public sealed class G2To3Service
{
private readonly ChiaService _chiaService;
private readonly IMemoryCache _memoryCache;
private readonly ILogger<G2To3Service> _logger;
private readonly IConfiguration _configuration;

public G2To3Service(ChiaService chiaService, ILogger<G2To3Service> logger, IConfiguration configuration) =>
(_chiaService, _logger, _configuration) = (chiaService, logger, configuration);
public G2To3Service(ChiaService chiaService, IMemoryCache memoryCache, ILogger<G2To3Service> logger, IConfiguration configuration) =>
(_chiaService, _memoryCache, _logger, _configuration) = (chiaService, memoryCache, logger, configuration);

public WellKnown GetWellKnown()
{
Expand All @@ -31,14 +34,42 @@ 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
{
return null; // 404 in the api
}
}

public async Task<string?> 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<string> GetValueAsHtml(string storeId, CancellationToken cancellationToken)
{
var hexKey = HexUtils.ToHex("index.html");
Expand Down Expand Up @@ -70,18 +101,4 @@ public async Task<byte[]> GetValuesAsBytes(string storeId, dynamic json, Cancell

return Convert.FromHexString(resultHex);
}

public async Task<string?> 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
}
}
}
4 changes: 3 additions & 1 deletion Web2Gateway/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
.AddConsole();

builder.Services.AddControllers();

builder.Services.AddSingleton<ChiaService>()
.AddSingleton<G2To3Service>()
.AddEndpointsApiExplorer()
.AddSwaggerGen();
.AddSwaggerGen()
.AddMemoryCache();

var logger = LoggerFactory.Create(config =>
{
Expand Down

0 comments on commit 316ddaa

Please sign in to comment.