ASP. NET performance optimization-local cache Analysis

Source: Internet
Author: User
Tags varnish

During website development, the following requirements are often met:

1: The page contains hot news. The hot news part needs to be updated every 10 minutes, and the rest of the page will not change within one day;

2: a banner on the homepage needs to be explicitly displayed: Welcome ***;

In scenario 1 above, if the cache of the entire page fails to be set to 10 minutes, it will inevitably increase the performance overhead. Therefore, the best strategy is to use different cache invalidation durations for different parts of the page. The same applies to scenario 2. We should not make the entire page not support caching in order to accommodate a BANNER's inability to apply the cache.

It can be said that if our cache policy during website development does not support partial page caching, the entire architecture is unreasonable.

I. common solutions for local cache

There are several solutions to the above requirements:

1. Client Side Events des (CSI): dynamically Includes the content of another page through frame, iframe, javascript, javacript + ajax, etc. Javascript libraries such as jquery are now well supported.

Advantages: the browser client can be used for Parallel Processing and loading; the browser cache mechanism can reduce network transmission time and improve performance; the computing is placed on the client, which can reduce the pressure on the server.

Disadvantages: search engine optimization, javascript compatibility, client cache, and other security risks

2. Server Side encryption DES (SSI ):

Advantages: SSI technology is a general technology and is not restricted by specific languages. It only requires support from Web servers or application servers. Ngnix, Apache, Tomcat, and Jboss all provide better support for this technology.

Disadvantage: SSI syntax cannot directly contain URLs of other servers (of course, it can also be implemented through redirect and other alternatives ), therefore, it is not flexible to make full use of the cache and Server Load balancer.

Of course, if you do not use a separate cache server, but use Ngnix to support SSI and Memcached, you can also implement page caching through NginxHttpSsiModule and NginxHttpMemcachedModule, however, compared with professional cache servers (such as Varnish), Ngnix is only suitable for small and medium sized applications.

3. Use ASP. NET fragment Cache

You can use the user control to segment pages and write cache statements to the ascx file instead of the aspx file. In this way, ASP. NET can only cache the output of ascx fragments.

Disadvantage: Segment caching does not support the Location feature. The only valid part of the cached page segment is the web server. This is because fragment caching is a new feature in ASP. NET, which is not supported by browsers and proxies. Because it is not W3C standard, proxy servers such as SQUID and VARNISH do not support it.

4. Edge Side primary des (ESI ):

Edge Side primary des (ESI) and Server Side primary des (SSI) are similar in functionality. SSI requires a special file suffix (shtml, inc ). ESI can directly include remote server files through Uris. ESI is more suitable for caching the entire page or page segment on the cache server. Therefore, ESI is particularly suitable for caching. This article introduces the ESI method to support local cache.

Advantages: ESI is a W3C standard and is supported by the popular Cache Server SQUID and Varnish.

Ii. ASP. NET Implementation of ESI

This article describes the implementation of ESI local cache. Homepage (test1.aspx) Foreground:
Copy codeThe Code is 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Div> local cache </div>
<Esi: include src = "test2.aspx"/>
<Div> local cache ends </div>
<% = DateTime. Now. ToString ("U", DateTimeFormatInfo. InvariantInfo) %>
</Body>
</Html>

For details about the background of the home page, refer to the previous article and adopt a cache policy for the home page, that is, use the esi: include identifier on the page.
Foreground of the contained page (test2.aspx:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "test2.aspx. cs" Inherits = "WebApplication2.aspx. test2" %>
<% @ Import Namespace = "System. Globalization" %>
<Div>
Pages in partial cache:
<% = DateTime. Now. ToString ("U", DateTimeFormatInfo. InvariantInfo) %>
</Div>

The background of the contained page does not need to be processed, that is, it does not add any Cache Policy for it. The page is real-time.
The VARNISH configuration file is as follows:
Copy codeThe Code is 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 = 0 s;
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 preceding vcl_fetch function adds two judgments, that is, if test1.aspx is encountered, the esi identifier is processed. If test2.aspx is encountered, the backend IIS is directly processed.
It is worth noting that the-p option is added to the startup command (this is a small varnish problem. For more information, see here. This is not a table ):
Varnishd-a: 8011-T: 8088-f c:/varnish/etc/default. vcl-p esi_syntax = 0x1-s file, c:/varnish/var/cache, 100 M
Iii. Effect
After varnish was started, we found that for test2.aspx, because we used esi to include it, And test2.aspx was not cached, so during the cache Validity Period of test1.aspx, with each refresh, the content of test1.aspx does not change, but the test2.aspx region is refreshed in real time.
Reference (the first section is mostly 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.