diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 20:46:38 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 20:46:38 -0400 |
commit | ba5cc149e265d3f14246db23b09c8feb5f9c0d3a (patch) | |
tree | 41f30948def325b50591d58779c531e6b33a6533 /src | |
parent | e4a2f555170b08263ae2d7e713808bd229b48947 (diff) | |
download | SMAPI-ba5cc149e265d3f14246db23b09c8feb5f9c0d3a.tar.gz SMAPI-ba5cc149e265d3f14246db23b09c8feb5f9c0d3a.tar.bz2 SMAPI-ba5cc149e265d3f14246db23b09c8feb5f9c0d3a.zip |
add in-memory cache for remote queries (#336)
Diffstat (limited to 'src')
-rw-r--r-- | src/StardewModdingAPI.Web/Controllers/ModsController.cs | 17 | ||||
-rw-r--r-- | src/StardewModdingAPI.Web/Startup.cs | 1 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs index 366201e3..bd5ecef9 100644 --- a/src/StardewModdingAPI.Web/Controllers/ModsController.cs +++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; using StardewModdingAPI.Web.Framework.ModRepositories; using StardewModdingAPI.Web.Models; @@ -24,10 +25,20 @@ namespace StardewModdingAPI.Web.Controllers } .ToDictionary(p => p.VendorKey, StringComparer.CurrentCultureIgnoreCase); + /// <summary>The cache in which to store mod metadata.</summary> + private readonly IMemoryCache Cache; + /********* ** Public methods *********/ + /// <summary>Construct an instance.</summary> + /// <param name="cache">The cache in which to store mod metadata.</param> + public ModsController(IMemoryCache cache) + { + this.Cache = cache; + } + /// <summary>Fetch version metadata for the given mods.</summary> /// <param name="modKeys">The namespaced mod keys to search as a comma-delimited array.</param> [HttpGet] @@ -69,7 +80,11 @@ namespace StardewModdingAPI.Web.Controllers } // fetch mod info - result[modKey] = await repository.GetModInfoAsync(modID); + result[modKey] = await this.Cache.GetOrCreateAsync($"{repository.VendorKey}:{modID}".ToLower(), async entry => + { + entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddHours(1); + return await repository.GetModInfoAsync(modID); + }); } return result; diff --git a/src/StardewModdingAPI.Web/Startup.cs b/src/StardewModdingAPI.Web/Startup.cs index 19dffb88..8aba6a5a 100644 --- a/src/StardewModdingAPI.Web/Startup.cs +++ b/src/StardewModdingAPI.Web/Startup.cs @@ -39,6 +39,7 @@ namespace StardewModdingAPI.Web public void ConfigureServices(IServiceCollection services) { services + .AddMemoryCache() .AddMvc() .AddJsonOptions(options => { |