Bring the caching services over
This commit is contained in:
parent
43b34143a3
commit
e8152186c1
|
|
@ -1,19 +1,25 @@
|
|||
using AS1024.GeoFeed.Core.CacheService;
|
||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AS1024.GeoFeed.MinimalAPI
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateSlimBuilder(args);
|
||||
builder.Services.AddTransient<IGeoFeedProvider, NetboxAoTGeoFeedProvider>();
|
||||
builder.Services.AddHostedService<GeoFeedCacheService>();
|
||||
builder.Services.AddTransient<IGeoFeedPersistentCacheProvider, GeoFeedLocalFileCache>();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddLogging();
|
||||
builder.Services.AddHttpClient();
|
||||
|
|
@ -21,14 +27,33 @@ namespace AS1024.GeoFeed.MinimalAPI
|
|||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||
});
|
||||
var app = builder.Build();
|
||||
var geoFeed = app.Map("/geofeed.csv", async (IGeoFeedProvider provider, ILogger<Program> logger) => {
|
||||
var geoFeed = app.Map("/geofeed.csv", async (IGeoFeedProvider provider,
|
||||
ILogger<Program> logger,
|
||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment) => {
|
||||
try
|
||||
{
|
||||
var results =
|
||||
await provider.GetGeoFeedData();
|
||||
return results.ToGeoFeedCsv();
|
||||
if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? feed))
|
||||
{
|
||||
feed = await provider.GetGeoFeedData();
|
||||
if (environment.IsProduction())
|
||||
{
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set("Geofeed", feed, cacheEntryOptions);
|
||||
}
|
||||
}
|
||||
|
||||
return feed.ToGeoFeedCsv();
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||
string geoFeedData = cacheProvider.GetGeoFeed();
|
||||
return geoFeedData;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Error: {ex}");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue