Skip to content

Commit

Permalink
ResponseCaching WebAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Jan 19, 2020
1 parent 2bde813 commit b8d3865
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
Expand Up @@ -18,6 +18,7 @@ namespace InfoValutarWebAPI.Controllers
[ApiVersion("1.0")] [ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/rates")] [Route("api/v{version:apiVersion}/rates")]
[ApiController] [ApiController]
[ResponseCache(CacheProfileName = "Default30")]
public class FromDBController : ControllerBase public class FromDBController : ControllerBase
{ {
private readonly IRetrieve retrieve; private readonly IRetrieve retrieve;
Expand Down
Expand Up @@ -30,6 +30,7 @@ public class LastCommitInfo
[ApiController] [ApiController]
[ApiVersion("1.0")] [ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/info")] [Route("api/v{version:apiVersion}/info")]
[ResponseCache(CacheProfileName = "Default1Day")]
public class InfoController public class InfoController
{ {
/// <summary> /// <summary>
Expand Down
Expand Up @@ -14,6 +14,7 @@ namespace InfoValutarWebAPI.Controllers
[ApiVersion("1.0")] [ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/save")] [Route("api/v{version:apiVersion}/save")]
[ApiController] [ApiController]
[ResponseCache(CacheProfileName = "Default30")]
public class LoadSaveDataController : ControllerBase public class LoadSaveDataController : ControllerBase
{ {
/// <summary> /// <summary>
Expand Down
1 change: 1 addition & 0 deletions InfoValutar/InfoValutarWebAPI/Controllers/TodayRates.cs
Expand Up @@ -15,6 +15,7 @@ namespace InfoValutarWebAPI.Controllers
[ApiController] [ApiController]
[ApiVersion( "1.0" )] [ApiVersion( "1.0" )]
[Route("api/v{version:apiVersion}/[controller]/[action]")] [Route("api/v{version:apiVersion}/[controller]/[action]")]
[ResponseCache(CacheProfileName = "Default30")]
public class TodayRates : ControllerBase public class TodayRates : ControllerBase
{ {
private readonly ILogger<TodayRates> _logger; private readonly ILogger<TodayRates> _logger;
Expand Down
17 changes: 15 additions & 2 deletions InfoValutar/InfoValutarWebAPI/Startup.cs
Expand Up @@ -33,6 +33,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddResponseCaching();
services.AddSpaStaticFiles(configuration => services.AddSpaStaticFiles(configuration =>
{ {
configuration.RootPath = "wwwroot"; configuration.RootPath = "wwwroot";
Expand All @@ -49,7 +50,19 @@ public void ConfigureServices(IServiceCollection services)
}); });
}); });
services.AddControllers(); services.AddControllers(c=>
{
c.CacheProfiles.Add("Default30",
new CacheProfile()
{
Duration = 30
});
c.CacheProfiles.Add("Default1Day",
new CacheProfile()
{
Duration = 60*60*24
});
});
services.AddSingleton<LoadExchangeProviders>(new LoadExchangeProviders("plugins")); services.AddSingleton<LoadExchangeProviders>(new LoadExchangeProviders("plugins"));
services.AddSingleton<InMemoryDB>(); services.AddSingleton<InMemoryDB>();


Expand Down Expand Up @@ -108,7 +121,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseStaticFiles(); app.UseStaticFiles();


app.UseRouting(); app.UseRouting();

app.UseResponseCaching();
app.UseAuthorization(); app.UseAuthorization();


app.UseOpenApi(); app.UseOpenApi();
Expand Down

0 comments on commit b8d3865

Please sign in to comment.