Click here to Skip to main content
15,893,814 members
Articles / Web Development / ASP.NET / ASP.NET Core

ASP.NET Core 2.0 Response Caching

Rate me:
Please Sign up or sign in to vote.
1.86/5 (3 votes)
5 Sep 2017CPOL1 min read 6.3K   1  
How to cache responses in ASP.NET Core. Continue reading...

Problem

How to cache responses in ASP.NET Core.

Solution

To an empty project, configure services and middleware for caching in Startup:

C#
public void ConfigureServices(
            IServiceCollection services)
        {
            services.AddResponseCaching();

            services.AddMvc();
        }

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            app.UseResponseCaching();

            app.UseMvcWithDefaultRoute();
        }

Create a controller, annotate with [ResponseCache] attribute:

C#
[ResponseCache(Duration = 60)]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View(new HomeOutputModel
            {
                LastUpdated = DateTime.Now
            });
        }
    }

The response will contain a Cache-Control header:

Response Caching - header

As you navigate between a controller with caching enabled (Home) and another one without it (Movies), you’ll notice that time isn’t being updated, i.e., not coming from server but rather from cache:

Response Caching - output

Discussion

Response Caching middleware is responsible for storing and serving responses from a cache. It adds cache related headers to HTTP responses, primary one being Cache-Control.

Once middleware is setup, [ResponseCache] attribute is applied on controller/action to enable caching. The attribute has some useful parameters to alter the behaviour of middleware:

  • Duration: Used to set cache expiry (in seconds)
  • Location: Translates into Cache-Control header of public, private or no-cache
  • VaryByQueryKeys: Responses are cached based on query string parameters
  • VaryByHeader: Used to set Vary HTTP response header
  • CacheProfileName: Can point to cache profile setup in MVC middleware (see below)
C#
services.AddMvc(options =>
            {
                options.CacheProfiles.Add("default", new CacheProfile
                {
                    Duration = 30,
                    Location = ResponseCacheLocation.Any
                });
            });

Logging

You could enable logging to view the workings of the Response Caching middleware. Below is the first request to Home/Index (see highlighted lines), subsequent requests will go through this lifecycle, and serve the page from cache (until its expiry):

Response Caching - logging

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --