asp. An overview of SqlCacheDependency caching technology in net

  • 2021-01-19 22:04:21
  • OfStack

This article illustrates the SqlCacheDependency caching technology in asp.net, which is of great practical value for large-scale web programming. The details are as follows:

Caching technology can greatly improve the operating efficiency for websites with large traffic but few updates. In addition, the cache dependency mechanism provided by.NET 2.0 makes it easy to manage and update the cache. The following is my learning 1 dim sum experience, I hope to play a role in attracting jade.

To create a cache dependency, implement the following code:


/**//// <summary> 
///  Establish a cache dependency  
/// </summary> 
/// <returns></returns> 
private AggregateCacheDependency TableDependency() 
{ 
AggregateCacheDependency dependency = new AggregateCacheDependency(); 
dependency.Add(new SqlCacheDependency("MSPetShop4", " The name of the table ")); 

return dependency; 
} 

First, let's take a look at two new NET 2.0 classes:

AggregateCacheDependency In the System.Web.Caching namespace, AggregateCacheDependency is primarily used to compose multiple dependencies between items stored in the Cache object of an ASP.NET application and an array of CacheDependency objects.

SqlCacheDependency also exists in the System.Web.Caching namespace. This class is used to establish a relationship between items stored in Cache objects of ASP.NET applications and specific SQL Server database tables.

How does SqlCacheDependency establish a relationship between items stored in an Cache object and a specific SQL Server database table? A look at the Web.Config configuration file is clear.


<?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> 

The configuration section < databases > < add name="MSPetShop4" connectionStringName="LocalConnString" pollTime="10000"/ > < /databases > The ES56en class automatically reads the configuration section information to establish a connection with the database. name="MSPetShop4" must correspond to the database name in new SqlCacheDependency("MSPetShop4", "table name "). More configuration information can be found in the MSDN help documentation.

Enable the database to support SqlCacheDependency features:

To make SQL Server support SqlCacheDependency features in versions 7.0 or 2000, you need to perform configuration steps for 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 located in the folder Windows\Microsoft.NET\Framework\[version]. If you want to configure SqlCacheDependency, you need to do it on the command line.

Here are the command parameters for the tool:

- the & # 63; Displays the help function of the tool;
-The parameter after S is the name of the database server or IP address;
-U parameter is the database login user name;
The parameter after P is the login password of database;
-E This feature is used when using windows for integration validation;
-d parameter after which database to use SqlCacheDependency function;
-t is followed by parameters for which table to use SqlCacheDependency function;
-ed allows the use of SqlCacheDependency functionality for databases;
-dd does not allow SqlCacheDependency functionality for databases;
-et allows the use of SqlCacheDependency functionality for data tables;
-dt does not allow the use of SqlCacheDependency for data tables;
-lt lists which tables in the current database have adopted sqlcachedependency functionality.

aspnet_regsql-S localhost-E-d MSPetShop4-ed aspnet_regsql-S localhost-E-d MSPetShop4-ed
As an example of the above command, the database named MSPetShop4 will use SqlCacheDependency functionality, and SQL Server uses windows integrated authentication. aspnet_regsql: aspnet_regsql: aspnet_regsql: aspnet_regsql: aspnet_regsql


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, use the cache:


protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
string key = "TableCache"; // Name of the cache  
DataSet data = (DataSet)HttpRuntime.Cache[key]; // Access to the cache  

//  Determine that the cached data is empty  
if (data == null) 
{ 
//  To get the data  
data = GetDataSource(); 

//  Creating a cache dependency  
AggregateCacheDependency cd = TableDependency(); 

//  To create the cache  
HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, 

CacheItemPriority.High, null); 
} 

GridView1.DataSource = data; // Data binding  
GridView1.DataBind(); 
} 
} 

Get the data source method, combined with the actual use of the change.


private DataSet GetDataSource() 
{ 
string ConnectionStringLocal = ConfigurationManager.ConnectionStrings["LocalConnString"].ConnectionString; 
SqlConnection connPubs = new SqlConnection(ConnectionStringLocal); 
SqlDataAdapter dad = new SqlDataAdapter("SELECT TOP 50 * FROM Product", connPubs); 
DataSet ds = new DataSet(); 
dad.Fill(ds); 
return ds; 
}

I hope the caching technology described in this paper is helpful to asp.net program design.


Related articles: