diff --git a/Overflow.Web/Components/Pages/Spills.razor b/Overflow.Web/Components/Pages/Spills.razor
index cfe0aad..731adb7 100644
--- a/Overflow.Web/Components/Pages/Spills.razor
+++ b/Overflow.Web/Components/Pages/Spills.razor
@@ -21,23 +21,7 @@
@code {
- private SouthernWaterApiJob? job;
- [Inject] private IMongoDatabase database { get; set; }
+ private SouthernWaterApiJob? job => cache.CurrentSouthernWaterApiJob;
+ [Inject] private SpillCache cache { get; set; }
// private bool showIds;
-
- protected override async Task OnInitializedAsync()
- {
- job = database.GetCollection(Static.JobCollectionName)
- .AsQueryable()
- .OrderByDescending(j => j.EndTime)
- .FirstOrDefault();
-
- if (job is not null)
- {
- job.Spills = database.GetCollection(Static.SpillCollectionName)
- .AsQueryable()
- .Where(s => s.JobId == job._id)
- .ToList();
- }
- }
}
\ No newline at end of file
diff --git a/Overflow.Web/Components/Pages/SpillsCalendarPage.razor b/Overflow.Web/Components/Pages/SpillsCalendarPage.razor
index 5577fa8..d9d28de 100644
--- a/Overflow.Web/Components/Pages/SpillsCalendarPage.razor
+++ b/Overflow.Web/Components/Pages/SpillsCalendarPage.razor
@@ -25,24 +25,8 @@
@code {
- private SouthernWaterApiJob? job;
- [Inject] private IMongoDatabase database { get; set; }
+ private SouthernWaterApiJob? job => cache.CurrentSouthernWaterApiJob;
+ [Inject] private SpillCache cache { get; set; }
// private bool showIds;
private bool genuineOnly = true;
-
- protected override async Task OnInitializedAsync()
- {
- job = database.GetCollection(Static.JobCollectionName)
- .AsQueryable()
- .OrderByDescending(j => j.EndTime)
- .FirstOrDefault();
-
- if (job is not null)
- {
- job.Spills = database.GetCollection(Static.SpillCollectionName)
- .AsQueryable()
- .Where(s => s.JobId == job._id)
- .ToList();
- }
- }
}
\ No newline at end of file
diff --git a/Overflow.Web/Program.cs b/Overflow.Web/Program.cs
index f2514c9..f7f108c 100644
--- a/Overflow.Web/Program.cs
+++ b/Overflow.Web/Program.cs
@@ -60,6 +60,22 @@ builder.Services.AddQuartz(q =>
.WithCronSchedule(builder.Configuration.GetSection("SouthernWater").GetValue("Cron") ?? "0 0 8 * * ?")
.WithDescription("Periodic trigger for Southern Water API pulling")
);
+
+ var cacheKey = new JobKey("cache-refresh", "cache");
+
+ q.AddJob(j => j
+ .WithDescription("Refresh caches")
+ .WithIdentity(cacheKey)
+ .UsingJobData("IsFull", false)
+ );
+
+ q.AddTrigger(t => t
+ .WithIdentity("cache-refresh-trigger")
+ .ForJob(cacheKey)
+ .StartNow()
+ .WithCronSchedule(builder.Configuration.GetSection("Cache").GetValue("Cron") ?? "0 0 8 * * ?")
+ .WithDescription("Periodic trigger for cache refreshing")
+ );
});
// ASP.NET Core hosting
@@ -74,6 +90,10 @@ builder.Services.AddSingleton();
builder.Services.AddScoped();
builder.Services.AddTransient();
+builder.Services.AddSingleton();
+builder.Services.AddSingleton();
+builder.Services.AddHostedService();
+
builder.Services.AddRadzenComponents();
var app = builder.Build();
diff --git a/Overflow.Web/appsettings.json b/Overflow.Web/appsettings.json
index 612c7fa..a569598 100644
--- a/Overflow.Web/appsettings.json
+++ b/Overflow.Web/appsettings.json
@@ -11,5 +11,8 @@
"AllowedHosts": "*",
"SouthernWater": {
"Cron": "0 26 20 * * ?"
+ },
+ "Cache": {
+ "Cron": "0 */3 * * * ?"
}
}
diff --git a/Overflow/CacheReloadJob.cs b/Overflow/CacheReloadJob.cs
new file mode 100644
index 0000000..923bd29
--- /dev/null
+++ b/Overflow/CacheReloadJob.cs
@@ -0,0 +1,15 @@
+using Microsoft.Extensions.Logging;
+using Quartz;
+
+namespace Overflow;
+
+[DisallowConcurrentExecution]
+public class CacheReloadJob(SpillCache cache, ILogger logger): IJob
+{
+ public Task Execute(IJobExecutionContext context)
+ {
+ logger.LogDebug("Refreshing caches");
+ cache.Refresh();
+ return Task.CompletedTask;
+ }
+}
\ No newline at end of file
diff --git a/Overflow/Overflow.csproj b/Overflow/Overflow.csproj
index 4aa6a63..d13b979 100644
--- a/Overflow/Overflow.csproj
+++ b/Overflow/Overflow.csproj
@@ -10,6 +10,8 @@
+
+
diff --git a/Overflow/SouthernWater/SouthernWaterSpillCache.cs b/Overflow/SouthernWater/SouthernWaterSpillCache.cs
new file mode 100644
index 0000000..78b79da
--- /dev/null
+++ b/Overflow/SouthernWater/SouthernWaterSpillCache.cs
@@ -0,0 +1,44 @@
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using MongoDB.Driver;
+
+namespace Overflow.SouthernWater;
+
+public class SouthernWaterSpillCache(IServiceProvider serviceProvider, ILogger logger)
+{
+ private readonly ILogger _logger = logger;
+
+ private SouthernWaterApiJob _currentJob;
+
+ public SouthernWaterApiJob CurrentJob => _currentJob;
+
+ public void ReloadJob()
+ {
+ _logger.LogInformation("Refreshing Southern Water Spills");
+
+ using var scope = serviceProvider.CreateScope();
+
+ var database = scope.ServiceProvider.GetRequiredService();
+
+ var job = database.GetCollection(Static.JobCollectionName)
+ .AsQueryable()
+ .OrderByDescending(j => j.EndTime)
+ .FirstOrDefault();
+
+ if (job is not null)
+ {
+ job.Spills = database.GetCollection(Static.SpillCollectionName)
+ .AsQueryable()
+ .Where(s => s.JobId == job._id)
+ .ToList();
+
+ _currentJob = job;
+
+ _logger.LogInformation("Southern Water Spills cache refreshed");
+ }
+ else
+ {
+ _logger.LogWarning("No Southern Water Spills returned");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Overflow/SpillCache.cs b/Overflow/SpillCache.cs
new file mode 100644
index 0000000..caa7db1
--- /dev/null
+++ b/Overflow/SpillCache.cs
@@ -0,0 +1,35 @@
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using Overflow.SouthernWater;
+using Quartz;
+
+namespace Overflow;
+
+public class SpillCache(SouthernWaterSpillCache southernWaterSpillCache, ILogger logger)
+{
+ public SouthernWaterApiJob CurrentSouthernWaterApiJob => southernWaterSpillCache.CurrentJob;
+
+ public void Refresh()
+ {
+ logger.LogDebug("Refreshing caches");
+
+ southernWaterSpillCache.ReloadJob();
+ }
+}
+
+public class LoadCacheOnStart(ISchedulerFactory scheduler, SpillCache cache, ILogger logger) : IHostedService
+{
+ public async Task StartAsync(CancellationToken cancellationToken)
+ {
+ logger.LogInformation("Loading caches for startup");
+
+ await (await scheduler.GetScheduler()).TriggerJob(new JobKey("cache-refresh", "cache"));
+ // cache.Refresh();
+
+ }
+
+ public Task StopAsync(CancellationToken cancellationToken)
+ {
+ return Task.CompletedTask;
+ }
+}
\ No newline at end of file