remove cached mod data not requested within 48 hours (#651)

This commit is contained in:
Jesse Plamondon-Willard 2019-07-24 18:34:28 -04:00
parent 17c6ae7ed9
commit edc00ddaab
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 31 additions and 4 deletions

View File

@ -5,6 +5,7 @@ using Hangfire;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
using StardewModdingAPI.Web.Framework.Caching.Mods;
using StardewModdingAPI.Web.Framework.Caching.Wiki; using StardewModdingAPI.Web.Framework.Caching.Wiki;
namespace StardewModdingAPI.Web namespace StardewModdingAPI.Web
@ -19,9 +20,12 @@ namespace StardewModdingAPI.Web
/// <summary>The background task server.</summary> /// <summary>The background task server.</summary>
private static BackgroundJobServer JobServer; private static BackgroundJobServer JobServer;
/// <summary>The cache in which to store mod metadata.</summary> /// <summary>The cache in which to store wiki metadata.</summary>
private static IWikiCacheRepository WikiCache; private static IWikiCacheRepository WikiCache;
/// <summary>The cache in which to store mod data.</summary>
private static IModCacheRepository ModCache;
/********* /*********
** Public methods ** Public methods
@ -30,10 +34,12 @@ namespace StardewModdingAPI.Web
** Hosted service ** Hosted service
****/ ****/
/// <summary>Construct an instance.</summary> /// <summary>Construct an instance.</summary>
/// <param name="wikiCache">The cache in which to store mod metadata.</param> /// <param name="wikiCache">The cache in which to store wiki metadata.</param>
public BackgroundService(IWikiCacheRepository wikiCache) /// <param name="modCache">The cache in which to store mod data.</param>
public BackgroundService(IWikiCacheRepository wikiCache, IModCacheRepository modCache)
{ {
BackgroundService.WikiCache = wikiCache; BackgroundService.WikiCache = wikiCache;
BackgroundService.ModCache = modCache;
} }
/// <summary>Start the service.</summary> /// <summary>Start the service.</summary>
@ -44,9 +50,11 @@ namespace StardewModdingAPI.Web
// set startup tasks // set startup tasks
BackgroundJob.Enqueue(() => BackgroundService.UpdateWikiAsync()); BackgroundJob.Enqueue(() => BackgroundService.UpdateWikiAsync());
BackgroundJob.Enqueue(() => BackgroundService.RemoveStaleMods());
// set recurring tasks // 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; return Task.CompletedTask;
} }
@ -75,6 +83,12 @@ namespace StardewModdingAPI.Web
BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out _, out _); BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out _, out _);
} }
/// <summary>Remove mods which haven't been requested in over 48 hours.</summary>
public static async Task RemoveStaleMods()
{
BackgroundService.ModCache.RemoveStaleMods(TimeSpan.FromHours(48));
}
/********* /*********
** Private method ** Private method

View File

@ -1,3 +1,4 @@
using System;
using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Web.Framework.ModRepositories; using StardewModdingAPI.Web.Framework.ModRepositories;
@ -22,5 +23,9 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
/// <param name="mod">The mod data.</param> /// <param name="mod">The mod data.</param>
/// <param name="cachedMod">The stored mod record.</param> /// <param name="cachedMod">The stored mod record.</param>
void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod); void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod);
/// <summary>Delete data for mods which haven't been requested within a given time limit.</summary>
/// <param name="age">The minimum age for which to remove mods.</param>
void RemoveStaleMods(TimeSpan age);
} }
} }

View File

@ -67,6 +67,14 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
cachedMod = this.SaveMod(new CachedMod(site, id, mod)); cachedMod = this.SaveMod(new CachedMod(site, id, mod));
} }
/// <summary>Delete data for mods which haven't been requested within a given time limit.</summary>
/// <param name="age">The minimum age for which to remove mods.</param>
public void RemoveStaleMods(TimeSpan age)
{
DateTimeOffset minDate = DateTimeOffset.UtcNow.Subtract(age);
var result = this.Mods.DeleteMany(p => p.LastRequested < minDate);
}
/********* /*********
** Private methods ** Private methods