Append when the GeoFeed is retrieved and if it was generated from in memory cache

This commit is contained in:
Jeff Leung 2024-01-16 13:05:58 -08:00
parent 690a117ffd
commit c1ed05a335
3 changed files with 22 additions and 5 deletions

View File

@ -5,10 +5,23 @@ namespace AS1024.GeoFeed.Core.Tools
{ {
public static class GeoFeedTools public static class GeoFeedTools
{ {
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds) /// <summary>
/// Returns a CSV string for a given GeoFeed retreived from various sources
/// </summary>
/// <param name="geoFeeds">GeoFeed returned from the source of truth</param>
/// <param name="timeStamp">If a timestamp should be appended at the header</param>
/// <param name="isCached">If the result is cached</param>
/// <returns></returns>
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds, bool timeStamp = false, bool isCached = false)
{ {
StringBuilder csvContent = new(); StringBuilder csvContent = new();
if (timeStamp)
csvContent.AppendLine($"# GeoFeed generated on {DateTime.UtcNow:R}");
if (isCached)
csvContent.AppendLine($"# Geofeed data is returned from local in memory cache");
foreach (IPGeoFeed feed in geoFeeds) foreach (IPGeoFeed feed in geoFeeds)
{ {
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}"); csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}");

View File

@ -55,10 +55,12 @@ namespace AS1024.GeoFeed.MinimalAPI
IMemoryCache memoryCache, IMemoryCache memoryCache,
IWebHostEnvironment environment) IWebHostEnvironment environment)
{ {
bool isCached = true;
try try
{ {
if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? feed)) if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? feed))
{ {
isCached = false;
feed = await provider.GetGeoFeedData(); feed = await provider.GetGeoFeedData();
if (environment.IsProduction()) if (environment.IsProduction())
{ {
@ -68,7 +70,7 @@ namespace AS1024.GeoFeed.MinimalAPI
} }
} }
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv()), return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
"text/csv", "text/csv",
"geofeed.csv"); "geofeed.csv");

View File

@ -36,10 +36,12 @@ namespace AS1024.GeoFeed.Controllers
[Route("")] [Route("")]
public async Task<IActionResult> Get() public async Task<IActionResult> Get()
{ {
bool isCached = true;
try try
{ {
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed)) if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
{ {
isCached = false;
feed = await builder.GetGeoFeedData(); feed = await builder.GetGeoFeedData();
if (environment.IsProduction()) if (environment.IsProduction())
{ {
@ -49,7 +51,7 @@ namespace AS1024.GeoFeed.Controllers
} }
} }
return ReturnFile(feed); return ReturnFile(feed, isCached);
} catch (HttpRequestException ex) } catch (HttpRequestException ex)
{ {
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}"); logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
@ -66,9 +68,9 @@ namespace AS1024.GeoFeed.Controllers
} }
[NonAction] [NonAction]
private IActionResult ReturnFile(List<IPGeoFeed>? feed) private IActionResult ReturnFile(List<IPGeoFeed>? feed, bool isCached = false)
{ {
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format. string csvContent = feed.ToGeoFeedCsv(true, isCached); // Assuming ToGeoFeedCsv() returns a string in CSV format.
return ReturnFile(csvContent); return ReturnFile(csvContent);
} }