Implementation of ASP. NET cache in petshop 4.0: "1 』

Source: Internet
Author: User

As a B2C pet online store, petshop needs to fully consider the visitor's user experience. If the response of the Web server is not timely due to a large amount of data, and the page and query data are delayed, this will damage the customer's mood to visit the website, and may lose this part of the customer after the patient wait. Undoubtedly, this is a very bad result. Therefore, when designing the system architecture, the performance of the entire system is very important. However, we cannot ignore data correctness because we focus on performance. In petshop 3.0 and earlier versions, this problem has not been well solved due to the limitations of ASP. NET cache. Petshop 4.0 introduces the sqlcachedependency feature, which greatly improves the system's processing of the cache.

4.3.1 cachedependency Interface

Petshop 4.0 introduces the sqlcachedependency feature and implements the SQL cache invalidation Technology for the cache corresponding to the category, product, and item data tables. When the corresponding data table changes, this technology can remove related items from the cache. The core of this technology is the sqlcachedependency class, which inherits the cachedependency class. However, to ensure the scalability of the entire architecture, we also allow designers to establish a custom cachedependency class to extend cache dependencies. Therefore, it is necessary to create an abstract interface for cachedependency and configure it in the web. config file.

In the namespace petshop. icachedependency of petshop 4.0, The ipetshopcachedependency interface is defined. It only contains one interface method:
Public interface ipetshopcachedependency
{
Aggregatecachedependency getdependency ();
}

Aggregatecachedependency is a new class added by. NET Framework 2.0. It monitors the collection of dependency objects. When any dependency object in this set changes, the cache object corresponding to the dependency object will be automatically removed.
The aggregatecachedependency class combines cachedependency objects. It can associate multiple cachedependency objects or even different types of cachedependency objects with cache items. Because petshop needs to create dependencies for the category, product, and item data tables, the ipetshopcachedependency interface method getdependency () is to return the aggregatecachedependency object with these dependencies established.

4.3.2 cachedependency implementation

The implementation of cachedependency is to create a corresponding sqlcachedependency type dependency for the category, product, and item data tables, as shown in the Code:
Public abstract class tabledependency: ipetshopcachedependency
{
// This is the separator that's used in Web. config
Protected char [] configurationseparator = new char [] {','};

Protected aggregatecachedependency dependency = new aggregatecachedependency ();
Protected tabledependency (string configkey)
{
String dbname = configurationmanager. receivettings ["cachedatabasename"];
String tableconfig = configurationmanager. configurettings [configkey];
String [] tables = tableconfig. Split (configurationseparator );

Foreach (string tablename in tables)
Dependency. Add (New sqlcachedependency (dbname, tablename ));
}
Public aggregatecachedependency getdependency ()
{
Return dependency;
}
}

The databases and data tables for which dependencies need to be created are configured in the web. config file. The settings are as follows:
<Add key = "cachedatabasename" value = "mspetshop4"/>
<Add key = "categorytabledependency" value = "category"/>
<Add key = "producttabledependency" value = "product, category"/>
<Add key = "itemtabledependency" value = "product, category, item"/>

Depending on the dependencies between data tables, the dependencies required for different data tables are also different. The value in the configuration file can be seen. However, no matter how many dependencies are created, the created behavior logic is similar. Therefore, a common class tabledependency is abstracted during design and a constructor with parameters is created, establish the dependency. In the implementation of the interface method getdependency (), the returned object dependency is created in a protected constructor. Therefore, the implementation method here can be seen as the flexible use of the template method mode. For example, the child product of tabledependency builds the sqlcachedependency dependency of the product and category data tables by using the constructor of the parent class:
Public class product: tabledependency
{
Public Product (): Base ("producttabledependency "){}
}

If you need to customize cachedependency, you can create dependencies in different ways. However, whether you create a sqlcachedependency object or a custom cachedependency object, you can add these dependencies to the aggregatecachedependency class. Therefore, you can also create a special class for the custom cachedependency, you only need to implement the ipetshopcachedependency interface.

4.3.3 cachedependency Factory

The product, category, and item classes that inherit the abstract class tabledependency must create their own objects during the call. Because their parent class tabledependency implements the ipetshopcachedependency interface, they also indirectly implement the ipetshopcachedependency interface, which provides a prerequisite for implementing the factory mode.

In petshop 4.0, the configuration file and reflection technology are still used to implement the factory model. In the namespace petshop. cachedependencyfactory, dependencyaccess is the factory class for creating ipetshopcachedependency objects:
Public static class dependencyaccess
{
Public static ipetshopcachedependency createcategorydependency ()
{
Return loadinstance ("category ");
}
Public static ipetshopcachedependency createproductdependency ()
{
Return loadinstance ("product ");
}
Public static ipetshopcachedependency createitemdependency ()
{
Return loadinstance ("item ");
}
Private Static ipetshopcachedependency loadinstance (string classname)
{
String Path = configurationmanager. etettings ["cachedependencyassembly"];
String fullyqualifiedclass = path + "." + classname;
Return (ipetshopcachedependency) Assembly. Load (PATH). createinstance (fullyqualifiedclass );
}
}
The implementation of the entire factory model is 4-3:


Figure 4-3 cachedependency Factory

Although the dependencyaccess class creates categories, products, and items that implement the ipetshopcachedependency interface, the purpose of introducing the ipetshopcachedependency interface is to obtain the aggregatecachedependency type object of the dependency item. We can call the object's interface method getdependency (), as shown below:
Aggregatecachedependency dependency = dependencyaccess. createcategorydependency (). getdependency ();

To facilitate callers, it seems that we can improve the dependencyaccess class and change the original createcategorydependency () method to the method for creating an aggregatecachedependency object.

However, this method disrupts the dependencyaccess responsibilities of the factory class, and the behavior of creating an ipetshopcachedependency interface object may still be called by the caller. Therefore, it is necessary to retain the original dependencyaccess class.

In the design of petshop 4.0, the facade mode is introduced to facilitate callers to obtain aggregatecachedependency type objects more easily.

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.