This article is currently in the process of being translated into Vietnamese (~20% done).
OutputCache
Trong ASP.NET WebForms, cũng như trong ASP.NET MVC trước đây thì khái niệm OutputCache đã tồn tại. Nó giúp cho việc cache toàn bộ trang/View giống như ResponseCache ở bài trước, nhưng khác là lưu trong bộ nhớ của webserver.
Khái niệm OutputCache không phải cho .Net core framework nhưng rất may cho chúng ta, cộng đồng đã phát triển phiên bản dựa trên in-memory caching giống OutputCache. Thực tế, tôi sẽ dựa trên WebEssentials.AspNetCore.OutputCaching NuGet package.
Khi bạn cài đặt package này xong thì bạn chỉ cần thay đổi một chút trong file Startup.cs để dùng OutputCache:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOutputCaching();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseOutputCaching();
app.UseMvcWithDefaultRoute();
}
}
The "magic" happens in the call to services.AddOutputCaching() and app.UseOutputCaching(). Now your website supports output caching and you can start using it like this:
[OutputCache(Duration = 120)]
public IActionResult Index()
{
...
Now, your page is cached in the memory of the webserver for 120 seconds and will only be generated on the first request (and then again when the cache has expired). If you want to verify this, you can either start debugging the project after setting a breakpoint inside the action/method, or you can pass in a DateTime instance and then output it in the View like this:
Controller:
[OutputCache(Duration = 120)]
public IActionResult Index()
{
return View(DateTime.Now);
}
View:
@model DateTime
Time of request: @Model.ToString()
Try requesting the page and notice that the date and time remains the same, even when reloading the page, until the cache has expired.
OutputCache Options
We already used a very important option when using the OutputCache: The Duration. It will tell the framework how long it can rely on the cached version, before it needs to be refreshed. There are several other relevant options though:
VaryByParam
Without any further options than the Duration, the request will be cached no matter what and served to all other users, but in many situations, your page might change depending on how the user accesses it. For instance, you might use parameters in the query string to serve different content based on how the user requests it, like this:
/Users/Details?id=1
/Users/Details?id=2
....
In a situation like that, you want the caching to take the parameter of the name "id" into consideration when caching the content. This is easily accomplished by using the VaryByParam option:
[OutputCache(Duration = 120, VaryByParam = "id")]
public IActionResult Details(int id)
{
return View(id);
}
Multiple versions of the Details View can now exist in the cache, depending on which value was passed to the id parameter. You can specify multiple parameter names, like this:
[OutputCache(Duration = 120, VaryByParam = "id,page,time")]
public IActionResult Details(int id)
{
...
VaryByHeader
You can also vary the content based on headers sent to the server from the client, e.g. "User-Agent", which identifies a client (a webbrowser). It can be used like this:
[OutputCache(Duration = 120, VaryByHeader = "User-Agent")]
public IActionResult Index()
{
return View(DateTime.Now);
}
This will vary the content based on which browser is used, and there are of course many other types of headers that you can use - for inspiration, have a look at this list of HTTP header fields.
Summary
Thanks to the OutputCaching NuGet package, you can have the same type of OutputCache functionality as previously found in ASP.NET WebForms and earlier versions of the MVC framework.