Introduction to ASP. NET 2.0 data cache

Source: Internet
Author: User
Tags connectionstrings
In ASP. NET 2.0, many new functions and controls are added. Among them, the data cache function has greatly changed compared with Asp.net 1.1. As we all know, the data cache function is very important. We can put some data that remains unchanged for a period of time in the cache, so that we do not need to read the database every time, when you need the data again next time, it can be obtained directly from the cache, greatly enhancing the efficiency.

In Asp.net 1.1, the data caching function has been well implemented, but there is a small problem, that is, if the data in the database changes, the cache cannot be updated within the specified time, but must wait until the cache expires. For example, if you use page caching for product details, such as prices, on a webpage, If you modify the information in the background, the user will not immediately see the information, it takes some time to see it. In some application scenarios, if you want to update the database, the changes in the cache can take effect immediately. In Asp.net 1.1, it is relatively difficult to implement, in Asp.net 2.0, this function can be conveniently implemented. This article will introduce the implementation of this function with. NET Framework 2.0 beta and Visual Web Developer express beta.

First, in Asp.net 2.0, new cache functions are provided for SQL Server 7/2000 and the SQL Server 2005 (Yukon) to be released. This section focuses on the cache function for SQL Server 7/2000. This new data cache function is called sqlcachedependency. To implement this function, follow these steps:

1. Under the. NET Framework 2.0 installation directory
(The default value is winnt \ microsoft. Net \ framework \ v2.0.40607). start a command line tool called aspnet_regsql.exe, for example:

Aspnet_regsql-s localhost-U sa-P 123456-D pubs-ed

The above means that the local database server localhost is specified, the login user name and password are specified, and the database to be used is specified with the-D parameter (pubs is specified here) database. The-ed parameter indicates that the sqlcachedependency function is allowed for the database.

Next, we need to specify which table to use the data sqlcachedependency function, for example:

Aspnet_regsql-s localhost-U sa-P 123456-ed-D pubs-et-T authors

The preceding statement indicates that the sqlcachedependency function is used for the authors table in the pubs database. The-t parameter is followed by the table name, and-et allows the table to use the sqlcachedependency function. Other parameters are described as follows:

Parameters Parameter meaning
-? The help function of the tool is displayed.
-S The following parameter is the name or IP address of the database server.
-U The following parameter is the Login User Name of the database.
-P The following parameter indicates the login password of the database.
-E This function is used for Windows integrated verification.
-T Which table is followed by the sqlcachedependency function.
-D Which database is followed by the sqlcachedependency function?
-Ed Allow sqlcachedependency for Databases
-Dd Disable sqlcachedependency for Databases
-Et Allow sqlcachedependency for Data Tables
-Dt Disable sqlcachedependency for Data Tables
-Lt Lists the tables in the current database that have used the sqlcachedependency function.

The principle of sqlcachedependency is simply to use the specialized aspnet_regsql tool to pre-set the database and table to be monitored, add a special table to the database, and open the database, you will find an additional data table aspnet_sqlcachetablesforchangenotification, for example:

This table has three fields. "tablename" records the name of the data table to be tracked. "icationicationcreated" records the start time of tracing. "changeid" is an int type field, when the data in the tracked data table changes, the value of this field is increased by 1. In addition, several stored procedures will be added to the specified database to enable ASP.. net engine queries the data table tracked, and adds several triggers to the table to use sqlcachedependency, corresponding to insert, update, and delete operations respectively. ASP. the net engine executes the stored procedure "aspnet_sqlcachepollingstoredprocedure" that directly returns the content of the "aspnet_sqlcachetablesforchangenotification" table to ASP. the net engine knows which table the data changes.

Next, we use Visual Web Developer 2005 Beta to create a website and add a label Label to it to display the current execution time. Add a gridview control to display the table authors from the pubs database. The Code is as follows:

<% @ Page Language = "VB" autoeventwireup = "false" compilewith = "default. aspx. VB" classname = "default_aspx" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
& Nbsp; <asp: Label id = "label1" runat = "server" text = "label" width = "74px" Height = "19px"> </ASP: label> & nbsp;
<Br/>
<Asp: gridview id = "gridview1" runat = "server" performanceid = "sqlperformance1">
</ASP: gridview>
<Br/>
<Br/>
<Asp: sqldatasource id = "sqldatasource1" runat = "server" selectcommand = "select * from authors"
Connectionstring = "Initial catalog = pubs; Data Source = localhost; user id = sa; Password = 123456">
</ASP: sqldatasource>
</Form>
</Body>
</Html>

Add the following code to the page_load event of the default. aspx. VB code:

Private sub page_load (byval sender as object, byval e as system. eventargs) handles me. Load
Label1.text = ctype (system. datetime. Now (), string)
End sub

Next, configure the Web. config file. The configuration file we use is as follows:

<? XML version = "1.0"?>
<Configuration>
<Connectionstrings>
<Add name = "pubsconn" connectionstring = "Initial catalog = pubs; Data Source = (local); User ID = sa; Password = 123456"/>
</Connectionstrings>
<System. Web>
<Caching>
<Sqlcachedependency enabled = "true" polltime = "500">
<Databases>
<Add name = "pubs" connectionstringname = "pubsconn"/>
</Databases>
</Sqlcachedependency>
</Caching>
</System. Web>
</Configuration>

Note that the polltime parameter is millisecond and the minimum value is 500. In this parameter, set the system time to check whether the data in the cache needs to be updated. The <database> tag must contain the database connection string to be used, which must be consistent with the name defined in <connectionstrings>. (In this example, the connectionstring name is pubsconn.

Finally, in the program header, we add the page cache Definition Statement as follows:

<% @ Outputcache duration = "60" varybyparam = "NONE" sqldependency = "pubs: authors" %>

In the preceding statement, the page output cache is defined as one minute, and the sqldependency and sqldependency attributes are defined in the form of "Name: Table name, that is, we are on the web. the name in <databases> defined in config, Which is pubs. Authors in "pubs: authors" indicates the table name authors to use sqldependency. Note that although VB.net is used, parameters in the sqldependency attribute are case sensitive, such as "pubs: the table name authors in authors must be the same as the table name authors defined in the command line using aspnet_regsql.

Run our program without modifying the data in the database. For example:

We constantly refresh the application and will find that the above time remains unchanged due to the use of the cache function. Now we open SQL Server, modify the white name of the author whose au_id is 172-32-1176, change it to red, and then refresh the program. Have you seen it? Because the sqldependency function is used, the cache immediately displays the newly modified data:
  

This article briefly introduces the newly added sqldependency function in Asp.net 2.0. This function also has many other applications. For details, refer to the relevant msdn documents.

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.