OutputCache attributes (4)-SqlDependency and sqldependency

Source: Internet
Author: User
Tags how to use sql server microsoft sql server 2005 how to use sql

OutputCache attributes (4)-SqlDependency and sqldependency

Directory

OutputCache Concept Learning

  • OutputCache attributes (1)

  • OutputCache attributes (2)

  • OutputCache attributes (III)

  • OutputCache attributes (4)-SqlDependency

 

NoStore

Boolean value, used to determine whether to block the second-level storage of sensitive information. The default value is false.

I did not write the test code for this attribute. I don't know how to write it. Let's talk about it!

 

SqlDependency

The string value that identifies a group of database/table name pairs. The output cache of pages or controls depends on these name pairs. Note that the SqlCacheDependency class monitors the tables in the database on which the output cache depends. Therefore, when table items are updated, these items will be removed from the cache when table-based round robin is used. If CommandNotification is used (in Microsoft SQL Server 2005), The SqlDependency class is used to register the query notification with the SQL Server 2005 Server.

 

SqlCacheDependency

The database cache dependency mainly solves the problem of how to promptly notify the cache and update the data in the cache when the database content changes. This section describes how to use SQL Server 2005 and. NET 2.0 to implement database cache dependencies.

Recommended: http://www.cnblogs.com/systemxgl/archive/2009/09/03/1559828.html


UseSqlDependencyFollow these steps:

1. register the database connection pool

Aspnet_regsql: http://msdn.microsoft.com/zh-cn/library/ms229862

Item

Description

-?

The command window displays the Aspnet_regsql.exe help text.

-W

Run the tool in wizard mode. If no command line parameter is specified, this is the default setting.

-C<Connection string>

Specifies the connection string of the computer on which the SQL Server is running and the database is installed or installed. If you only specify (-S) And login (-UAnd-P, Or-E), This option is not required.

-S<Server>

Name of the computer on which SQL Server is running and the database will be installed or installed.

-U<Logon ID>

The ID of the SQL Server user to log on. This option also requires a password (-P. If you want to use Windows creden (-E) For authentication, this option is not required.

-P<Password>

The SQL Server password to be used for logon. This option also requires the use of user ID (-U. If you want to use Windows creden (-E) For authentication, this option is not required.

-E

Use the Windows creden。 of the current logon user for authentication.

-Sqlexportonly<File Name>

Generate an SQL script file that can be used to add or remove a specified function. Do not perform the specified operation.

Diagram:

Aspnet_regsql.exe-S localhost-U sa-P abcd1234-ed-d TestStaff-et-t dbo. Staff

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

Case Sensitive

After the registration is completed, we will see an additional table in the database for registration and storage cache:

2. Configure the project WebConfig code

<? Xml version = "1.0"?> <! -- For more information on how to configure your ASP. NET application, please visit http://go.microsoft.com/fwlink? LinkId = 169433 --> <configuration> <connectionStrings> <add name = "TestSqlCacheDependency_connectionStrings" connectionString = "SERVER = localhost; UID = SA; PWD = abcd1234; DATABASE = TestStaff; "/> </connectionStrings> <system. web> <caching> <sqlCacheDependency enabled = "true"> <databases> <! -- PollTime: Set sqlCacheDependency to poll the database table to check the frequency of changes --> <add connectionStringName = "TestSqlCacheDependency_connectionStrings" name = "TestSqlCacheDependency" pollTime = "50000"/> </databas /sqlCacheDependency> </caching> <compilation debug = "true"/> </system. web> </configuration>

3. Configure the caller

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><%@ OutputCache SqlDependency="TestSqlCacheDependency:Staff" Duration="100" VaryByParam="id" %>

4. Test

Using SQL server profiler monitoring, You can refresh the page multiple times without any related SQL query requests, as shown below:

Five chapters on OutputCache are completed. If you have any questions, please correct them.

 

Author: Sakya bitter monk Source: http://www.cnblogs.com/woxpp/p/3986185.html this article copyright belong to the author and blog park a total, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection.

 

 


Briefly describe what do the Duration, VaryByParam, and VaryByControl attributes of the output cache @ OutputCache page command mean?

The control cache and the @ OutputCache command of the page output cache have both similarities and differences. The similarities between the two are that they are basically the same setup method, both of which are the @ OutputCache instruction strings with attributes set at the top of the file. There are two differences: first, the @ OutputCache command of the control cache is set in the user control file, and the @ OutputCache of the page output cache is set in the common ASP. NET file. Second, the @ OutputCache command in the control cache can only set six attributes: Duration, Shared, SqlDependency, VaryByControl, VaryByCustom, and VaryByParam. The @ OutputCache instruction string in the page output cache has up to 10 attributes. The preceding problems must be noted when setting the control cache. The following lists some examples of using the @ OutputCache command to set the control cache, with emphasis on Attribute applications such as VaryByParam and VaryByControl.

The @ OutputCache command in the user control sets the source code.

<% @ OutputCache Duration = "120" VaryByParam = "CategoryID; SelectedID" %>

The code above sets the cache validity period of the user control to 120 seconds, and allows you to use the CategoryID and SelectedID parameters to change the cache. By setting the VaryByParam attribute, instances of multiple user controls may be stored in the server cache. For example, for a page containing a user control, the following URL link may exist.

URL link of the page containing the user control

Localhost/... edid = 0
Localhost/... edid = 1

When you request a page with the above URL address, because of the @ OutputCache instruction setting in the control, especially the setting of the attribute VaryByParam, then, two versions of user control cache instances are stored in the server cache.

In addition to the VaryByParam attribute described above, the control cache settings also support the VaryByControl attribute. The VaryByParam attribute changes the cache based on the name/value pairs sent using POST or GET, while the VaryByControl attribute changes the cache through the server control contained in the user control file. The following is the sample code of the application of the VaryByControl attribute.

The @ OutputCache command in the user control sets the source code.

<% @ OutputCache Duration = "120" VaryByParam = "none" VaryByControl = "Category" %>

The above Code sets the cache validity period to 120 seconds, and the page does not change with any GET or POST parameters (even if the VaryByParam attribute is not used, it still needs to be explicitly declared in the @ OutputControl command ). If the user control contains the server control whose ID attribute is "Category" (for example, the drop-down box control ...... the remaining full text>
 
C # places the cached file in outputcache

It should be the cache of the client. Put it in the temporary folder of the client.
 

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.