diff options
Diffstat (limited to 'src/SMAPI.Web/Controllers/ModsController.cs')
-rw-r--r-- | src/SMAPI.Web/Controllers/ModsController.cs | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/src/SMAPI.Web/Controllers/ModsController.cs b/src/SMAPI.Web/Controllers/ModsController.cs index ca866a8d..b621ded0 100644 --- a/src/SMAPI.Web/Controllers/ModsController.cs +++ b/src/SMAPI.Web/Controllers/ModsController.cs @@ -1,12 +1,8 @@ -using System; using System.Linq; using System.Text.RegularExpressions; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; -using StardewModdingAPI.Toolkit; -using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; +using StardewModdingAPI.Web.Framework.Caching.Wiki; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.ViewModels; @@ -19,10 +15,10 @@ namespace StardewModdingAPI.Web.Controllers ** Fields *********/ /// <summary>The cache in which to store mod metadata.</summary> - private readonly IMemoryCache Cache; + private readonly IWikiCacheRepository Cache; - /// <summary>The number of minutes successful update checks should be cached before refetching them.</summary> - private readonly int CacheMinutes; + /// <summary>The number of minutes before which wiki data should be considered old.</summary> + private readonly int StaleMinutes; /********* @@ -31,20 +27,20 @@ namespace StardewModdingAPI.Web.Controllers /// <summary>Construct an instance.</summary> /// <param name="cache">The cache in which to store mod metadata.</param> /// <param name="configProvider">The config settings for mod update checks.</param> - public ModsController(IMemoryCache cache, IOptions<ModCompatibilityListConfig> configProvider) + public ModsController(IWikiCacheRepository cache, IOptions<ModCompatibilityListConfig> configProvider) { ModCompatibilityListConfig config = configProvider.Value; this.Cache = cache; - this.CacheMinutes = config.CacheMinutes; + this.StaleMinutes = config.StaleMinutes; } /// <summary>Display information for all mods.</summary> [HttpGet] [Route("mods")] - public async Task<ViewResult> Index() + public ViewResult Index() { - return this.View("Index", await this.FetchDataAsync()); + return this.View("Index", this.FetchData()); } @@ -52,23 +48,23 @@ namespace StardewModdingAPI.Web.Controllers ** Private methods *********/ /// <summary>Asynchronously fetch mod metadata from the wiki.</summary> - public async Task<ModListModel> FetchDataAsync() + public ModListModel FetchData() { - return await this.Cache.GetOrCreateAsync($"{nameof(ModsController)}_mod_list", async entry => - { - WikiModList data = await new ModToolkit().GetWikiCompatibilityListAsync(); - ModListModel model = new ModListModel( - stableVersion: data.StableVersion, - betaVersion: data.BetaVersion, - mods: data - .Mods - .Select(mod => new ModModel(mod)) - .OrderBy(p => Regex.Replace(p.Name.ToLower(), "[^a-z0-9]", "")) // ignore case, spaces, and special characters when sorting - ); + // fetch cached data + if (!this.Cache.TryGetWikiMetadata(out CachedWikiMetadata metadata)) + return new ModListModel(); - entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(this.CacheMinutes); - return model; - }); + // build model + return new ModListModel( + stableVersion: metadata.StableVersion, + betaVersion: metadata.BetaVersion, + mods: this.Cache + .GetWikiMods() + .Select(mod => new ModModel(mod.GetModel())) + .OrderBy(p => Regex.Replace(p.Name.ToLower(), "[^a-z0-9]", "")), // ignore case, spaces, and special characters when sorting + lastUpdated: metadata.LastUpdated, + isStale: this.Cache.IsStale(metadata.LastUpdated, this.StaleMinutes) + ); } } } |