Aggregatecachedependency, cachedependency, sqlcachedependency Asp.net 2.0 and SQL Server Cache Management and Use objectbuil

Source: Internet
Author: User
In the past two days, petshop is edevil? I have made a lot of discussions on petshop, and I am also making a discussion here. I will give you an understanding of the new cache management content in Asp.net 2.0 and the cache processing in petshop 4. Let's take a look at it and exchange ideas to make progress.

New Cache Management in Asp.net 2.0
CachedependencyTracks cache dependencies. cache dependencies can be files, directories, or keys with other objects in the application's cache.

SqlcachedependencyMonitors specific SQL Server database tables on all supported SQL Server versions (7.0, 2000,200 5) so that when the table is changed, items associated with the table are automatically deleted from the cache. When the database table is changed, the cache items are automatically deleted and new versions are added to the cache. When using SQL Server 2005 databases, the sqlcachedependency class also supports integration with the system. Data. sqlclient. sqldependency class. Use the query notification mechanism of SQL Server 2005 to detect data changes that make the SQL query results invalid. Any cache items associated with SQL queries will be removed from system. Web. caching. cache. When using SQL Server 2005, you can use the sqlcachedependency class to add items that depend on SQL Server database tables or SQL queries to the application cache.

AggregatecachedependencyClass monitors the collection of dependency objects, so that the cache will be automatically removed when any dependency object changes. The object in the array can beCachedependencyObject,SqlcachedependencyObject, slaveCachedependencyA derived custom object or any combination of these objects.

AggregatecachedependencyClass andCachedependencyThe difference between classes is that the former allows you to associate multiple dependencies of different types with a single cache item. For example, if you create a page to import data from SQL Server database tables and XML files, you can createSqlcachedependencyObject To indicate the dependencies of the database table, andCachedependency. Can be createdAggregatecachedependencyInstead of calling the cache. insert method for each dependency. Then, you can use a single insert call to make the page dependent onAggregatecachedependencyInstance.

ASP. NET 2.0 allows you to use the sqlcachedependency class to create cache items that depend on tables or rows in the database. When changes occur in a table or a specific row, the items with dependencies become invalid and will be removed from the cache. You can set table dependencies in Microsoft SQL Server 7.0, SQL Server 2000, and SQL Server 2005. If you use SQL Server 2005, you can also set dependencies for specific records.

ASP. NET 2.0 SQL cache dependencies provide the following functions:

1. SQL cache dependencies can be used for application cache and page output cache.

2. You can use SQL cache dependencies in SQL Server 7.0 and later.

3. You can use SQL cache dependencies in the network Park (multiple processors exist on one server) or network farm (multiple servers run the same application.

4. database operations associated with SQL cache dependencies are relatively simple, so there is no high processing cost for the server.

ASP. NET 2.0 provides a round-robin model for cache dependencies of SQL Server 7.0 and SQL Server 2000. A thread in the ASP. Net Process polls the SQL Server database at a specified interval to determine whether the data has been changed. If the data has been changed, the cache dependency will expire and be removed from the cache. You can specify the polling interval in the application in declarative form in the web. config file, or you can useSqlcachedependencySpecifies the interval programmatically.

For SQL Server 7.0 and SQL Server 2000, the SQL cache dependency is limited to table-level data changes. You can configure ASP. NET as a poll database to determine the changes in the table, but cannot determine the changes in a specific row.

Enable SQL Cache

To use SQL cache dependencies in SQL Server 7.0 and SQL Server 2000, you must first configure SQL Server to support cache dependencies. ASP. NET provides some practical tools for configuring SQL cache on SQL Server, including a tool named aspnet_regsql.exe andSqlcachedependencyadminClass.

The cache dependency model of SQL Server 2005 is different from that of SQL Server 7.0 and SQL Server 2000. In SQL Server 2005, you do not need to perform any special configuration steps to enable SQL cache dependencies. In addition, SQL Server 2005 implements a change notification model, which can send notifications to the Application Server subscribed to the notification, rather than relying on the necessary Polling Model in earlier versions of SQL Server.

SQL Server 2005 cache dependencies are more flexible in receiving notification changes. SQL Server 2005 monitors changes to the result set of a specific SQL command. If the database changes the result set that will modify the command, the dependency will invalidate the cached items. This feature enables SQL Server 2005 to provide row-level notifications.

 

Cache Management in MS petshop4.0

In petshop, You need to implement cache for data tables. These caches are stored in the. NET cache collection object system. Web. caching. aggregatecachedependency provided by the web system. The Code marked in red in the following code is the code of aggregatecachedependency Cache Management.

/// <Summary>

/// This is the base class for sql2kcachedependency implementation that encapsulates common

/// Algorithm to retrieve database and table names from configuration file and create

/// The necessary aggregatecachedependency object

/// </Summary>

Public abstract class tabledependency: petshop. icachedependency. ipetshopcachedependency {

// This is the separator that's used in Web. config

Protected char [] configurationseparator = new char [] {','};

 

Protected aggregatecachedependency dependency = new aggregatecachedependency ();

 

/// <Summary>

/// The constructor Retrieves all related configuration and add cachedependency object accordingly

/// </Summary>

/// <Param name = "configkey"> Configuration key for specific derived class implementation </param>

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 following figure shows the Cache Management class of petshop 4.


It uses many design modes, such as factory mode and factory mode. However, his design is also very inflexible. For example, to add a cache item, you also need to modify code in many places, such as dependencyaccess. Microsoft has a core component, objectbuilder, in the Enterprise Library, if I didn't go into deep learning half a year ago, I sent an article about Ms IOC container (objectbuilder )? . In the following example, I used objectbuilder for transformation, that is, the IOC method for transformation. Recently, many studies have been conducted on the typical IOC (Castle.
I will not talk about the specific Code. The Code is very simple. There are two basic unit tests. You can download and study the code yourself. If you have any questions, let's talk about it. Below I will compare the configuration file with the petshop4 configuration file.
Petshop4 configuration file
<Deleetask>

<! -- Enable data caching -->
<Add key = "enablecaching" value = "true"/>
<! -- Cache duration (in hours-whole number only) -->
<Add key = "categorycacheduration" value = "12"/>
<Add key = "productcacheduration" value = "12"/>
<Add key = "itemcacheduration" value = "12"/>
<! -- 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"/>
</Appsettings>

Configuration file after transformation
<Configsections>
<Section name = "aspcacheconfiguration" type = "aspcachedependency. configuration. aspcachedependencysettings, aspcachedependency"/>
</Configsections>

<Aspcacheconfiguration>
<Aspcacheproviders>
<Add name = "product" type = "aspcachedependency. aspsqlcachedependency, aspcachedependency" database = "mspetshop4" table = "product, category"/>
<Add name = "category" type = "aspcachedependency. aspsqlcachedependency, aspcachedependency" database = "mspetshop4" table = "category"/>
</Aspcacheproviders>
</Aspcacheconfiguration>
The modified cache is very simple to use, and can be applied to any project, instead of specific project-related. For example, the unit test code below:
[Testmethod]
Public void getaspsqlcachedependencycategory ()
{
Aspsqlcachedependency sqlcacheldep = aspcachedependencyfactory. createdependency ("category ");
Assert. areequal (sqlcacheldep. Table, "category ");
Aggregatecachedependency dependency = sqlcacheldep. getdependency ();
Assert. isnotnull (dependency );
}

Download the code: asplcachedependency.rar

 

Related Article

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.