Cache is the "killer" of website development. As a key factor in performance improvement, MVC naturally does not lack this content. However, it directly borrows the page cache mechanism of ASP. NET itself.
Public class OutputCacheAttribute: ActionFilterAttribute
{
Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
...
OutputCachedPage page = new OutputCachedPage (_ cacheSettings );
Page. ProcessRequest (HttpContext. Current );
}
Private sealed class OutputCachedPage: Page
{
...
}
}
We are familiar with the usage and parameters.
[OutputCache (Duration = 10, VaryByParam = "none")]
Public ActionResult Index ()
{
Return View ();
}
However, I personally do not recommend that you write the cache parameters to the Controller cs code, because you have to re-compile the Assembly file when adjusting the cache policy. It is much easier to directly write the parameters in the. aspx file.
<% @ Page Language = "C #" Inherits = "System. Web. Mvc. ViewPage" %>
<% @ OutputCache Duration = "10" VaryByParam = "None" %>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Index </title>
</Head>
<Body>
<% = DateTime. Now %>
</Body>
</Html>
Of course, you can also write the parameters in the configuration file so that you can combine "OutputCacheAttribute" and "Easy to modify.
[OutputCache (CacheProfile = "Index")]
Public ActionResult Index ()
{
Return View ();
}
Web. config
<System. web>
<Caching>
<OutputCacheSettings>
<OutputCacheProfiles>
<Add name = "Index" duration = "10" varyByParam = "none"/>
</OutputCacheProfiles>
</OutputCacheSettings>
</Caching>
</System. web>
We can also implement static HTML-based caching to improve website performance.