Control cache and data cache in petshop

Source: Internet
Author: User

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;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.