Asp. NE's caching technology improves Web site performance

Source: Internet
Author: User
Tags httpcontext

One: Why should we use caching?

To understand the fundamentals of asp: to cache the data that is frequently accessed and the amount of time it takes to load, the user returns the data in memory directly to the user the next time they request the same data, which greatly improves the performance of the application.

Two: Cache classification (full page cache, page partial cache, application cache)

So when do we use the full page cache, when we use a partial page cache, and when do we use the application cache?

>> Full Page Cache scenario:

A) pages that do not require trivial update data

b) pages that occupy a significant amount of time and resources

>> page Partial cache:

As the name implies, part of the page cache, you only need to cache some pages in the page, the common method is "post-cache replacement", is to cache the entire page, and then replace the page does not need to cache the place. A substitution control implementation is used here, and the application is explained in detail later.

>> Application Cache:

Used to store application-related objects, primarily implemented by the cache class, gives us a mechanism that allows us to flexibly control cache operations in a coded fashion.

Three: How to use "full page cache"?

First we need to add such an instruction at the top of the page:

<%@ OutputCache duation= "5" varybyparam= "Id,name" varybycontrol= "None" location= "any" varybycustom= "browser"%>

Here is a detailed description of the purpose of each property:

        • Duration: The necessary attribute, that is, this page too long after the cache does not play a role, the unit is S

        • VaryByParam: A semicolon-delimited string variable/none/*, none means that the cache does not change with any parameters, * represents caching a separate page for any valid parameter. Note: This property is necessary if there is no Barybycontrol property.

        • VaryByControl: A semicolon-delimited string variable that represents the ID of the server control declared in the user control, and note that if there is no VaryByParam, the property is required.

        • VaryByCustom: Custom output caching requirements, it needs to define custom changes in Global.asax, the general default value is browser, according to the browser to determine the cache requirements.

        • Location: Specifies the position of the output cache entry, OutputCacheLocation enumeration, and the default value is any.

For example: Now we have a book details page, here take Dangdang's book details page for example, Dangdang when you choose a book, go to the book details page, the details page will certainly include: The book's clicks, Prices, directory information, authors, publishers and so on some introduction.

So we're going to cache this page the first thing you need to do is add an instruction at the top of the page:

1) Unconditional cache: <%@ OutputCache duration= "" varybyparam= "None"%>

What does that mean? This is the page cache time is 120 seconds, if you enter this detail page, and then the seller in the background to modify the book publishing house, then you now no matter how to refresh, you see the price is always just into the page when the publisher information. You must wait until 120s, click Refresh to see the seller's revised publisher information.

2) According to the condition cache: <%@ OutputCache duration= "varybyparam=" Bookid,bookname "%>"

This means that pages that are passed as arguments with BookId or bookname will be cached in 120s, for example: http://web/BookDetails.aspx?bookId, and the page will be cached 120s.

3) According to VaryByControl property: <%@ OutputCache duration= "" "varybyparam=" None "Varybycontrol=" Ddlcategories "%>

When the data that needs to be cached changes with the value of a control on the page, according to the naming rules of the ASP, everyone knows that the Ddlcategories control is a category control for a book (dropdawnlist).

Now if there is a book category page, then if the seller modifies a certain category under the information of a book, when the user on the page to see or modify the previous information, only after the replacement of the Ddcategories control items, the update will display the modified book information.

Three: How to use the "page part cache"?

<%@ OutputCache duration= "" varybyparam= "None"%>

What happens when we use a partial page cache, or take the book details page for example: now to count the number of clicks on a book.

First put into one: <asp:substitution id= "Sub1" runat= "Server" methodname= "GetClickCount"/> Control.

This control is used to show the number of clicks, and we can see that the MethodName property calls a GetClickCount () method.

static string GetClickCount (HttpContext contxt)

{

Query the number of clicks on the book based on the book ID

int clickcount = manage. Getclick (BOOKID);

return clickcount.tostring ();

}

If the number of hits is now 1000 times, then after you refresh, the amount of clicks may become 1000+ times, because at this moment the book may have other users click to access. This proves that the number of clicks on the book has not been cached.

It is important to note that the method called by the MethodName property of the substitution control must meet the following three conditions.

1) must be statically static method

2) The return value must be of type string

3) parameter type must be HttpContext type

Asp. NE's caching technology improves Web site performance

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.