add in-memory caching to API & Web, add response caching to Web

This commit is contained in:
Collin M. Barrett 2017-11-02 12:21:27 -05:00
parent 8105e4d89e
commit 344b97e9a3
11 changed files with 81 additions and 51 deletions

View file

@ -0,0 +1,9 @@
// https://github.com/aspnet/Docs/blob/master/aspnetcore/performance/caching/memory/sample/WebCache/CacheKeys.cs
namespace FilterLists.Api
{
public static class CacheKeys
{
public static readonly string Entry = "_Entry";
}
}

View file

@ -13,6 +13,7 @@ public static void AddFilterListsApi(this IServiceCollection services)
services.AddMvcCustom();
services.AddApiVersioning();
services.AddResponseCaching();
services.AddMemoryCache();
services.AddSwaggerGenCustom();
}

View file

@ -26,6 +26,7 @@
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.0.0" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.1.2-alpha" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
</ItemGroup>

View file

@ -28,19 +28,18 @@ public void Configure(IApplicationBuilder app)
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseResponseCaching();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/v1/swagger.json", "FilterLists API V1");
c.RoutePrefix = "docs";
});
app.UseStaticFiles();
app.UseMvc(routes => { routes.MapRoute("default", "v{version:apiVersion}/{controller=Default}/{action=Get}/{id?}"); });
app.UseMvc(routes =>
{
routes.MapRoute("default", "v{version:apiVersion}/{controller=Default}/{action=Get}/{id?}");
});
}
}
}

View file

@ -1,5 +1,7 @@
using FilterLists.Services.Contracts;
using System;
using FilterLists.Services.Contracts;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
@ -9,16 +11,22 @@ namespace FilterLists.Api.V1.Controllers
public class ListsController : Controller
{
private readonly IFilterListService filterListService;
private readonly IMemoryCache memoryCache;
public ListsController(IFilterListService filterListService)
public ListsController(IMemoryCache memoryCache, IFilterListService filterListService)
{
this.memoryCache = memoryCache;
this.filterListService = filterListService;
}
[HttpGet]
public IActionResult Get()
{
return Json(filterListService.GetAllSummaries());
return memoryCache.GetOrCreate(CacheKeys.Entry, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
return Json(filterListService.GetAllSummaries());
});
}
}
}

View file

@ -0,0 +1,9 @@
// https://github.com/aspnet/Docs/blob/master/aspnetcore/performance/caching/memory/sample/WebCache/CacheKeys.cs
namespace FilterLists.Web
{
public static class CacheKeys
{
public static readonly string Entry = "_Entry";
}
}

View file

@ -1,13 +1,27 @@
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Web.Controllers
{
[ResponseCache(CacheProfileName = "Long-Lived")]
public class HomeController : Controller
{
private readonly IMemoryCache memoryCache;
public HomeController(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
public IActionResult Index()
{
return View();
return memoryCache.GetOrCreate(CacheKeys.Entry, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
return View();
});
}
public IActionResult Error()

View file

@ -1,37 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
namespace FilterLists.Web.Controllers
{
[Route("api/[controller]")]
public class SampleDataController : Controller
{
private static readonly string[] Summaries =
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet("[action]")]
public IEnumerable<WeatherForecast> WeatherForecasts()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
});
}
public class WeatherForecast
{
public string DateFormatted { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
public int TemperatureF => 32 + (int) (TemperatureC / 0.5556);
}
}
}

View file

@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace FilterLists.Web.DependencyInjection.Extensions
{
public static class ConfigureServicesCollection
{
public static void AddFilterListsWeb(this IServiceCollection services)
{
services.AddMvcCustom();
services.AddResponseCaching();
services.AddMemoryCache();
}
private static void AddMvcCustom(this IServiceCollection services)
{
services.AddMvc(options =>
{
options.CacheProfiles.Add("Long-Lived",
new CacheProfile
{
Duration = 86400
});
});
}
}
}

View file

@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.1.2-alpha" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
</ItemGroup>
<ItemGroup>

View file

@ -1,3 +1,4 @@
using FilterLists.Web.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
@ -17,7 +18,7 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddFilterListsWeb();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@ -32,12 +33,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseResponseCaching();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(