diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-25 02:18:24 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-25 02:18:24 -0500 |
commit | 3da98ff0a032255d8b567d27554f13abe1dba62e (patch) | |
tree | 6e535aa758ffeaacb5801a5a97fbb64fcd0bfd2b | |
parent | 70cbfa541dade1822394f81bd1d5b6baa18e51e9 (diff) | |
download | SMAPI-3da98ff0a032255d8b567d27554f13abe1dba62e.tar.gz SMAPI-3da98ff0a032255d8b567d27554f13abe1dba62e.tar.bz2 SMAPI-3da98ff0a032255d8b567d27554f13abe1dba62e.zip |
cache release info (#411)
-rw-r--r-- | src/SMAPI.Web/Controllers/IndexController.cs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/SMAPI.Web/Controllers/IndexController.cs b/src/SMAPI.Web/Controllers/IndexController.cs index c2c5f2fe..5d45118f 100644 --- a/src/SMAPI.Web/Controllers/IndexController.cs +++ b/src/SMAPI.Web/Controllers/IndexController.cs @@ -1,6 +1,8 @@ +using System; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.ViewModels; @@ -14,17 +16,25 @@ namespace StardewModdingAPI.Web.Controllers /********* ** Properties *********/ + /// <summary>The cache in which to store release data.</summary> + private readonly IMemoryCache Cache; + /// <summary>The GitHub API client.</summary> private readonly IGitHubClient GitHub; + /// <summary>The cache time for release info.</summary> + private readonly TimeSpan CacheTime = TimeSpan.FromMinutes(5); + /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> + /// <param name="cache">The cache in which to store release data.</param> /// <param name="github">The GitHub API client.</param> - public IndexController(IGitHubClient github) + public IndexController(IMemoryCache cache, IGitHubClient github) { + this.Cache = cache; this.GitHub = github; } @@ -33,7 +43,11 @@ namespace StardewModdingAPI.Web.Controllers public async Task<ViewResult> Index() { // fetch latest SMAPI release - GitRelease release = await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI"); + GitRelease release = await this.Cache.GetOrCreateAsync("latest-smapi-release", async entry => + { + entry.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(this.CacheTime); + return await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI"); + }); string downloadUrl = this.GetMainDownloadUrl(release); string devDownloadUrl = this.GetDevDownloadUrl(release); |