summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-07-24 18:34:28 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 18:58:59 -0400
commitedc00ddaab46a2a2d0ba07591a6206159421ef41 (patch)
treeb2bb3705854612153508bddbc92a9e70a55fc0dd /src/SMAPI.Web
parent17c6ae7ed995344111513ca91b18ec6598ec2399 (diff)
downloadSMAPI-edc00ddaab46a2a2d0ba07591a6206159421ef41.tar.gz
SMAPI-edc00ddaab46a2a2d0ba07591a6206159421ef41.tar.bz2
SMAPI-edc00ddaab46a2a2d0ba07591a6206159421ef41.zip
remove cached mod data not requested within 48 hours (#651)
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r--src/SMAPI.Web/BackgroundService.cs22
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs5
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs8
3 files changed, 31 insertions, 4 deletions
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
/// <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
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
/// <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);
}
}
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));
}
+ /// <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