ASP.NET Core supports server-side caching in addition to standard HTTP cache headers. In this video, Nate explains how server-side caching works and how to add it to the API.
- [Instructor] There's one other type of caching that ASP.NET Core supports, and that's server side caching. Server side caching means that your server can cache responses that are expensive to generate, but don't change often. This can be one more useful way to reduce load on your server and improve response time. To enable server side caching, we'll need to add a NuGet package to the project. Using the NuGet package manager, I'm going to search for aspnetcore.responsecaching. And install the Response Caching package.
The Response Caching package contains a middleware component that you can add to your application pipeline. We'll do that in Startup.cs. First we need to add the services to ConfigureServices. So anywhere inside of ConfigureServices we can say services.AddResponseCaching. Then in the configure method, right above app.UseMvc, we need to add app.UseResponseCaching. The order is important here in the configure method, because we want the Response Caching middleware to handle requests before they get to Mvc.
The Response Caching middleware uses the same Response Cache attribute we used before to add the cache control headers for client side caching. Let's try putting the Response Cache attribute on a big collection response that take some time to generate. I'll use the RoomsController GetAllRoomOpeningsAsync method. We can add ResponseCache and a duration of maybe 30 seconds. This will tell the client that the response from this route can be cached, and it will now also cache this response on the server for 30 seconds.
If an identical request comes into this route, no matter which client it comes from, it will immediately be returned from the cache instead of regenerated. To make it even smarter, we can give ASP.NetCore a hint as to which query stream parameters will cause the response to change or vary. In this case, the response will be affected if the user passes the offset or limit parameters, or order by or search parameters. So we can add the VaryByQueryKeys property and pass a string array of offset, limit, orderBy, and search.
Alright, let's test this in Postman. First I'll make a request to get the rooms route. We haven't added any caching here. We did add it to the /rooms/openings route. Let's hit this once and see what the response time is. About 200 milliseconds, almost 300 milliseconds. Let's hit it again and see how much it'll improve with the cache, and it improves somewhat. There's always some variation in how long the response will take when you send a request. But the caching is working here.
When you're adding caching, it's important not to cache so aggressively that clients won't be able to get new information. In this case, it's probably unlikely that the current openings at the hotel will change more often than every 30 seconds. If it's implemented with some planning, caching can greatly improve the performance of your API. That wraps up our look at using compression, client side caching, and server side caching to improve the performance of the API. You should now have a good understanding of how caching and compression work and how they can be utilized in your own project.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Deploying ASP.NET Core Applicationswith Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.