asp.net SqlCacheDependency Caching Technology Overview _ Practical Skills

Source: Internet
Author: User

This paper illustrates the SqlCacheDependency caching technology in asp.net, which is of great practical value for large web programming. Specifically as follows:

The use of caching techniques in Web sites with a large number of visits, but with fewer updates, can greatly improve operational efficiency; With the caching dependency mechanism provided by. NET 2.0, we can easily manage the cache update; The following is My learning experience, I hope to play a role.

To establish a cache dependency, the implementation code is as follows:

/**////<summary> 
///Build cache dependencies 
///</summary> 
///<returns></returns> 
Private AggregateCacheDependency tabledependency () 
{ 
aggregatecachedependency dependency = new AggregateCacheDependency (); 
Dependency. ADD (New SqlCacheDependency ("MSPETSHOP4", "Table name")); 

return dependency; 
} 

A very simple approach, first let's take a look at two new two classes in. NET 2.0:

AggregateCacheDependency in the System.Web.Caching namespace, the AggregateCacheDependency primary role is to combine items stored in the Cache object of the ASP.net application and Multiple dependencies between an array of CacheDependency objects.

SqlCacheDependency also exists in the System.Web.Caching namespace, which 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.

How does SqlCacheDependency establish the connection between the items stored in a cache object and a particular SQL Server database table? A glance at the Web.config configuration file.

<?xml version= "1.0"?> 
<configuration xmlns= "http://schemas.microsoft.com/". netconfiguration/v2.0 "> 
<connectionStrings> 
<add name=" localconnstring "connectionstring=" server= (local); UID=SA;PWD=123456;DATABASE=MSPETSHOP4 "/> 
</connectionStrings> 
<system.web > 
<caching> 
<sqlcachedependency enabled= "true" Polltime= "10000" > 
<databases> 
<add name= "MSPETSHOP4" connectionstringname= "localconnstring" polltime= "10000"/> 
</databases > 
</sqlCacheDependency> 
</caching> 
<compilation debug= "true"/> 
</ System.web> 
</configuration> 

Configuration section <databases><add name= "MSPETSHOP4" connectionstringname= "localconnstring" Polltime= "10000"/></ Database information is configured in Databases>, and the SqlCacheDependency class automatically reads this configuration section information to establish a connection to the database. (note) name= "MSPETSHOP4" must match the database name in the new SqlCacheDependency ("MSPETSHOP4", "Table name"). More configuration information can be viewed (MSDN Help documentation).

To enable the database to support SqlCacheDependency features:

To enable the 7.0 or 2000 version of SQL Server to support the SqlCacheDependency feature, you need to perform related configuration steps on the database server. There are two ways to configure SQL Server:

Use the aspnet_regsql command line tool, or use the SqlCacheDependencyAdmin class.

The Aspnet_regsql tool is in the Windows\microsoft.net\framework\[version folder, and if you want to configure SqlCacheDependency, you need to execute it on a command-line basis.

The following is a description of the tool's command parameters:

-? Displays the help features of the tool;
-S followed by the name of the database server or IP address;
-U The next parameter for the database login username;
-p after the parameter for the database login password;
-e Use this feature when using Windows Integrated authentication;
The-D back-connect parameter is to which database uses SqlCacheDependency function;
-T back-connect parameter to which table to use SqlCacheDependency function;
-ed allows the use of SqlCacheDependency functions on the database;
-DD prohibit the use of SqlCacheDependency function to the database;
-et allows the use of SqlCacheDependency functions for data tables;
-DT prohibit the use of SqlCacheDependency function to the data table;
-LT lists which tables in the current database are already using the SqlCacheDependency feature.

For example, use the SqlCacheDependency feature in a petshop4.0 database: Aspnet_regsql-s localhost-e-D mspetshop4-ed
Taking the above command as an example, the SqlCacheDependency feature is used for a database named MSPETSHOP4, and SQL Server uses Windows Integrated authentication. We can also execute aspnet_regsql commands on related datasheets, such as:

Aspnet_regsql-s Localhost-e-D mspetshop4-t item-et 
aspnet_regsql-s localhost-e-D mspetshop4-t product-et
   
    aspnet_regsql-s Localhost-e-D mspetshop4-t Category-et


   

Finally, caching is used:

protected void Page_Load (object sender, EventArgs e) 
{ 
if (! IsPostBack) 
{ 
string key = ' tablecache ';//cache name 
DataSet data = (DataSet) Httpruntime.cache[key];//Get Cache 

//Judge the cached data to be null 
if (data = = null) 
{ 
//Get Data 
= Getdatasource (); 

Create cache dependency 
aggregatecachedependency cd = Tabledependency (); 

Create cache 
HTTPRUNTIME.CACHE.ADD (key, data, CD, DateTime.Now.AddHours (1), Cache.noslidingexpiration, 

Cacheitempriority.high, NULL); 

Gridview1.datasource = data; Bound Data 
gridview1.databind (); 
} 
 

Gets the method of the data source, the actual use makes the modification.

Private DataSet Getdatasource () 
{ 
string connectionstringlocal = configurationmanager.connectionstrings[" Localconnstring "]. ConnectionString; 
SqlConnection connpubs = new SqlConnection (connectionstringlocal); 
SqlDataAdapter dad = New SqlDataAdapter ("SELECT top * from Product", connpubs); 
DataSet ds = new DataSet (); 
Dad. Fill (DS); 
return ds; 
}

It is hoped that the caching technology described in this paper will help us to ASP.net program design.

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.