Sqlcachedependency features in ASP.NET 2.0

xiaoxiao2021-03-06  41

The page cache in ASP.NET is a very useful stuff. As long as you add an OutputCache tab above the page, you can save the page directly in the cache in the duration, without the need. Let the ASP.NET engine execute the page code again, of course, save this execution time is not the most important reason, the main reason is that if the page content is removed from the database, then you can save the connection database, take the step of data, this The benefit is bigger.

PageCache is a little small problem, for example, the homepage of many ASP.NET Forum has used PageCache, so the latest post posted in a version does not immediately reflect the home page, but needs to wait for a while. Although it is a bit helpless, this delay is not allowed in some occasions (such as a page that is easy to display item auction status).

That is to say, the Cache in ASP.NET cannot automatically change according to the corresponding data in the database, and the corresponding Cache expires, the new SQLCachedependeency feature in ASP.NET 2.0 makes it possible. (Of course, as long as we understand the approximate implementation in ASP.NET 2.0, we can implement this feature based on ASP.NET 1.0, and later I summarize the corresponding method.)

To apply Sqlcachedependencence features in ASP.NET 2.0, steps are as follows (based on whidbey PDC version):

1, make the database support SQLCACHEDEPENDENCY

In the installation directory of .NET Framework 1.2 (usually Windows / Microsoft.Net / Framework / V1.2.30703), there is an ASPNET_REGSQLCACHE.EXE, this command line tool allows our SQLServer 7.0 or SQLServer 2000 to support the SQLCachedependencence feature,

First: "ASPNET_REGSQLCACHE -S server name -u login ID -P password -D database name -ed", this command supports SQLCACHEDEPENDENCY to specify the database.

Then we join the data table to be tracked: "ASPNET_REGSQLCACHE -S server name -u login ID -P password -D database name -t to track the name of the data table - Table", this command allows the specified table to support SQLCachedependency.

What did it do after the scene?

First, it has created a table in the specified database called "ASPNET_SQLCACHETABLESFORCHANGENOTIFICATION", this table has three fields, "Tablename" records the name of the data to track, "NotificationCreated" records the time that starts tracking, "ChangeID" is a INT type field, each of the values ​​of this field plus 1 whenever the data of the tracking data table is changed.

It also adds several stored procedures in the specified database to allow the ASP.NET engine to query the situation of the tracking data sheet.

Then, it will add several Trigger to the Table to track, respectively, corresponding to INSERT, UPDATE, DELETE operation, and these Trigger's statement is very simple, that is, the "ASPNET_SQLCACHETABLESFORCHANGENOTIFICATION" table corresponding to "Tablename" field for this tracking The "ChangeID" field of the name of the table is plus one 1.

The ASP.NET engine passes the stored procedure "ASPNET_SQLCACACALLINGSTOREDPROCEDURE" by executing it, this stored procedure returns the contents of the "ASPNET_SQLCACHETABLESFORCHANGENOTINET_SQLCACHETABLESFORCHANGENOTINOTIFICATION" table, so that the ASP.NET engine knows which table of data changes. This stored procedure is performed once every 500 milliseconds, but you can modify this interval in Web.config. My experience is that this query is also very resource, huh, huh. 2. Web.config configuration

The configuration in Web.config is simple.

Connectionstring = "Server = SQLSERVER1; UID = SA; PWD =

Password; database = portaldb "/>

ConnectionstringName = "SQLSERVERCONNECTIONSTRING" />

3. Specify the SQLCachedependency feature in the OutputCache tag on the page:

<% @ outputcache duration = "9999" VarybyParam = "None" Sqldependency = "Portaldb: Tracking Data Table Name"%>

As long as INSERT, UPDATE, DELETE operations are performed on this tracking table, the Trigger on the data table modifies the corresponding "changID" field value of the corresponding record of the corresponding record of the "ASPNET_SQLCACHETABLESFORCHANGENOTINET_SQLCACHETABLESFORCHANGENOTIFICATION" table, and the ASP.NET engine will pass Get a new value to know the content of the tracking table changes, automatically invalidate the cache of this page.

Whenever 1, what about yukon?

From the above, SQL Server does not have a built-in automatic tracking data table, then notify the ASP.NET engine (this is certain, SQL Server comes out for N long), so the development group of ASP.NET 2.0 Customized Table, Trigger, StoredProcedure, etc. to implement data change tracking. YUKON is no longer so troublesome, yukon has a NOTINICATION Delivery Service, which will directly notify an IIS built-in listener through the 80 port of Web Server, then this listener notifies the ASP.NET.

And everyone can notice that the tracking mode described above can only track the data changes to the table, that is, the ASP.NET engine can only know that the data of a table has changed, and which record is generated. The changes are not tracked, and Yukon's Notification Delivery Service can record the tracking of the recording level. Later 2, can you be implemented on the current ASP.NET?

Of course, let's follow the first steps above (or define a set of rules to track the TABLE data change), in accordance with the gourd painting scoop, add these Table, Trigger what.

ASP.NET's PageCache has a VarybyCustom property, this property can make our own rules that "caching expiration" (exactly, it can make us customize the version of the cache page, but indirectly enable customization Cache "expire", huh, huh), as long as we rewrive the httpapplication.getvaryByCustomString () method in Global.asa, this method is based on the input parameter string, such as "checkdbtable = users", query the "ASPNET_SQLCHETABLESFORCHANGENGENOTINOTIFICATION" in the database (or Your own tracking record table), let this getVaryByCustomString () method returns the value of the "ChangeID" field. VarybyCustom's usage MSDN documentation has a detailed description.

转载请注明原文地址:https://www.9cbs.com/read-72437.html

New Post(0)