ASP.NET Caching
.NET caching could be the key to the success of your next project. There are several fascinating aspects to caching. You may be aware of caching, but you maynot realize how much performance gain it can provide your website. In this post I will explain the three caching techniques I used to load an experimental pageten times faster:
- Output caching:A simple tactic which caches all output for a page.
- Fragment caching:A more precise approach and works great with server or user controls. Also known as partial caching.
- Cache substitution:Allows a portion of a cached page to display dynamic behavior.
Output Caching
@OutputCache is set as a page directive, similar to @Register or @Page directives. You can apply the @OutputCache directive to a Page or a UserControl. All outputis then cached depending on the parameters listed.
- Duratio:Time in seconds the cache will persist until the page code runs again.
- Shared:A flag to determine if the output of a UserControl can be shared by parent pages. This parameter is not valid for a Page that implements@OutputCache.
- VaryByParam:One of the many VaryBy… attributes. A different cache copy will be saved per the query string parameter(s) specified.
The above will get you through most situations. Here is what a common @OutputCache directive looks like:
<%@ OutputCache Duration=”2592000″ VaryByParam=”DN;page” %>
Consider the URL http://yourdomain.com/default.aspx?DN=true&page=15.
The directive caches this output for 15 minutes. A different cache copy will be kept for each variation of the “DN” and “page” query string parameters. Yes, that means if “DN” has 1,000 variations, there will be 1,000 different cached versions of this output. The cache number exponentially increases with the various combinations of “DN” and “page” values.
Fragment Caching
The PartialCaching characteristic is similar to the @OutputCache directive. It is typically the practice of caching output for a UserControl, which is only a fragment of the entire output. You can apply @OutputCache to the UserControl or you can add the PartialCaching attribute to the code-behind. The nice thing about the PartialCaching attribute is that you can apply it to any class – even server controls. Here is an example of how to add PartialCaching:
<PartialCaching(2592000, “DN”, Nothing, Nothing, True)> _
Public Partial Class myCacheClass
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(sender As Object, e As EventArgs)
‘ Page code here
End Sub
End Class
<partialcaching(2592000,>
Here we have a class called “myCacheClass” which inherits from the UserControl class. Directly above the class declaration we have a PartialCachingAttribute called PartialCaching(). Here are the parameters for the PartialCachingAttribute above:
- Duration: Set to 30 days.
- VaryByParams: Set to look for the “DN” variable in the URL
- VaryByControls: Nothing, since there are no Controls which are to be cached.
- VaryByCustom: Nothing, since there are no Custom objects considered.
- Shared: True (this User Control output can be shared by more than one page).
One of the real benefits of using the PartialCachingAttribute is that you can apply it to server controls. In the event that you are using cache key dependencies, it may be beneficial to keep your caching code in the code-behind.
Cache Substitution
Cache Substitution is the process of outputting dynamic content in a cached situation. This could come in handy if you want to show a user’s membership name, for example, yet have the rest of the page cached. Below is a quick example of how to implement cache substitution to output the current date and time:
.aspx page:
<%@ OutputCache Duration=”900″ VaryByParam=”none” %>
<asp:Substitution runat=”server” MethodName=”OutputTime” />
Code-behind:
Protected Shared Function OutputTime(context As HttpContext) As String
Return DateTime.Now.ToString()
End Function
When you refresh the page you will see that even though the output is cached, your substitution control will always have the current date and time.