diff --git a/src/SMAPI.Web/BackgroundService.cs b/src/SMAPI.Web/BackgroundService.cs index 2ccfd5f7..2dd3b921 100644 --- a/src/SMAPI.Web/BackgroundService.cs +++ b/src/SMAPI.Web/BackgroundService.cs @@ -5,6 +5,7 @@ using Hangfire; using Microsoft.Extensions.Hosting; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; +using StardewModdingAPI.Web.Framework.Caching.Mods; using StardewModdingAPI.Web.Framework.Caching.Wiki; namespace StardewModdingAPI.Web @@ -19,9 +20,12 @@ namespace StardewModdingAPI.Web /// The background task server. private static BackgroundJobServer JobServer; - /// The cache in which to store mod metadata. + /// The cache in which to store wiki metadata. private static IWikiCacheRepository WikiCache; + /// The cache in which to store mod data. + private static IModCacheRepository ModCache; + /********* ** Public methods @@ -30,10 +34,12 @@ namespace StardewModdingAPI.Web ** Hosted service ****/ /// Construct an instance. - /// The cache in which to store mod metadata. - public BackgroundService(IWikiCacheRepository wikiCache) + /// The cache in which to store wiki metadata. + /// The cache in which to store mod data. + public BackgroundService(IWikiCacheRepository wikiCache, IModCacheRepository modCache) { BackgroundService.WikiCache = wikiCache; + BackgroundService.ModCache = modCache; } /// Start the service. @@ -44,9 +50,11 @@ namespace StardewModdingAPI.Web // set startup tasks BackgroundJob.Enqueue(() => BackgroundService.UpdateWikiAsync()); + BackgroundJob.Enqueue(() => BackgroundService.RemoveStaleMods()); // set recurring tasks - RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *"); + RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *"); // every 10 minutes + RecurringJob.AddOrUpdate(() => BackgroundService.RemoveStaleMods(), "0 * * * *"); // hourly return Task.CompletedTask; } @@ -75,6 +83,12 @@ namespace StardewModdingAPI.Web BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out _, out _); } + /// Remove mods which haven't been requested in over 48 hours. + public static async Task RemoveStaleMods() + { + BackgroundService.ModCache.RemoveStaleMods(TimeSpan.FromHours(48)); + } + /********* ** Private method diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs index 23929d1d..bcec8b36 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs @@ -1,3 +1,4 @@ +using System; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.ModRepositories; @@ -22,5 +23,9 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods /// The mod data. /// The stored mod record. void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod); + + /// Delete data for mods which haven't been requested within a given time limit. + /// The minimum age for which to remove mods. + void RemoveStaleMods(TimeSpan age); } } diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs index d8ad7d21..4258cc85 100644 --- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs +++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs @@ -67,6 +67,14 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods cachedMod = this.SaveMod(new CachedMod(site, id, mod)); } + /// Delete data for mods which haven't been requested within a given time limit. + /// The minimum age for which to remove mods. + public void RemoveStaleMods(TimeSpan age) + { + DateTimeOffset minDate = DateTimeOffset.UtcNow.Subtract(age); + var result = this.Mods.DeleteMany(p => p.LastRequested < minDate); + } + /********* ** Private methods