Details on several practices of asp.net page caching

Source: Internet
Author: User

Asp tutorial. net page cache practices details

1. aspx page cache
The usage of page cache is very simple. You only need to add a statement <% @ outputcache duration = "60" varybyparam = "none" %> at the top of the aspx page.
In this way, the content of the entire page will be cached. The asp.net tutorial code and data source on the page will not be executed during the cache, but will directly output the cached page content.
The page cache is for all visitors to this page. In this way, the pressure on database tutorials is the same for one visitor and 10 thousand visitors, one access, and 1 million access.

Duration = "60" means to specify the cache time to 60 seconds. You can set the cache as needed. If the cache exceeds this time, the cache will expire. After the cache is generated again, it will be cached for 60 seconds, and so on.
Varybyparam = "none" means to set the cache with no parameters (the cache with parameters will be discussed below)
But for some pages with parameters, such as news content pages (for example, for viewnews. aspx pages, will not be read automatically? Id = 1). If this is set, the first news is cached, because? Id = 2 ,? Id = 3 is only different parameters of the page. To cache different news, you can set varybyparam = "id" to cache different id parameters separately. If multiple specified cache parameters exist, separate them with semicolons, for example, varybyparam = "id; number ".
If you want to create different caches for any different query strings, set varybyparam = "*". Generally, set.
These two parameters are required and cannot be used by default. Another important parameter, diskcacheable = "true | false", indicates whether to place the cache on the hard disk. If it is set to false, the cached data is stored in the memory.
Note that if the page data is small, the cached data can be stored in the memory. If the data is large, it is best to put it on the hard disk. Otherwise, it will occupy a large amount of memory and affect the running of the server, if the data is cached on the hard disk, remember to set the duration = "" value to a greater value, such as duration = "3600". If the value is too small, if the server writes data too frequently on the hard disk, the performance will be reduced. If the data is cached in the memory for duration ", do not set it too long, of course, the specific length of time requires you to try more.


2. Disable ie caching
Suppose there is a page new. aspx, after the first access by the client, it will have a new. aspx, file generation. if new. after accessing the data of aspx, I found that ie did not. the data of aspx is updated. Instead, the page is accessed for the first time!
Ie automatically (by default) calls a new in the temporary internet folder. instead of downloading the new. aspx, how can I enable ie to automatically download the new one. what about aspx? Just like clicking the refresh button to reload this page?
Solution 1:
Client setting method: internet Options & rarr; General & rarr; "settings" & rarr; in the temporary internet file; check each time you access this page.
It is best to delete temporary files at the same time in this setting. This method allows the customer to set their own browsers. If the customer forgets the setting, the new page will not always be downloaded locally. what do customers think? ("It must be a program error! ")
In addition, when others access your page, the client's browser will operate on them. Generally, this method is not suitable for solving such problems.
Solution 2:
Let the program automatically download the page! In this way, the page is not saved to the temporary internet folder. Every time you access the page, the browser downloads the page.
In the page_load event of the new. aspx code, add
Context. response. cache. setcacheability (httpcacheability. nocache.
When this sentence is not added, open the page. There will be a page file in the temporary internet folder. If this sentence is added, it will disappear. However, if an image or js file exists in an aspx file, it will still be downloaded to a temporary internet folder.


3. The showmodaldialog () function in js disables page caching when the aspx page is opened.
Method 1: first write an htm page with an iframe embedded in it. The src of iframe is the aspx page, and then the showmodaldialog () page is the htm page instead of the previous aspx page, in this way, there is no cache problem.
Method 2: Write response. expires =-1 in page_load () of the aspx page, which means that the page will expire immediately and no htm page will be nested outside.


4. Data source cache
For example, set the cacheduration (cache Time: seconds) of objectdatasource, enablecaching = true. In this way, the method specified by selectmethod is called every time period specified by cacheduration to execute database queries. In other cases, cached data is directly returned.

 

Related Article

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.