using AS1024.GeoFeed.Core.Interfaces; using AS1024.GeoFeed.Models; using AS1024.GeoFeed.Core.Tools; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; namespace AS1024.GeoFeed.Core.CacheService { public class GeoFeedLocalFileCache : IGeoFeedPersistentCacheProvider { private readonly IConfiguration configuration; private readonly ILogger logger; public GeoFeedLocalFileCache(IConfiguration _configuration, ILogger logger) { configuration = _configuration; this.logger = logger; } string IGeoFeedPersistentCacheProvider.ProviderName => "file"; Task IGeoFeedPersistentCacheProvider.CacheGeoFeed(IList pGeoFeeds) { string? tempPath = GetTempPath(); logger.LogInformation($"Writing geofeed data to: {tempPath}"); File.WriteAllText(tempPath, pGeoFeeds.ToList(). ToGeoFeedCsv()); return Task.FromResult(true); } private string GetTempPath() { string? tempPath = Path.Combine(Path.GetTempPath(), "geoFeedCache.csv"); logger.LogInformation($"Getting GeoFeed data from: {tempPath}"); if (!string.IsNullOrEmpty(configuration["TempCache"])) tempPath = configuration["TempCache"]; return tempPath; } string IGeoFeedPersistentCacheProvider.GetGeoFeed() { return File.ReadAllText(GetTempPath()); } } }