Programming Microsoft ASP.NET 4 - Dino Esposito [402]
[PartialCaching(60)]
public partial class CustomersGrid : UserControl {
...
}
The PartialCaching attribute allows you to specify the duration and values for the VaryByParam, VaryByControl, and VaryByCustom parameters.
Vary by Controls
The VaryByControl attribute allows you to vary the cache for each specified control property. For user controls, the property is mandatory unless the VaryByParam attribute has been specified. You can indicate both VaryByParam and VaryByControl, but at least one of them is required.
The following user control displays a grid with all the customers in a given country/region. The country/region is specified by the user control’s Country property.
<%@ Control Language="C#" CodeFile="CustomersGrid.ascx.cs"
Inherits="CustomersGridByCtl" %>
<%@ OutputCache Duration="30" VaryByControl="Country" %>
<%= DateTime.Now.ToString() %>
SelectMethod="LoadByCountry">
Here is the code file of the user control:
public partial class CustomersGridByCtl : System.Web.UI.UserControl
{
public String Country;
protected void Page_Load(Object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Country))
{
ObjectDataSource1.SelectParameters.Add("country", Country);
GridView1.DataSourceID = "ObjectDataSource1";
}
}
}
The @OutputCache directive caches a different copy of the user control output based on the different values of the Country property. Figure 18-9 shows it in action.
Figure 18-9. Two pages created at different moments use the same user control output, as you can see from the creation time of the grid.
The strings you assign to VaryByControl can be properties of the user controls as well as ID property values for contained controls. In this case, you’ll get a distinct cached copy for each distinct combination of property values on the specified control.
The Shared Attribute
In Figure 18-9, you see two instances of the same page sharing the cached output of a user control. Try the following simple experiment. Make a plain copy of the page (say, page1.aspx), and give it another name (say, page2.aspx). You should have two distinct pages that generate identical output. In particular, both pages contain an instance of the same cacheable user control. Let’s say that the cache duration of the user control is 30 seconds.
As the next step of the experiment, you open both pages at different times while the cache is still valid. Let’s say you open the second page ten seconds later than the first. Interestingly enough, the two pages no longer share the same copy of user control output, as Figure 18-10 documents.
Figure 18-10. Distinct pages don’t automatically share the output of the same user control.
By default, distinct pages don’t share the output of the same cacheable user control. Each page will maintain its own copy of the user control response, instead. Implemented to guarantee total separation and avoid any sort of conflicts, this feature is far more dangerous than one might think at first. It might lead to flooding the Web server memory with copies and copies of the user control responses—one for each varying parameter or control property and one set for each page that uses the control.
To allow distinct pages to share the same output of a common user control, you need to set the Shared attribute to true in the user control’s @OutputCache directive:
<%@ OutputCache Duration="30" VaryByParam="None" Shared="true" %>
Fragment Caching in Cacheable