Take the navigationcontrol. ascx control in petshop as an example. The following "page" refers to the "navigationcontrol. ascx" control.
Page:
Code
<%@ Outputcache duration="10"Varybyparam="*" %>
The page cache is set. Due to worries about data expiration, the page dependency is set:
Code
This. Cachepolicy. Dependency=Dependencyfacade. getcategorydependency ();
The Round-Robin mechanism is used to regularly check whether the page dependency expires, as shown below:
Code
< Caching >
< Sqlcachedependency Enabled = " True " Polltime = " 1000 " > <! -- 1 second = 1000 ms -->
< Databases >
< Add name = " Petshop " Connectionstringname = " Mssqlconnstring " Polltime = " 1000 " />
</ Databases >
</ Sqlcachedependency >
</ Caching >
Check the database at every polltime to see if the page dependency (table) has data updates. If there are updates, the cache becomes invalid and the data is re-queried.
In the configuration file:
Code
< Add key = " Categorycacheduration " Value = " 12 " />
< Add key = " Productcacheduration " Value = " 12 " />
< Add key = " Itemcacheduration " Value = " 12 " />
The cache expiration time used for data cache. As shown in the following method,
Code
/// <Summary>
/// Method to retrieve and cache category name by its ID
/// </Summary>
/// <Param name = "categoryid"> CATEGORY ID </Param>
/// <Returns> Category name </Returns>
Public Static String Getcategoryname ( String Categoryid ){
category Category = New category ();
If ( ! enablecaching)
return category. getcategory (categoryid ). name;
StringCachekey= String. Format (category_name_key, categoryid );
// Check if the data exists in the data cache
String Data = ( String ) Httpruntime. cache [cachekey]; // 1
If (Data = Null ){
// Caching duration from web. config
Int Cacheduration = Int . Parse (configurationmanager. receivettings [ " Categorycacheduration " ]);
// if the data is not in the cache then fetch the data from the business logic tier
data = category. getcategory (categoryid ). name;
// Create a aggregatecachedependency object from the factory
aggregatecachedependency CD = dependencyfacade. getcategorydependency ();
// store the output in the data cache, and add the necessary aggregatecachedependency object
httpruntime. cache. add (cachekey, Data, CD, datetime. now. addhours (cacheduration), cache. noslidingexpiration, cacheitempriority. high, null ); // 2
}
ReturnData;
}