Use absolute expiration in ASP. NET to implement page Cache

Source: Internet
Author: User
ArticleDirectory
    • Context
    • Implementation Policy
    • Test considerations
    • Result Context
    • Variant
    • Related Mode
Content on this page

Context
Implementation Policy
Test considerations
Result Context
Variant
Related Mode

Context

You need to build a web application in ASP. NETProgramAnd you want to cache the page to improve performance. You have evaluatedPage Cache(Page cache) in the proposed alternative scheme, and has decided to adopt the absolute expiration policy.

Back to Top Implementation Policy

The page cache improves the Request Response throughput by caching the content generated from a dynamic page. By defaultASP. NETPage caching is supported, but the output from any given response is not cached unless a valid expiration policy is defined. To define an expiration policy, you can use a low-levelOutputcache APIOr advanced@ OutputcacheCommand.

After page caching is enabledGetThe request creates a page cache entry. The cache entries on this page are used to respond to subsequentGetOrHeadRequest until the cached response expires.

The page cache follows the page expiration policy. If the expiration policy is60The page is cached in seconds.60Seconds later, the page will be deleted from the output cache. If the cache receives another request after this time, it executes the web pageCodeAnd refresh the cache. This expiration policy is called "absolute expiration", which means that the page remains valid until a certain time.

The following example shows how to use@ OutputcacheCommand to cache the response:

 
<% @ Outputcache duration = "60" varybyparam = "NONE" %> <HTML> <script language = "C #" runat = "server"> void page_load (Object sender, eventargs e) {timemsg. TEXT = datetime. now. tostring ("G") ;}</SCRIPT> <body>  

This example shows the response generation time. To understand the role of the output cache, call the webpage and pay attention to the response generation time. Then refresh the page and you will notice that the time has not changed. This shows that the second response is provided from the cache.

The following code is used to activate the response page cache:

 
<% @ Outputcache duration = "60" varybyparam = "NONE" %>

This command only indicates that the page should be cached60Second, the page will notGetOrPostParameters. In the first60Requests received within seconds are provided from the cache. After60Seconds later, the page is deleted from the cache; the next request caches the page again.

Back to Top test considerations

Page cache makes testing more difficult. For example, if you change a webpage and view the webpage in a browser, you may not see the updated webpage because the browser displays the page provided from the cache, instead of the newly generated page. Ideally, you can disable page caching and then run a test without caching. After these tests are successfully run, you can enable caching and then run the tests to be cached.

Context of the returned top result

InASP. NETUse the absolute expiration mode.Page CacheIt has the following advantages and disadvantages:

Advantages

    • This is so far ASP. NET The simplest page caching Method. If you want to analyzeWebThe usage mode of the application to determine which pages are cached. In many cases, expiration may be enough, and it is undoubtedly a good start. In addition, we need to consider the variability of dynamic content on the web page. For example, the weather webpage expiration policy can be60Minutes, because the weather will not become very fast. However, webpages that display stock quotations may not be cached at all. To determine the correct expiration time, you must know which pages are most frequently viewed and understand the variability of the data contained in the pages.

    • You can set different expiration policies for different webpages.. In this way, you can cache webpages that are frequently accessed without wasting the cache space on webpages that are rarely accessed. You can also refresh webpages that contain more frequently changed data than other data.

Disadvantages

  • The dynamic content on the cached page may be invalid.. This is because the page expires based on time rather than content. In the preceding example, it takes several seconds to display on the webpage. Because each60Second build once, second field is invalid in the moment after page build. The invalid data section in this example is small. For example, if you want to display financial quotations that are time-sensitive and require high accuracy, consider using a cache policy that ensures that you will never display invalid data. (See"Page data caching".)

  • This policy does not support passing parameters to webpages. Dynamic Web pages are often determined by parameters. For example, the weather webpage may use zip code as a parameter. Unless you want thousands of postal codes (for example, the United States has42,000Postal Code) create different web pages andURLOtherwise, you cannot cache the webpage with an absolute expiration date.Vary-by-parameter cachingSolved this problem.

  • Absolute expiration is applicable only when the entire page remains unchanged.. In many applications, most of the web pages are rarely changed (very suitable for caching), but they are coupled with other frequently changed parts (which cannot be cached. Because the absolute expiration mode only caches the entire webpage, it cannot use local changes like this. In these cases,Page fragment cachingIt may be a better choice because it can cache part of the webpage.HtmlThe Framework provides another option for simulating page segments. However, the frameworkWebThe browser has some known issues, such as navigation and printing issues.

  • Unable to refresh the cached page. The webpage remains in the cache until it expires or the server is restarted. This makes the test a problem. In addition, the cache also has a big problem when there are few changes to the data and once the changes are made, there is no delay. For example, it may be enough to update the weather forecast every two hours. However, if the hurricane is approaching, you may not want to wait two hours before updating the weather forecast.

  • You must modify the code on each page to change the expiration policy.. Because the expiration policy can only be changed in the Code, the cache mechanism of the entire application is not disabled.

  • Storing webpages in the cache requires disk space on the server. In the preceding example, a small webpage does not require much disk space. However, as the content on each web page and the number of pages in the cache increaseWebThe server provides more disk space.

Back to header Variation

The following pattern explainsPage CacheOther implementation methods:

    • Vary-by-parameter caching

    • Sliding expiration caching

Back to Top Mode

For the page cache design and implementation policies, see the following modes:

    • Page data caching

    • Page fragment caching

Back to Top

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.