Initial work on getting minimal API to work
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
|
||||
58
AS1024.GeoFeed.Core/CacheService/GeoFeedCacheService.cs
Normal file
58
AS1024.GeoFeed.Core/CacheService/GeoFeedCacheService.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.CacheService
|
||||
{
|
||||
public class GeoFeedCacheService : IHostedService
|
||||
{
|
||||
private readonly ILogger<GeoFeedCacheService> logger;
|
||||
private readonly IGeoFeedProvider feedProvider;
|
||||
private readonly IHost host;
|
||||
|
||||
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
||||
IGeoFeedProvider feedProvider,
|
||||
IHost host)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.feedProvider = feedProvider;
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_ = StartPerioidicSync(cancellationToken);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<bool> StartPerioidicSync(CancellationToken Token)
|
||||
{
|
||||
while (!Token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var scope = host.Services.CreateScope();
|
||||
|
||||
var persistentCacheProvider =
|
||||
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
||||
|
||||
var results = await feedProvider.GetGeoFeedData();
|
||||
await persistentCacheProvider.CacheGeoFeed(results);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry...");
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromMinutes(30));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
AS1024.GeoFeed.Core/CacheService/GeoFeedLocalFileCache.cs
Normal file
55
AS1024.GeoFeed.Core/CacheService/GeoFeedLocalFileCache.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.CacheService
|
||||
{
|
||||
public class GeoFeedLocalFileCache : IGeoFeedPersistentCacheProvider
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly ILogger<GeoFeedLocalFileCache> logger;
|
||||
|
||||
public GeoFeedLocalFileCache(IConfiguration _configuration,
|
||||
ILogger<GeoFeedLocalFileCache> logger)
|
||||
{
|
||||
configuration = _configuration;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
string IGeoFeedPersistentCacheProvider.ProviderName => "file";
|
||||
|
||||
Task<bool> IGeoFeedPersistentCacheProvider.CacheGeoFeed(IList<IPGeoFeed> pGeoFeeds)
|
||||
{
|
||||
string? tempPath = GetTempPath();
|
||||
logger.LogInformation($"Writing geofeed data to: {tempPath}");
|
||||
File.WriteAllText(tempPath, pGeoFeeds.ToList().
|
||||
ToGeoFeedCsv());
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
private string GetTempPath()
|
||||
{
|
||||
string? tempPath = Path.Combine(Path.GetTempPath(), "geoFeedCache.csv");
|
||||
|
||||
logger.LogInformation($"Getting GeoFeed data from: {tempPath}");
|
||||
|
||||
if (!string.IsNullOrEmpty(configuration["TempCache"]))
|
||||
tempPath = configuration["TempCache"];
|
||||
return tempPath;
|
||||
}
|
||||
|
||||
string IGeoFeedPersistentCacheProvider.GetGeoFeed()
|
||||
{
|
||||
return File.ReadAllText(GetTempPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
54
AS1024.GeoFeed.Core/GeoFeedPreloader/PreloadGeoFeed.cs
Normal file
54
AS1024.GeoFeed.Core/GeoFeedPreloader/PreloadGeoFeed.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.GeoFeedPreloader
|
||||
{
|
||||
public class PreLoadGeoFeed : IHostedService
|
||||
{
|
||||
private readonly ILogger<PreLoadGeoFeed> logger;
|
||||
private readonly IGeoFeedProvider provider;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IHostEnvironment environment;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public PreLoadGeoFeed(ILogger<PreLoadGeoFeed> logger,
|
||||
IGeoFeedProvider provider,
|
||||
IMemoryCache memoryCache,
|
||||
IHostEnvironment environment)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.provider = provider;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (environment.IsProduction())
|
||||
await StartPreLoad();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning($"Failed to preload, exception settings below:\n{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StartPreLoad()
|
||||
{
|
||||
logger.LogInformation("Preloading GeoFeed data in memory...");
|
||||
List<IPGeoFeed> feed = await provider.GetGeoFeedData();
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
}
|
||||
|
||||
Task IHostedService.StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
AS1024.GeoFeed.Core/Interfaces/IGeoFeedCacheProvider.cs
Normal file
31
AS1024.GeoFeed.Core/Interfaces/IGeoFeedCacheProvider.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using AS1024.GeoFeed.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a persistent cache GeoFeed provider
|
||||
/// </summary>
|
||||
public interface IGeoFeedPersistentCacheProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Name of the provider
|
||||
/// </summary>
|
||||
public string ProviderName { get; }
|
||||
/// <summary>
|
||||
/// Returns the GeoFeed
|
||||
/// </summary>
|
||||
/// <returns>String of the CSV geofeed</returns>
|
||||
public string GetGeoFeed();
|
||||
/// <summary>
|
||||
/// Stores the GeoFeed in the cache backend
|
||||
/// </summary>
|
||||
/// <param name="pGeoFeeds">GeoFeed retrieved from the backend</param>
|
||||
/// <returns></returns>
|
||||
public Task<bool> CacheGeoFeed(IList<IPGeoFeed> pGeoFeeds);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user