ASP. NET 2.0 learning 3 -- cache Performance
Content
• Page Caching
• Control Caching
• Data Caching
• SQL data Cache
• Cache Configuration
I,ASP. NET 2.0 Output Caching
When a webpage is frequently accessed, We Can cache the entire webpage to improve the execution efficiency. The advantage of this is that when the user visits the webpage again, the formatted HTML will be directly displayed,
1. Output Cache
• Output Cache is a very effective technology to enhance access performance. Due to some features of IIS, the Output Cache is enabled by default, but some requests must be cached, developers are also required to customize them.
Custom Output Caching
• For custom Output Caching, we have two methods: one is based on the underlying API technology, and the other is based on the high-level @ OutputCaching
• Once the Output Caching has been customized, this webpage will generate a cache when it is accessed for the first time until the request expires.
• Define a Cache page using @ Output Cache to display the current time. You can see that the current time remains unchanged when the page is refreshed after it is cached.
Example
• On this page, <% @ Output Cache Duration = "60" VaryByParam = "none" %>
• This section defines the page to be cached, And the cache time is 60 seconds. After a page is cached, The varybyparam attribute is defined, it does not change because of the parameters accepted by the request. After 60 seconds, the page automatically clears the cache. At this time, the first access provides a new cache.
2. Parameter caching
Performance advantage: the cached content is changed by Parameters
• Sometimes we need to generate pages based on user requests, but there are only a limited number of combinations of user requests. In this case, several cache pages can be generated based on user requests for caching.
• Accept the parameter example to display different database content based on the user's selection of different States
Receive parameter example explanation
• Key statement <% @ output cache duration = "60" varybyparam = "state" %>
• A database query example is shown above, and the current access time is displayed at the bottom of the page. You can clearly see that for each different parameter, access within one minute will get the same timestamp, which indicates that the user accesses the same cache during this time.
3. Hard Disk output Cache
• By default, the output cache is cached on the hard disk. You can modify the diskcacheenable attribute to set whether the file is cached. You can also configure the size of the cached file in Web config.
<Caching>
<OutputCache>
<DiskCache enabled = "true" maxSizePerApp = "2"/>
</OutputCache>
</Caching>
• Use DiskOutput Cache
<% @ OutputCache Duration = "3600" VaryByParam = "name" DiskCacheable = "true" %>
4. Callback Cache
• By setting the callback cache mechanism, you can insert dynamic parts for each request in the page to make up for the insufficiency of using static cache separately.
• Callback cache example
• Callback cache through APIS
<% @ OutputCache Duration = "60" VaryByParam = "none" %>
<Script runat = "server">
Shared Function GetCurrentDate (context As HttpContext) As String
Return DateTime. Now. ToString ()
End Function
</Script>
<Html>
<Head id = "Head1" runat = "server">
<Title> Post Cache Substitution </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<B> This page uses post cache substitution to insert a dynamic value into a cached page. </B>
<Br/>
Time: <% = DateTime. Now. ToString () %>
<Br/>
<B>
Real Time: <% Response. WriteSubstitution (New HttpResponseSubstitutionCallback (GetCurrentDate) %>
</B>
</Div>
</Div>
</Form>
</Body>
</Html>
• Use Substitution for caching
<% @ OutputCache Duration = "60" VaryByParam = "none" %>
<Script runat = "server">
Shared Function GetCurrentDate (ByVal context As HttpContext) As String
Return Now. ToString ()
End Function
</SCRIPT>
<HTML>
<Head id = "head1" runat = "server">
<Title> post cache substitution </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<H4>
This page uses post cache substitution to insert a dynamic value into a cached page. </H4>
<P>
Time:
<% = Datetime. Now. tostring () %>
</P>
<P>
<B> Real Time:
<Asp: Substitution ID = "Substitution1" runat = "server" MethodName = "GetCurrentDate"/>
</B>
</P>
</Form>
</Body>
</Html>
• If you need to set the cache in more detail, you can set the System. web. the following statement is the same as <% @ Output Cache Duration = "60" VaryByParam = "none" %> When configuring the HttpCachePolicy attribute.
• Response. Cache. SetExpires (Now. AddSeconds (60 ))
Response. Cache. SetCacheability (HttpCacheability. Public)
Ii. Control CachePage Fragment Caching
• As an additional function of the Output cache, it also provides a caching technology specifically used to cache user controls.
• On the page, you can specify return parameters to determine the cached part of the control. Use the statement VaryByParam to specify that the control is changed according to the parameter.
• Example of modifying the cache content through return parameters
Accept Control Parameter Modification
• The cache user control can also use the control as the parameter source. You can specify the control as the data source of the Cache Control to cache the control data.
• Example of modifying cache content through Control Parameters
Iii. data Cache (Cache object)
• Asp.net provides a very quick way to cache databases, allowing users to conveniently cache page variables. To improve program efficiency.
• The cache lifecycle of a page variable is the same as that of an application.
• When modifying the background data, you also need to process the cache accordingly.
• Data Cache example
Cache. Insert ("Mydata", dataset)
DataSet ds = Cache ["Mydata"]
Iv. Database dependent CacheSQL Caching
• In the previous example, the cache technology is used to release the cache once the time is reached, regardless of whether the data on the server is changed or not.
• In the following example, by configuring the database connection pool, the cache changes only when the database data is changed.
• Connection pool configuration example
Cache Configuration
• You can configure different cache descriptions in webconfig and call this description on the page to reduce the workload of repeatedly defining cache descriptions.
<Caching>
<Sqlcachedependency enabled = "true" polltime = "1000">
<Databases>
<Add name = "pubsdb" connectionstringname = "pubsconnectionstring"/>
</Databases>
</SqlCacheDependency>
</Caching>
• Cache description definition example
1. Database Configuration
The aspnet_regsql command is run in c: \ windows \ Microsoft.net \ fframework \...
Start cache Database
Aspnet_regsql-s localhost-u sa-p mypassword-d pubs-dd
Start cache table
Aspnet_regsql-s localhost-u sa-p mypassword-d pubs-t titles-dt
2. Page Configuration
<% @ OutputCache Duration = "10000" SqlDependency = "pubs: titles" VaryByParam = "none" %>