Rename the GetGeofeed Method to Async
This commit is contained in:
parent
3a56e2426e
commit
e0189381f1
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ namespace AS1024.GeoFeed.Core.CacheService
|
||||||
var persistentCacheProvider =
|
var persistentCacheProvider =
|
||||||
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
||||||
|
|
||||||
var results = await feedProvider.GetGeoFeedData();
|
var results = await feedProvider.GetGeoFeedDataAsync();
|
||||||
await persistentCacheProvider.CacheGeoFeed(results);
|
await persistentCacheProvider.CacheGeoFeed(results);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
|
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
using System.Text;
|
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedLogic
|
|
||||||
{
|
|
||||||
public class GeoFeedReturn
|
|
||||||
{
|
|
||||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
|
||||||
private readonly IGeoFeedProvider provider;
|
|
||||||
private readonly ILogger<GeoFeedReturn> logger;
|
|
||||||
private readonly IGeoFeedPersistentCacheProvider cacheProvider;
|
|
||||||
private readonly IMemoryCache memoryCache;
|
|
||||||
private readonly IWebHostEnvironment environment;
|
|
||||||
|
|
||||||
public GeoFeedReturn(IGeoFeedProvider provider,
|
|
||||||
ILogger<GeoFeedReturn> logger,
|
|
||||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
|
||||||
IMemoryCache memoryCache,
|
|
||||||
IWebHostEnvironment environment)
|
|
||||||
{
|
|
||||||
this.provider = provider;
|
|
||||||
this.logger = logger;
|
|
||||||
this.cacheProvider = cacheProvider;
|
|
||||||
this.memoryCache = memoryCache;
|
|
||||||
this.environment = environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IResult> GetGeoFeed()
|
|
||||||
{
|
|
||||||
bool isCached = true;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
|
||||||
{
|
|
||||||
isCached = false;
|
|
||||||
feed = await provider.GetGeoFeedData();
|
|
||||||
if (environment.IsProduction())
|
|
||||||
{
|
|
||||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
||||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
|
||||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
|
|
||||||
"text/csv",
|
|
||||||
"geofeed.csv");
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (HttpRequestException ex)
|
|
||||||
{
|
|
||||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
|
||||||
string geoFeedData = cacheProvider.GetGeoFeed();
|
|
||||||
|
|
||||||
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
|
|
||||||
"text/csv",
|
|
||||||
"geofeed.csv");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError($"Error: {ex}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.NoContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -41,7 +41,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedPreloader
|
||||||
private async Task StartPreLoad()
|
private async Task StartPreLoad()
|
||||||
{
|
{
|
||||||
logger.LogInformation("Preloading GeoFeed data in memory...");
|
logger.LogInformation("Preloading GeoFeed data in memory...");
|
||||||
List<IPGeoFeed> feed = await provider.GetGeoFeedData();
|
List<IPGeoFeed> feed = await provider.GetGeoFeedDataAsync();
|
||||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<IPGeoFeed>> GetGeoFeedData()
|
public async Task<List<IPGeoFeed>> GetGeoFeedDataAsync()
|
||||||
{
|
{
|
||||||
List<IPGeoFeed> geoFeed = [];
|
List<IPGeoFeed> geoFeed = [];
|
||||||
using HttpClient client = httpClientFactory.CreateClient();
|
using HttpClient client = httpClientFactory.CreateClient();
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@ namespace AS1024.GeoFeed.Core.Interfaces
|
||||||
public interface IGeoFeedProvider
|
public interface IGeoFeedProvider
|
||||||
{
|
{
|
||||||
public string GeoFeedProviderName { get; }
|
public string GeoFeedProviderName { get; }
|
||||||
public Task<List<IPGeoFeed>> GetGeoFeedData();
|
public Task<List<IPGeoFeed>> GetGeoFeedDataAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue