Asp. NET performance optimization Local cache analysis _ Practical skills

Source: Internet
Author: User
Tags varnish
In the Web site development process, often encountered a kind of demand scenario is:

1: The page contains hot news, hot news parts need to update 10 minutes, and the other parts of the entire page will not change within 1 days;

2: The first page of a banner need to be explicit: welcome * * *;

1 of the above scenario, if the entire page cache failure is set to 10 minutes, it is bound to increase performance overhead, so the best strategy is different parts of the page with different cache expiration time. For scenario 2, too, we should not be able to accommodate a banner not to apply caching, so that the entire page does not support caching.

It can be said that if we are in the development of the Web site caching strategy is not support page local caching, the entire architecture is unreasonable.

One: Local caching common solution

For the above requirements, there are several types of solutions:

1, Client Side Includes (CSI): Through frame, iframe, JavaScript, Javacript+ajax and other ways of the content of another page dynamically included. JavaScript libraries, such as the popular jquery, now have better support.

Advantages: It can utilize the mechanism of parallel processing and loading of browser client, and can reduce network transmission time and improve performance by browser caching mechanism, and can reduce server-side pressure by computing on client.

Disadvantages: Search engine optimization problems; JavaScript compatibility issues; Client caching may cause server-side content to be updated and not effective in time;

2. Server Side Includes (SSI):

Advantages: SSI Technology is a universal technology, not subject to specific language constraints, only the Web server or application server support, Ngnix, Apache, Tomcat, JBoss and so on have better support

Disadvantage: SSI is not syntactically capable of directly containing the URLs of other servers (and, of course, it can be implemented by redirect, etc.), so it is relatively inflexible in environments where caching and load balancing are needed to be fully leveraged.

Of course, if you do not use a separate caching server, instead of using Ngnix, you can use Ngnix for SSI and memcached support, and page caching through Nginxhttpssimodule, Nginxhttpmemcachedmodule, However, compared with professional caching servers (such as varnish), Ngnix is only suitable for small and medium sized occasions as a caching server.

3, using asp.net fragment cache

You can use user controls to fragment pages, write cached statements in an ascx file, and not write cached statements in an ASPX file, so that asp.net can only cache the output of an ascx fragment.

Disadvantage: Fragment caching does not support location attributes; The only legitimate place to cache page fragments is the Web server. This is because fragment caching is a new feature in ASP.net, so browsers and proxy servers are not supported. Because it is not a standard for the consortium, proxy servers such as squid and varnish do not support it.

4, Edge Side Includes (ESI):

Edge Side Includes (ESI) and server Side Includes (SSI) and functionality are similar. SSI requires a special file suffix (shtml,inc). ESI can contain remote server files directly through URIs, and ESI is better suited for caching servers, caching entire pages or page fragments, so ESI is especially good for caching. This article is about ESI's way to support local caching.

Benefits: ESI is a standard for the consortium, supported by the current popular caching server Squid,varnish.

II: asp.net implementation of ESI

This article describes the implementation of the ESI local cache. Front page (test1.aspx):
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Test1.aspx.cs" inherits= "WebApplication2.aspx.test1"% >
<%@ Import namespace= "System.Globalization"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<div> here is the local cache </div>
<esi:include src= "test2.aspx"/>
<div> Local Cache End </div>
<%=datetime.now.tostring ("U", Datetimeformatinfo.invariantinfo)%>
</body>

The background of the main page please take a look at the article, the main page has taken a caching policy, that is, the use of the Esi:include logo in the page.
Front desk for included pages (test2.aspx):
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Test2.aspx.cs" inherits= "WebApplication2.aspx.test2"% >
<%@ Import namespace= "System.Globalization"%>
<div>
Pages in the local cache:
<%=datetime.now.tostring ("U", Datetimeformatinfo.invariantinfo)%>
</div>

The background of the included page does not need to be processed, that is, no caching policy is added to it, and the page is real-time.
The varnish configuration file is as follows:
Copy Code code as follows:

Backend Default {
. Host = "192.168.0.77";
. Port = "80";
}
Sub Vcl_fetch {
Remove Beresp.http.set-cookie;
if (req.url ~ "Test1.aspx") {
Esi
}
if (req.url ~ "Test2.aspx") {
return (pass);
}
}
Sub Vcl_recv {
Remove Req.http.Cookie;
#remove req.http.accept-encoding;
#remove req.http.Vary;
}
Sub Vcl_hit {
if (req.http.cache-control~ "No-cache" | | req.http.cache-control~ "Max-age=0" | | req.http.pragma~ "No-cache") {
Set obj.ttl=0s;
return (restart);
}
return (deliver);
}
Sub Vcl_deliver {
if (Obj.hits > 0) {
Set Resp.http.x-cache = "HIT";
} else {
Set Resp.http.x-cache = "MISS";
}
}

The above Vcl_fetch function adds two judgments, referring to the following: If you encounter a test1.aspx to deal with ESI identification, if you encounter test2.aspx, you simply ignore the background IIS processing.
It is noteworthy that the-p option was added to the startup command (this is a varnish problem, please refer to the reference, not listed here):
Varnishd-a: 8011-t: 8088-f c:/varnish/etc/default.vcl-p esi_syntax=0x1-s file,c:/varnish/var/cache,100m
Three: Effect
After starting the varnish, we found that For test2.aspx, because we use ESI to contain it, and the test2.aspx is not cached, the contents of the test1.aspx are not changed with each refresh during the test1.aspx cache lifetime, but the contained test2.aspx areas are refreshed in real time.
Reference (most of the first section comes from reference text):
https://www.varnish-cache.org/trac/ticket/352

http://cd34.com/blog/infrastructure/no-esi-processing-first-char-not/

Http://hi.baidu.com/chuanliang2007/blog/item/075f67963e20f315d31b7035.html

Http://www.w3.org/TR/esi-lang

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.