ASP. NET page Cache OutputCache Usage example (with AB pressure test comparison chart)

Source: Internet
Author: User
Tags browser cache

This article mainly introduces the use of Web Forms page, MVC use method, we Baidu.

A simple line of instructions can be achieved

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

So the entire page is cached, because I am the cache on the homepage, without any parameters so the VaryByParam value is set to None, if you want to cache the content by the specified parameters can be set to "Id;classid" this form, or simply use "*", all parameters to cache the content.

Second, before and after the cache performance comparison

Using AB to simulate 100 users 1000 times concurrency, the results are as follows:

As shown, after the cache is added, the throughput increases by about 10 times times, and the processing time of each request is reduced to about one-tenth of the original, and the performance is significantly improved.

Third, the OutputCache bug

This bug accompanies. NET1.0 to 4.0, that is, OutputCache will ignore the browser cache, I use. NET 4.0, Microsoft official said has been a perfect solution, but the actual bug is still there, the specific performance is:

When 1.VaryByParam is set to None, no Bug,f5 Refresh status code is 304 each time

2.VaryByParam is set to * or other values, each time you press F5 to refresh the status code is 200

Note: This time although the browser does not help us to cache the page, each request is from the server request, but the server side is the page cache, so with AB test efficiency is still very high. Of course, if the browser helps us cache, it will not produce this request at all.

3. The workaround is to add the following code to the Page_Load

Response.Cache.SetOmitVaryStar (true);

This allows the client browser to cache and, unless forced to flush, requests data from the server side again.

Cache data locally so that some data is not cached

This is too common, we always do not want to log in the state is also cached, assuming that the page did not make any improvements, you log in before the "login" "register" this button, when you log in and still display "login" "registration" this is certainly inappropriate, then we hope that this part of the content is not cached, Then we're going to use the substitution control.

Placing substitution controls in a page

<asp:substitution id="subloginstatus" runat="server " Methodname="getloginstatus"/>

Add a function to the background CS file Getloginstatus

 Public Static stringGetloginstatus (HttpContext context) {if(Context. session["usr"] !=NULL)    {        returnContext. session["usr"]. ToString () +"Welcome to your login"; }    Else    {        return ""Sign in " "register""; }}

This way the rest of the page is cached, and only the login state is dynamically read every time.

Note: Because a part of the page is dynamic, the status code for each request is 200.

V. Problems of OutputCache failure

There are cookies and cache conflicts on the Internet, we check them by ourselves, and I do encounter the problem of outputcache failure when I do, troubleshooting code discovery, including the following code

<script language="C #"runat="Server">stringSiteId ="ab71a8cfda50fbfeb2d3f6ee182af524";</script><!--#include file="hm.aspx"--><%stringret = Trackpageview (); %><div style="Display:none;">"<%= ret%>"Width="0"height="0"/></div>

This code is Baidu's statistical code, removed after the solution. If you really want to add Baidu statistics, you can replace the following similar code:

<div style="Display:none;"> <script>var_HMT = _HMT | | []; (function () {varHM = Document.createelement ("Script"); HM.SRC="//"; vars = document.getElementsByTagName ("Script")[0];        S.parentnode.insertbefore (hm, s);    })(); </script></div>

Accessories: AB pressure test Tool Download (release later)

ASP. NET page Cache OutputCache Usage example (with AB pressure test comparison chart)

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: 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.