Complete the implementation of returning cached data from the local disk cache
This commit is contained in:
@@ -4,6 +4,7 @@ using AS1024.GeoFeed.GeoFeedBuilder;
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using AS1024.GeoFeed.Models;
|
using AS1024.GeoFeed.Models;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using AS1024.GeoFeed.GeoFeedLocalCache;
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Controllers
|
namespace AS1024.GeoFeed.Controllers
|
||||||
{
|
{
|
||||||
@@ -17,13 +18,16 @@ namespace AS1024.GeoFeed.Controllers
|
|||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly IWebHostEnvironment environment;
|
private readonly IWebHostEnvironment environment;
|
||||||
private readonly ILogger<GeofeedController> logger;
|
private readonly ILogger<GeofeedController> logger;
|
||||||
|
private readonly GeoFeedCacheDbContext dbContext;
|
||||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||||
|
|
||||||
public GeofeedController(IGeoFeedProvider builder,
|
public GeofeedController(IGeoFeedProvider builder,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
IWebHostEnvironment environment,
|
IWebHostEnvironment environment,
|
||||||
ILogger<GeofeedController> logger) {
|
ILogger<GeofeedController> logger,
|
||||||
|
GeoFeedCacheDbContext dbContext) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.dbContext = dbContext;
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
@@ -46,19 +50,36 @@ namespace AS1024.GeoFeed.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
return ReturnFile(feed);
|
||||||
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
} catch (HttpRequestException ex)
|
||||||
string contentType = "text/csv";
|
{
|
||||||
|
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||||
|
var results =
|
||||||
|
dbContext.GeoFeedCacheEntries.ToList();
|
||||||
|
List<IPGeoFeed> cachedData = [];
|
||||||
|
results.ForEach(cachedData.Add);
|
||||||
|
return ReturnFile(cachedData);
|
||||||
|
}
|
||||||
|
|
||||||
return new FileContentResult(contentBytes, contentType)
|
catch (Exception ex)
|
||||||
{
|
|
||||||
FileDownloadName = "geofeed.csv"
|
|
||||||
};
|
|
||||||
} catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
logger.LogError($"Geofeed generation failed. Exception: {ex}");
|
logger.LogError($"Geofeed generation failed. Exception: {ex}");
|
||||||
return StatusCode(500);
|
return StatusCode(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[NonAction]
|
||||||
|
private IActionResult ReturnFile(List<IPGeoFeed>? feed)
|
||||||
|
{
|
||||||
|
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||||
|
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||||
|
string contentType = "text/csv";
|
||||||
|
|
||||||
|
return new FileContentResult(contentBytes, contentType)
|
||||||
|
{
|
||||||
|
FileDownloadName = "geofeed.csv"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user