Remember a good friend, dongge, who said several things that must be done before graduation from college: 1. Understanding the petshop architecture; 2. Skilled use of cache technology; 3. petshop renovation. Over the past few days, I have carefully studied the Cache technology ). I would like to share with you the hope that it will be useful for beginners.
The cache dependency of ASP. NET2.0 database ensures that the cache is invalid only when the table content changes, and the cache data can be refreshed in a timely manner. According to my experiment, cache failure occurs as long as the customer recompiles or the database table changes. The steps are as follows.
1. Enable table cache dependency. Take the Authors table of the Pubs database as an example:
You can use the "Visual Studio 2005 command prompt": if you do not understand the configuration, you can use apsnet_regsql.exe -? Command to view help documentation
// Enable the database dependency Function
Aspnet_regsql.exe-S localhost-U sa-P sa-d database name-ed
// Enable the dependency function of the database table
Aspnet_regsql.exe-S localhost-U sa-P sa-d database name-t table name-et
// Disable the data cache dependency function of the database
Aspnet_regsql.exe-S localhost-U sa-P sa-d database name-dd
// Disable the data cache dependency function of a data table
Aspnet_regsql.exe-S localhost-U sa-P sa-d database name-t table name-dt
Or log in with windows authentication (as shown below)
// Aspnet_regsql-S. "sqlexpress-E-d pubs-ed
// Aspnet_regsql-S. "sqlexpress-E-d pubs-t authors-et
To view the existing cache dependency table of the database, run the following command:
Aspnet_regsql-S. "sqlexpress-E-d pubs-lt
2. Configure cache in the web. config file as follows:
Web. comfig
<ConnectionStrings>
<Add name = "Pubs" connectionString = "server =." sqlexpress; database = pubs; integrated security = true ;"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
<System. web>
<! -- Configure the buffer connection pool -->
<Caching>
<SqlCacheDependency enabled = "true" pollTime = "1000">
<Databases>
<Add name = "Pubs" connectionStringName = "Pubs" pollTime = "1000" type = "codeph" text = "/codeph"/>
</Databases>
</SqlCacheDependency>
</Caching>
</System. web>
3. Test the cache dependency for encoding:
Code
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Web. Caching;
Using System. Data. SqlClient;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Set SqlCacheDependency, it can only be related to one table in the database.
// Aspnet_regsql-S. \ sqlexpress-E-d pubs-lt
// Note that the following table must be the same as the preceding output case.
SqlCacheDependency dependency = new SqlCacheDependency ("Pubs", "authors ");
// If cache is invalid, then regenerate dataset and insert into cache.
If (Cache ["DATA"] = null)
{
Cache. Insert ("DATA", GetDataSet (), dependency );
Response. Write ("create cache! ");
}
Else
Response. Write ("read cache! ");
GridView gvAuthors = new GridView ();
This. form1.Controls. Add (gvAuthors );
GvAuthors. DataSource = (DataSet) Cache ["DATA"];
GvAuthors. DataBind ();
}
// Generate dataset
Public DataSet GetDataSet ()
{
SqlConnection connection = new SqlConnection (@ "data source =. \ sqlexpress; initial catalog = Pubs; Integrated Security = True ");
DataSet ds = new DataSet ();
SqlCommand command = connection. CreateCommand ();
Command. CommandText = "select * from authors ";
SqlDataAdapter sa = new SqlDataAdapter ();
Sa. SelectCommand = command;
Sa. Fill (ds, "usertomin ");
Return ds;
}
}
4. Continued (unfinished )....