. Net petshop 4.0 cache Processing

Source: Internet
Author: User
Code
Caching can be greatly improved for websites with large traffic but few updatesProgramThe operation efficiency provides a good experience for network users. In the classic example project. Net petshop provided by Microsoft 4 In. 0, it also provides support for caching. This article is some of the experiences of the author while learning about this project. It is not very clear in some places, so I hope I can give a reference to it.

In. Net petshop4In. 0, the factory mode and interface (Interface), Static class (staticClass), Abstract class (Abstract Class. When cache is used, it is also configured through web. config, which is flexible in use. The following is a detailed analysis of. Net petshop from the bottom up4. 0 cache technology.

First, let's take a look at the namespace directly related to the cache in this project:

Petshop. icachedependency
Petshop. tablecachedependency
Petshop. cachedependencyfactory
Petshop. Web

1. petshop. icachedependency namespace

The lowest layer should be the interface definition. In the petshop. icachedependency namespace, only one interface ipetshopcachedependency is defined. This interface has only one method, getdependency, and no parameter. The aggregatecachedependency type is returned. Aggregatecachedependency is in the. NET Framework2.0Is a new class, combining multiple dependencies between the items stored in the cache object of the ASP. NET application and the array of cachedependency objects (in the msdn article ).

Ii. petshop. tablecachedependency namespace

The petshop. tablecachedependency namespace provides two types: abstract class tabledependency and Its Inheritance class category, item, and product. The constructor of the abstract class tabledependency is:

ProtectedTabledependency (StringConfigkey ){

string dbname = configurationmanager. appsettings [ " cachedatabasename " ];
string tableconfig = configurationmanager. appsettings [configkey];
string [] tables = tableconfig. split (configurationseparator);

foreach ( string tablename in tables)
dependency. add ( New sqlcachedependency (dbname, tablename);
}

A parameter configkey is passed. Based on this parameter, the table name list is obtained from the Web. config file, and the database name is obtained from web. config. Add all data tables in the table name list to the dependency variable of the aggregatecachedependency type. In addition, the. NET Framework2.0Version is another new cache-related sqlcachedependency class. This class is used to establish the connection between items stored in the cache object of ASP. NET applications and specific SQL Server database tables. Both aggregatecachedependency and sqlcachedependency are inherited from cachedependency, but in. net2. 0 does not provide classes for other databases such as oracle.

The following are cache-related settings in the web. config file:

<! -- Cache dependency options. Possible values: petshop. tablecachedependency For SQL Server and keep empty For Oracle -->
< Add key = " Cachedependencyassembly " Value = " Petshop. tablecachedependency " />
<! -- Cachedatabasename shocould match the name under caching section, when Using Tablecachedependency -->
< Add key = " Cachedatabasename " Value = " Mspetshop4 " />
<! --   * Tabledependency lists table dependency For Each instance separated by comma -->
< Add key = " Categorytabledependency " Value = " Category " />
< Add key = " Producttabledependency " Value = " Product, category " />
< Add key = " Itemtabledependency " Value = " Product, category, item " />

Each inherited class has only one constructor. The configkey parameter of the base class is changed to three different classes. The constructor of the product class is:

PublicProduct ():Base("Producttabledependency"){}

Iii. petshop. cachedependencyfactory namespace

The petshop. cachedependencyfactory namespace has two classes: dependencyaccess and dependencyfacade. If you understand the hierarchy of some classic examples provided by Microsoft before, it is not difficult to understand the names of these two classes and their corresponding levels. Both classes are static classes. The methods in the dependencyaccess class both return ipetshopcachedependency, and the methods in the dependencyfacade return aggregatecachedependency.

One of the key methods in the dependencyaccess class is loadinstance, which is the specific implementation of the factory mode. ItsCodeAs follows:

private static ipetshopcachedependency loadinstance ( string classname) {

string path = configurationmanager. appsettings [ " cachedependencyassembly " ];
string fullyqualifiedclass = path + " . " + classname;

//Using the evidence given in the config file load the appropriate assembly and class
Return(Ipetshopcachedependency) Assembly. Load (PATH). createinstance (fullyqualifiedclass );
}

In this method, the corresponding set and class are returned through the classname parameter set in the configuration file. The other three methods in the dependencyaccess class call this method and pass in different parameters.

The three methods provided by the dependencyfacade class correspond to the three methods of the dependencyaccess class, respectively obtaining the aggregatecachedependency of the category, item, and product. The dependencyfacade class also reads the cachedependencyassembly settings in Web. config to determine whether to call the method corresponding to dependencyaccess or directly return null.

Iv. petshop. Web namespace

In the app_code of petshop. Web, four static classes are directly related to the cache, namely categorydataproxy, itemdataproxy, productdataproxy, and webutility. The first three invoke the dependencyfacade method. Unfortunately, I haven't fully figured out where or how to use these three classes.

Webutility has two methods, getcategoryname and getproductname, which use cache. The following is the code for getcategoryname:

If (Data = null ) {
// caching duration from web. config
int cacheduration = int . parse (configurationmanager. appsettings [ " 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. nosdomainingexpiration, cacheitempriority. High,Null);
}

In. net2. 0, there are two ways to maintain the cache: the first is to judge whether the cache is stored every time the cache is used, and if it does not exist, read the data into the cache; another method is to use the cacheitemremovedcallback delegate. When the cache fails, the delegate process is automatically called to re-generate the cache. From the code above,. Net petshop4. 0 uses the first method.

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.