Cache handling for. Net PetShop 4.0

Source: Internet
Author: User
Tags abstract config constructor static class table name

In a very large number of visits, but less updated web sites using caching, can greatly improve the efficiency of the program operation, to the network users a good experience effect. In the Classic sample project. Net PetShop 4.0, which is provided by Microsoft, also provides the support for caching, this article is the author in learning this project some experience, some places are not very clear, hope to be able to give a comment.

In. Net PetShop 4.0, members of the factory pattern as well as interfaces (interface), static classes (static Class), Abstract classes (Abstracts Class) were very successful. When using caching, it is also set through the Web.config configuration and is flexible when used. Below is a detailed analysis of the. Net PetShop 4.0 cache technology from the bottom up.

First look at the namespaces that are directly related to caching in this project:

PetShop.ICacheDependency
PetShop.TableCacheDependency
PetShop.CacheDependencyFactory
PetShop.Web

First, petshop.icachedependency namespaces

The lowest layer should be the definition of the interface, in the Petshop.icachedependency namespace only defines an interface ipetshopcachedependency, the interface has only one method getdependency, without any parameters, Returns the AggregateCacheDependency type. AggregateCacheDependency is a new class in the. NET Framework version 2.0, combining multiple dependencies between the items stored in the Cache object of the ASP.net application and the array of CacheDependency objects ( MSDN Central dialect).

Second, petshop.tablecachedependency namespaces

In the Petshop.tablecachedependency namespace, provide two kinds: abstract class tabledependency and its inheriting class category, item, and product. The constructor for the abstract class tabledependency is:

Protected tabledependency (string configkey) {
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, from which the table name list is fetched from the Web.config file, and the database names are obtained in web.config. Adds all the data tables in the table name list to the dependency variable of the aggregatecachedependency type. In addition, the. NET Framework version 2.0 is another new cache-related SqlCacheDependency class. This class is used to establish the connection between the items stored in the cache object of the ASP.net application and the tables of a particular SQL Server database. AggregateCacheDependency and SqlCacheDependency are inherited from CacheDependency, but there are no classes in. NET 2.0 that correspond to 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 should 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 inheriting class has only one constructor, which is changed into three different classes by setting the Configkey parameter of the base class. The constructor of the product class is:

public Product() : base("ProductTableDependency") { }

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.