remove cached mod data not requested within 48 hours (#651)
This commit is contained in:
parent
17c6ae7ed9
commit
edc00ddaab
|
@ -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
|
|||
/// <summary>The background task server.</summary>
|
||||
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;
|
||||
|
||||
/// <summary>The cache in which to store mod data.</summary>
|
||||
private static IModCacheRepository ModCache;
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
|
@ -30,10 +34,12 @@ namespace StardewModdingAPI.Web
|
|||
** Hosted service
|
||||
****/
|
||||
/// <summary>Construct an instance.</summary>
|
||||
/// <param name="wikiCache">The cache in which to store mod metadata.</param>
|
||||
public BackgroundService(IWikiCacheRepository wikiCache)
|
||||
/// <param name="wikiCache">The cache in which to store wiki metadata.</param>
|
||||
/// <param name="modCache">The cache in which to store mod data.</param>
|
||||
public BackgroundService(IWikiCacheRepository wikiCache, IModCacheRepository modCache)
|
||||
{
|
||||
BackgroundService.WikiCache = wikiCache;
|
||||
BackgroundService.ModCache = modCache;
|
||||
}
|
||||
|
||||
/// <summary>Start the service.</summary>
|
||||
|
@ -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 _);
|
||||
}
|
||||
|
||||
/// <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
|
||||
|
|
|
@ -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
|
|||
/// <param name="mod">The mod data.</param>
|
||||
/// <param name="cachedMod">The stored mod record.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,6 +67,14 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue