ASP.NET2.0 provides some new technical features for lifting program performance, in which caching technology is a very important feature that provides a very good local data caching mechanism to effectively improve data access performance.
Data caching (datacaching) is a technique for staging data in a memory cache (sometimes in a hard disk cache). When the data itself changes less frequently, and the frequency of access is relatively high, the use of this technology will greatly improve the efficiency of the vigilance of data access.
1. Web page Output Caching
(1) Explicit caching
<% @OutputCacheDuration = "Varybyparam=none%>"
Statement <%@%> is a Web page configuration directive used to specify cache parameters for a Web page. Where: duration= "60" represents a cache duration of 60 seconds, and the VaryByParam property is used to specify a specific version of the page output, which is changed by which parameter. In <%@ The VaryByParam attribute must be added to the outputcache%> configuration directive. Even if you do not use this version attribute, you will add it, but set its value to none.
(2) Callback cache
<% @OutputCacheDuration =60varybyparam=stata%>
Cache separately based on lll.aspx?stata= content
The following methods can be refreshed based on the content
Copy Code code as follows:
<scriptrunat= "Server" >
Sharedfunctiongetcurrentdate (Byvalcontextashttpcontext) asstring
Returnnow.tostring ()
Endfunction
</script>
<asp:substitutionid= "Substitution1" runat= "Server" methodname= "Getcurrentdate"/>
2. Database Caching
In asp.net2.0, you can set cache parameters for a datasheet from a data source. There are several items in the properties of the SqlDataSource control that are used to set the cached parameters.
CacheDuration: Represents the duration of the cache. Default is infinite (infinite)
CacheExpirationPolicy: Cached policy. Includes two settings: absolute and sliding. When set to the former, the time limit is invalidated to the buffer; When set to the latter, the time limit is to immediately refresh the data in the cache and the updated data continues to be cached.
Cachekeydependency: Can be used for SQLServer2005 databases.
EnableCaching: False By default, that is, no data cache is used, and when this property is changed to True, the data cache is started.
Sqlcachedependecy: Caching dependencies. For example, setting to Pubs:authors represents a dependency between the authors datasheet in the pubs of the database and its data cache.
Sometimes the data table is too large, and the buffer may take up too much memory space. You can cache data into your hard disk's buffer, and to do this, you need to make some special settings. The setting includes two aspects.
a: Configure <% @OutputCacheDuration = "3600" varybyparam= "None" diskcacheable= "true" in Web pages%>
two: Specify the size of the hard drive in the Web.config file, and configure the following statements:
Copy Code code as follows:
<system.web>
<caching>
<outputCache>
<diskcacheenabled= "true" maxsizeperapp= "2"/>
</outputCache>
</caching>
</system.web>
where diskcacheenabled= "true" is used to start the hard disk caching function; maxsizeperapp= "2" is used to determine the capacity of the buffer area, where the maximum buffer size is 2M. Same as the slow access to the hard drive, Therefore, the hard disk cache applies only to situations where the cache lasts longer (in this case, the cache time is 1 hours).
3. Cache Pool
Data caching technology greatly improves the speed of data access, but it may cause data inconsistency. Therefore, the above caching technology is only suitable for data with large data volume and low frequency of change. Now asp.net2.0 new version combined with database SQLServer2005, To the data caching function has made a great improvement, not only simplifies the use of the process, but also improve the processing measures, one of the most prominent point is the combination with the database automatically solve the data failure problem.
In order to solve the problem of data invalidation in asp.net2.0, the "SQL cache dependency (sqlexpressdependencycaching)" of the New Generation database is established through the novel class SqlCacheDependency. This relationship allows the database to automatically monitor the status of the datasheet, and once the data table has changed, the trigger that immediately launches the database itself invalidates the data in the corresponding buffer.
This method can also be used for sql7/2000 databases, but you must first configure the databases that you are monitoring. Configuration methods are cumbersome, but they are simple to use after configuration. This functionality is not available for earlier versions of SQL Server or other types of databases, such as Oracle.
Configuring the Database Connection pool
C:\windows\microsoft.net\framework\v2.0.50727>aspnet_regsql.exe
Run the Asp.netsqlserver Configuration Wizard
Aspnet_regsql.exe–s "\dbname" –e–d "pubs" –ed
-ewindows Authorization
–ed is enabled
Aspnet_regsql.exe–s "\dbname" –e–d "pubs" –et–t "authors"
Change the cache when Pubs.authors changes
<% @OutputCacheDuration = "9999999" varybyparam= "None" sqldependency= "Pubs.authors"%>
Cache Configuration
Copy Code code as follows:
<caching>
<outputCache>
<diskcacheenabled= "true" maxsizeperapp= "2"/>
</outputCache>
<outputCacheSettings>
<outputCacheProfiles>
<addname= "Cachefor60seconds" duration= "/>"
</outputCacheProfiles>
</outputCacheSettings>
<!--
<sqlcachedependencyenabled= "true" polltime= "1000" >
<databases>
<addname= "Pubsdb" connectionstringname= "pubsconnectionstring"/>
</databases>
</sqlCacheDependency>
-->
</caching>