ASP. NET2.0 provides some new technical features for improving program performance. The cache technology is a very important feature and provides a very good local data cache mechanism, this effectively improves the performance of data access.
Data caching is a technology used to store data in the memory cache (sometimes stored in the hard disk cache. when the data itself is not frequently changed and accessed frequently, using this technology will greatly improve the efficiency of data access.
1. webpage output Cache
(1) Add explicit Cache
<% @ OutputCacheDuration = "60" VaryByParam = none %>
The statement <% @ %> is the webpage configuration instruction, which is used to specify cache parameters for the webpage. duration = "60" indicates that the cache Duration is 60 seconds. The VaryByParam attribute is used to specify the webpage output of a specific version. the VaryByParam attribute must be added to the <% @ OutputCache %> configuration command. even if you do not use this version attribute, you must add it, but set its value to none.
(2) callback Cache
<% @ OutputCacheDuration = 60 VaryByParam = stata %>
According to lll. aspx? Content of stata = is cached separately.
The following method can be refreshed Based on the Content
Copy codeThe Code is as follows:
<Scriptrunat = "server">
SharedFunctionGetCurrentDate (ByValcontextAsHttpContext) AsString
ReturnNow. ToString ()
EndFunction
</Script>
<Asp: SubstitutionID = "Substitution1" runat = "server" MethodName = "GetCurrentDate"/>
2. database cache
In ASP. NET2.0, you can set data table cache parameters through the data source. There are several items in the SqlDataSource control attribute for setting cache parameters.
CacheDuration: indicates the cache duration. The default value is Infinite (unlimited)
CacheExpirationPolicy: Cache Policy. there are two types of settings: Absolute and Sliding. when it is set to the former, the time limit is 1 to the cache area; when it is set to the latter, the time limit is to immediately refresh the data in the cache area and continue to cache the updated data.
CacheKeyDependency: Can Be Used for SQLServer2005 databases.
EnableCaching: The default value is False. If this attribute is set to True, data caching is enabled.
SqlCacheDependecy: cache dependency. For example, if it is set to pubs: authors, it indicates that the authors data table in the database Pubs is dependent on its data cache.
Sometimes the data table is too large, and the cache area may occupy too much memory space. data can be cached in the cache area of the hard disk. To achieve this, you need to make some special settings. the configuration includes two aspects.
I: Configure <% @ OutputCacheDuration = "3600" VaryByParam = "none" DiskCacheable = "true" %>
II: Specify the disk partition size in the Web. config file. The configuration statement is as follows:
Copy codeThe Code is as follows:
<System. web>
<Caching>
<OutputCache>
<DiskCacheEnabled = "true" maxSizePerApp = "2"/>
</OutputCache>
</Caching>
</System. web>
DiskCacheEnabled = "true" is used to enable the hard disk cache function. maxSizePerApp = "2" is used to determine the cache capacity. The maximum cache capacity is 2 MB. this is equivalent to slow access to the hard disk. Therefore, the hard disk cache is only applicable to cases with a long cache duration (in this example, the cache duration is 1 hour ).
3. cache pool
The data cache technology greatly increases the speed of data access, but may cause data inconsistency. therefore, the above cache technology is only applicable to data with a large amount of data and a low change frequency. now ASP. the new version of NET2.0, combined with the database SQLServer2005, has greatly improved the data cache function, which simplifies the use process and improves the handling measures, among them, the most prominent point is that the combination with the database automatically solves the data failure problem.
In ASP. to solve the data failure problem, NET2.0 uses the new SQLCacheDependency class to establish the "SQL cache dependency (SQLExpressdependencycaching)" with the next-generation database, which enables the database to automatically monitor the status of the data table, once the data table changes, immediately start the database trigger to invalidate the data in the corresponding cache.
This method can also be used for SQL7/2000 databases, but you must configure the monitored database first. the configuration method is troublesome, but it is very easy to use after configuration. earlier versions of SQL Server or other types of databases (such as Oracle) cannot use this function.
Configure database connection pool
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727> aspnet_regsql.exe
Run 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 cache when pubs. authors changes
<% @ OutputCacheDuration = "9999999" VaryByParam = "none" SqlDependency = "pubs. authors" %>
Cache Configuration
Copy codeThe Code is as follows:
<Caching>
<OutputCache>
<DiskCacheenabled = "true" maxSizePerApp = "2"/>
</OutputCache>
<OutputCacheSettings>
<OutputCacheProfiles>
<Addname = "CacheFor60Seconds" duration = "60"/>
</OutputCacheProfiles>
</OutputCacheSettings>
<! --
<SqlCacheDependencyenabled = "true" pollTime = "1000">
<Databases>
<Addname = "PubsDB" connectionStringName = "pubsConnectionString"/>
</Databases>
</SqlCacheDependency>
-->
</Caching>