Don't use memory cache if it's in development mode

This commit is contained in:
Jeff Leung 2024-01-04 23:41:31 -08:00
parent af492dedff
commit fec33bff1d
1 changed files with 9 additions and 2 deletions

View File

@ -15,24 +15,31 @@ namespace AS1024.GeoFeed.Controllers
{
private readonly IGeoFeedProvider builder;
private readonly IMemoryCache memoryCache;
private readonly IWebHostEnvironment environment;
private const string GeoFeedCacheKey = "GeoFeedData";
public GeofeedController(IGeoFeedProvider builder,
IMemoryCache memoryCache) {
IMemoryCache memoryCache,
IWebHostEnvironment environment) {
this.builder = builder;
this.memoryCache = memoryCache;
this.environment = environment;
}
[HttpGet]
[Route("")]
public async Task<IActionResult> Get()
{
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed)
&& environment.IsProduction())
{
feed = await builder.GetGeoFeedData();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
} else
{
feed = await builder.GetGeoFeedData();
}
var csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.