SqlCacheDependency Cache Technology overview in asp.net,

Source: Internet
Author: User

SqlCacheDependency Cache Technology overview in asp.net,

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

The cache technology can greatly improve the running efficiency of websites with large traffic but few updates. the cache dependency mechanism provided by NET 2.0 allows us to conveniently manage and update the cache. The following is my learning experience and I hope it will be helpful.

The implementation code is as follows:

/** // <Summary> // create a cache dependency // </summary> // <returns> </returns> private AggregateCacheDependency TableDependency () {AggregateCacheDependency dependency = new AggregateCacheDependency (); dependency. add (new SqlCacheDependency ("MSPetShop4", "table name"); return dependency ;}

A very simple method. First, let's look at two new classes in. NET 2.0:

AggregateCacheDependency is in the System. web. in the Caching namespace, AggregateCacheDependency is mainly used to combine ASP. multiple dependencies between the items stored in the Cache object of the NET application and the arrays of CacheDependency objects.

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

How does SqlCacheDependency establish a connection between items stored in the Cache object and specific SQL Server database tables? Take a look 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> 

The database information is configured in the configuration section <databases> <add name = "MSPetShop4" connectionStringName = "LocalConnString" pollTime = "10000"/> </databases>, the SqlCacheDependency class automatically reads the configuration section information to establish a connection with the database. (Note) name = "MSPetShop4" must be the same as the database name in new SqlCacheDependency ("MSPetShop4", "table name. For more configuration information, see (MSDN help documentation ).

Make the database support the SqlCacheDependency feature:

To make SQL Server 7.0 or 2000 support the SqlCacheDependency feature, you need to perform the relevant configuration steps on the database Server. There are two ways to configure SQL Server:

Use the aspnet_regsql command line tool or the SqlCacheDependencyAdmin class.

The aspnet_regsql tool is located in the Windows \ Microsoft. NET \ Framework \ [version] folder. to configure SqlCacheDependency, run it in the command line.

The command parameters of the tool are described as follows:

-? Displays the help function of the tool;
-S is followed by the database server name or IP address;
-U is followed by the login username of the database;
-P is followed by the database login password;
-E this function is used for windows integrated verification;
-D the following parameter indicates which database adopts the SqlCacheDependency function;
-T followed by the SqlCacheDependency function for which table the parameter is used;
-Ed allows SqlCacheDependency for databases;
-Dd prohibits the use of SqlCacheDependency for databases;
-Et allows SqlCacheDependency for data tables;
-Dt prohibits the use of SqlCacheDependency for data tables;
-Lt lists the tables in the current database that have used the sqlcachedependency function.

For example, use the SqlCacheDependency feature in the petshop4.0 Database: aspnet_regsql-S localhost-E-d MSPetShop4-ed
The preceding command is used as an example to describe how to use the SqlCacheDependency function for a database named MSPetShop4, and the SQL Server adopts the windows integrated verification method. We can also execute the aspnet_regsql command on the relevant data table, such:

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"; // cache name DataSet data = (DataSet) HttpRuntime. cache [key]; // obtain the Cache // determine whether the cached data is null if (data = null) {// obtain 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; // binds GridView1.DataBind ();}}

The method used to obtain the data source is modified based on actual use.

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 cache technology described in this article will be helpful to the asp.net program design.


Aspnet server Cache Technology

Implementation

To implement the page output cache, you only need to add an OutputCache command to the page.

<% @ OutputCache Duration = "60" VaryByParam = "*" %>

Like other page commands, this command should appear at the top of the ASPX page, that is, before any output. It supports five attributes (or parameters), two of which are required.

Duration

Required attribute. The time when the page should be cached, in seconds. Must be a positive integer.

Location

Specify the location where the output should be cached. To specify this parameter, it must be Any, Client, Downstream, None, Server, or ServerAndClient.

VaryByParam

Required attribute. The name of the variable in the Request. These variable names should generate separate cache entries. "None" indicates no change. "*" Can be used to create a cache entry for each variable group. Variables are separated.

VaryByHeader

Changes cache entries based on changes in the specified header.

VaryByCustom

Allow you to specify custom changes (for example, "Browser") in global. asax ").

The combination of the required Duration and VaryByParam options can be used to handle most cases. For example, if your product directory allows users to view the directory Page Based on categoryID and page variables, you can use the parameter value to "categoryID; page "VaryByParam caches the product directory for a period of time (if the product is not changed at any time, one hour is acceptable, so the duration is 3600 seconds ). This creates separate cache entries for each directory page of each category. Each entry is counted from its first request for one hour.

VaryByHeader and VaryByCustom are mainly used to customize the appearance or content of the page based on the client accessing the page. The same URL may need to be rendered and output for both the browser and mobile client. Therefore, different content versions must be cached for different clients. Alternatively, the page may have been optimized for IE, but it must be completely optimized for Netscape or Opera (not just to destroy the page ). The next example is very common. We will provide an example to illustrate how to achieve this goal:

Example: VaryByCustom is used to support browser customization.

To make each browser have a separate cache entry, the value of VaryByCustom can be set to "browser ". This function has been built into the cache module and will insert a separate page Cache version for each browser name and major version.

<% @ OutputCache Duration = "60" VaryByParam = "None" VaryByCustom = "browser" %>

Segment cache, user control output Cache

Caching the entire page is usually not feasible because some parts of the page are customized for users. However, the rest of the page is shared by the entire application. These parts are most suitable for cache using fragment caching and user controls. Menu and other layout elements, especially those dynamically generated from the data source, should also be cached in this way. If necessary, you can configure the Cache control to be changed based on changes to its control (or other attributes) or any other changes supported by the page-level output cache. Using hundreds of pages of the same control group can also share the cache entries of those controls, rather than retaining a separate Cache version for each page.

Implementation

The description used by the fragment cache... the remaining full text>

How can aspnet associate page cache with files!

This is a simple problem.
VB example
Response. AddCacheDependency (New CacheDependency ("F: \ w webpage \ newtuanxue_ajax \ tuanxue \ 11.txt "))
On the third floor, the CacheDependency object is not a file name.

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.