MVC Cache 1

Source: Internet
Author: User

MVC Cache

Why does caching, caching really work?

Let's say a scenario we have a home menu layout is basically not a change, if the dynamically generated Web pages are frequently requested and are built with a lot of system resources, how can we improve the response time of this situation? So in this case the cache will work, so we can avoid frequent access to the server to increase the pressure on the server. Now let's go to the MVC page cache, let's look at the principle before using a simple example

So now we are analyzing how to see whether a page has the corresponding caching behavior. The meaning of the parameters inside him.

Cache-control is the cache used to control Web pages. Common values are private, No-cache, Max-age, must-revalidate, etc., default to private

Cache-control

Open a new window (_blank)

Private, No-cache, must-revalidate access the server again
Cache-control:max-age=5 (indicates that access will not go to the server within 5 seconds of accessing this page)

Enter in the Address bar
A value of private or must-revalidate will only access the server on the first visit and will no longer be accessed at a later time.
The value is No-cache, which is accessed every time.
A value of max-age, the access is not repeated until it expires.

Press Back to press the button
The value is private, must-revalidate, Max-age, and is not re-accessed.
A value of No-cache, which is repeatedly accessed every time

Press Refresh to press the button
No matter what the value, it will be accessed repeatedly

Private max-age=0 means that every time you go to the server, if it's not 0, it indicates the cache setting.

Suggestions:

1. For JS and CSS can be independent to a two-level domain name, gzip enabled, and set a longer expiration time

2. For picture Independent to another two level domain name, and set a longer expiration time

For static files (HTML), you can set a slightly longer expiration time (such as 30 days) If you do not update for a long period, and set the time for the update according to your practice.

For dynamic Files (PHP), you can set a short expiration time (for example, 120 seconds)

Note: If you change the contents of the file during the expiration period, you cannot browse or crawl the contents of your changes to the user or spider. This is also the main reason for the short expiration time of dynamic file settings. Cache-control's setting is just to save search engine spider resources, let it crawl more effective content, must not self-defeating.

How to use MVC to build a simple cache first configure this in a Web page

  <caching>      <outputCacheSettings>        <outputCacheProfiles>          <add name= "Index" Duration= "enabled=" true "location=" ServerAndClient "varybyparam=" City,type "/>        </outputcacheprofiles >      </outputCacheSettings>    </caching>

Then write in action [OutputCache (CacheProfile = "Index")] and Response.Cache.SetOmitVaryStar (True This allows for a simple cache

 [OutputCache (cacheprofile = "index")]public      ActionResult Index ()        {           Response.Cache.SetOmitVaryStar (true);            Viewbag.message = "Welcome to ASP. mvc!";            List<person> persons=new list<person>            {               new person () {name= ' Lin ', age=23},               new person () { Name = "Zhang", age=24},               new person () {name= ' Tang ', age = +}            };            JavaScriptSerializer json=new JavaScriptSerializer ();            Viewbag.json = Json. Serialize (persons);            return View ();        }

This is the first visit.

Let's click on F5 to observe the status 304

304 (not modified) The requested webpage has not been modified since the last request. When the server returns this response, the Web page content is not returned. If the page has not changed since the requestor last requested it, you should configure the server to return this response (known as the If-modified-since HTTP header). The server can tell Googlebot that the webpage has not changed since the last crawl, thus saving bandwidth and overhead.

Now you see the benefits of caching. In fact, there are many ways to use caching.

Tomorrow we'll talk about what it is like to configure the cache parameters in MVC. How to use MVC to set the page cache well.

(For reference http://www.ggseo.cn/blog/post/cache-control.html)

MVC Cache 1

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.