37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
|
|
using AS1024.GeoFeed.Interfaces;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace AS1024.GeoFeed.GeoFeedBuilder
|
|
{
|
|
public class PreLoadGeoFeed : IHostedService
|
|
{
|
|
private readonly ILogger<PreLoadGeoFeed> logger;
|
|
private readonly IGeoFeedProvider provider;
|
|
private readonly IMemoryCache memoryCache;
|
|
private const string GeoFeedCacheKey = "GeoFeedData";
|
|
|
|
public PreLoadGeoFeed(ILogger<PreLoadGeoFeed> logger,
|
|
IGeoFeedProvider provider,
|
|
IMemoryCache memoryCache)
|
|
{
|
|
this.logger = logger;
|
|
this.provider = provider;
|
|
this.memoryCache = memoryCache;
|
|
}
|
|
|
|
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
logger.LogInformation("Preloading GeoFeed data in memory...");
|
|
var feed = await provider.GetGeoFeedData();
|
|
var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(1));
|
|
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
|
}
|
|
|
|
Task IHostedService.StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|