Use ASP. NET technology to optimize local cache

Source: Internet
Author: User

Use ASP. NET technology to optimize local cache

In terms of ASP. NET performance optimization, local cache is used to solve the problem that a specific region needs to be updated frequently, while updates in other regions are not obvious.

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:

The following is a code snippet:
<% @ 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">

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:

The following is a code snippet:
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "test2.aspx. CS "inherits =" webapplication2.aspx. test2 "%> <% @ import namespace =" system. globalization "%> <div>

Pages in partial cache:

The following is a code snippet:
<% = 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:

The following is a code snippet:
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 cl_fetch function adds two judgments, which indicate that if test1.aspx is used, the ESI identifier is processed. If test2.aspx is used, the backend IIS is used for processing directly.

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

The following is a code snippet:
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.

Http://vivavillaverde.es/bin/pg/blog/read/8904/japans-population-will-plummet
Http://ingasti.com/terraza/pg/blog/Chan/read/66710/referendum-to-solve-russianjapanese-territorial-dispute
Http://test.elgg.cz/blog/view/28264/european-debt-crisis-firewall-provoke-controversy
Http://www.yourlastmessage.com/pg/blog/read/86694/european-debt-crisis-will-become-optimistic
Http://tkweb2.com/unitingcreatives/pg/blog/read/1096636/russias-lease-one-million-hectares-of-farmland
Http://network.exclusivebusiness.org/pg/blog/read/371205/5-people-were-arrested-wiretapping-case

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.