diff --git a/AS1024.GeoFeed/GeoFeedBuilder/GeoFeedCacheService.cs b/AS1024.GeoFeed/GeoFeedBuilder/GeoFeedCacheService.cs index 33dd36e..ceaa071 100644 --- a/AS1024.GeoFeed/GeoFeedBuilder/GeoFeedCacheService.cs +++ b/AS1024.GeoFeed/GeoFeedBuilder/GeoFeedCacheService.cs @@ -1,26 +1,63 @@ - using System.ComponentModel.DataAnnotations; using AS1024.GeoFeed.Models; +using AS1024.GeoFeed.GeoFeedBuilder; using Microsoft.EntityFrameworkCore; +using AS1024.GeoFeed.Interfaces; -public class GeoFeedCacheService : IHostedService +namespace AS1024.GeoFeed.GeoFeedCacheService { - public Task StartAsync(CancellationToken cancellationToken) + public class GeoFeedCacheService : IHostedService { - throw new NotImplementedException(); + CancellationToken _cancellationToken; + private readonly ILogger logger; + private readonly IGeoFeedProvider feedProvider; + + public GeoFeedCacheService(ILogger logger, + IGeoFeedProvider feedProvider) + { + this.logger = logger; + this.feedProvider = feedProvider; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _ = StartPerioidicSync(); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public async Task StartPerioidicSync(){ + List geoFeedCacheEntry = []; + while (!_cancellationToken.IsCancellationRequested) { + await Task.Delay(TimeSpan.FromMinutes(30)); + logger.LogInformation("Running on disk fallback cache process..."); + var results = await feedProvider.GetGeoFeedData(); + + results.ForEach(x => { + geoFeedCacheEntry.Add(new (){ + Prefix = x.Prefix, + GeolocCity = x.GeolocCity, + GeolocCountry = x.GeolocCountry, + GeolocHasLocation = x.GeolocHasLocation, + GeolocPostalCode = x.GeolocPostalCode, + GeolocRegion = x.GeolocRegion + }); + }); + } + return false; + } } - public Task StopAsync(CancellationToken cancellationToken) - { - throw new NotImplementedException(); + public class GeoFeedCacheEntry : IPGeoFeed { + [Key] + public int Id { get; set; } } -} -public class GeoFeedCacheEntry : IPGeoFeed { - [Key] - public int Id { get; set; } -} - -public class GeoFeedCacheDbContext : DbContext { - public virtual DbSet GeoFeedCacheEntries {get;set;} + public class GeoFeedCacheDbContext : DbContext { + public virtual DbSet GeoFeedCacheEntries {get;set;} + } } \ No newline at end of file