Examples of OutputCache usage in ASP. NET page cache (with a comparison diagram of AB stress test ),

Source: Internet
Author: User

Examples of OutputCache usage in ASP. NET page cache (with a comparison diagram of AB stress test ),

This article mainly introduces how to use Web forms pages, MVC, and Baidu.

1. A simple line of commands can be implemented.

<%@ OutputCache VaryByParam="none"  Duration="300" %>

In this way, the entire page is cached. As I cache the Homepage without any parameters, the VaryByParam value is set to none. If you want to cache the content through the specified parameters, you can set "id; classid ", or simply use" * "to cache content through all parameters.

Ii. performance comparison before and after caching

Use AB to simulate 100 concurrent operations for 1000 users. The following result is returned:


As shown in, after the cache is added, the throughput is increased by about 10 times, and the processing time of each request is reduced to about, which significantly improves the performance.


3. OutputCache BUG

This bug is accompanied. from NET1.0 to 4.0, that is, OutputCache ignores the browser cache. NET 4.0, Microsoft officially said it has been perfectly resolved, but the detected bug still exists, the specific performance is:

1. When VaryByParam is set to none, there is no bug. F5 refreshes the status code 304 each time.


2. When VaryByParam is set to * or another value, each time you press F5 to refresh the status code, the status code is 200.



Note: at this time, although the browser does not help us cache the page, every request is from the server, but the Server caches the page, so the efficiency of testing with AB is still very high. Of course, if the browser helps us cache, this request will not be generated at all.

3. The solution is to add the following code in Page_Load:

Response.Cache.SetOmitVaryStar(true);

In this way, the client browser implements the cache, and will request data from the server again unless forced refresh.

4. Locally cached data so that some data is not cached

This is too common. We do not want the logon status to be cached. Assume that the page has not been improved. Before you log on, you use the "Log on" and "register" button, when you log on to the console and still display [Login] [registration], this is definitely inappropriate. If we want this part of content not to be cached, we need to use the Substitution control.

Place the Substitution control on the page

<asp:Substitution ID="subLoginStatus" runat="server" MethodName="GetLoginStatus"/>

Add the GetLoginStatus function to the backend cs file.

Public static string GetLoginStatus (HttpContext context) {if (context. Session ["usr"]! = Null) {return context. Session ["usr"]. ToString () + "Welcome to log on";} else {return "[Login] [register ]";}}

In this way, other parts of the page are cached, and only the logon status is dynamically read each time.

Note: because some of the pages are dynamic, the status code of each request is 200.

V. OutputCache failure

The Cookie conflicts with the Cache on the Internet. Check whether the Cookie and Cache conflict with each other and check whether the OutputCache is invalid. Check the code and find that the following code is included:

<script language="C#" runat="server">    string siteId = "ab71a8cfda50fbfeb2d3f6ee182af524";</script><!-- #include file="hm.aspx" --><% string ret = TrackPageView(); %><div style="display:none;"></div>


This code is Baidu's statistical code, which can be removed and solved. If you really want to add Baidu statistics, you can replace the following code:

<div style="display:none;">    <script>        var _hmt = _hmt || [];        (function () {            var hm = document.createElement("script");            hm.src = "//hm.baidu.com/hm.js?ab71a8cfda50fbfeb2d3f6ee182af524";            var s = document.getElementsByTagName("script")[0];            s.parentNode.insertBefore(hm, s);        })();    </script></div>

Attachment: apachebench(abw.pressure test tool (windows).rar

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.