Leverage caching to improve website performance

Source: Internet
Author: User
Tags http post send cookies website performance

ensure consistency when using URLs. browsers cache resources based on URLs. When the URL changes, the browser receives a new version of the resource from the source server. Changes to query string parameters are also treated as URL changes. For example, "/default.aspx" is cached in the browser, and if you request "/default.aspx?123", the browser will get a new version from the server. For the response of this new URL, if you return the correct cache header, it will still be cached. In this case, the query string is changed to resemble "/default.aspx?456", then the server will return a new version. So, when you want to respond to a cache, make sure you use consistent URLs everywhere. On the home page, if you request a file with a URL of "/welcome.gif", make sure that the same URL is used on the other page when you request the file. A common mistake is that the "www" subdomain is sometimes omitted from the URL. Www.nowamagic.net/default.aspx and nowamagic.net/default.aspx are different, and the two will be cached separately. Gambling King Casino

static content is cached longer. static content can be cached longer, such as one months. If you are considering should only cache a few days, so that when you modify the file, users can quickly get to the new version, then you are wrong. If a file is cached by setting the expiration header (expires header), when you update it, the new user can immediately get to the latest version, while the old user can only see the old content until it expires on the browser side. So as long as you're caching the static file with an expired header, set the value as big as possible. For example, you have cached a file by setting the expiration header value to 3 days, a user obtains the file today, and saves it in the cache until three days later, and another user gets the file tomorrow and caches it until three days after tomorrow. If you change the file the day after tomorrow, the first user will see it on the fourth day, and the second user will see it on the fifth day. Therefore, different users will see different versions of the file. As a result, setting a low value is not helpful to ensure that all users get the latest version in the shortest amount of time. You will have to make sure that everyone gets the exact same file immediately by modifying the URL of the file. You can use IIS Manager to set an expiration header for a static file, which will show you how to set it later.

use a cache-friendly folder structure. Store the content that you want to cache in a common folder. For example, store all the pictures of your site in the "/static" folder instead of storing them in separate subfolders. This will help you use consistent URLs throughout your site, because you can use "/static/images/somefile.gif" from anywhere. Later, we'll learn that when you put a static cache file inside a common root folder, it's easy to move to a content delivery network.

reuse the same graphics files. sometimes we store the same graphics files in several different virtual directories so that we can write a short path. For example, you have a indicator.gif file in the root directory, some subdirectories, and a CSS directory. This is done because you don't have to worry about path problems that are accessed from different places, you just need to use the file name as a relative URL. This does not help the cache. Each copy of the file is cached separately on the browser side. Therefore, you should put all the image files in the project into the "static" folder under the root directory, remove duplicates, and use the same URL in all pages and CSS files.

change the file name to expire the cache . When you change a static file, do not just update the file itself, because it is already cached in the client's browser. You need to change the file name and update all the references everywhere so that the browser gets to the new version. You can also store the file name in a database or configuration file and generate the URL dynamically through data binding. In this way, you can change the URL in one place and make the entire site immediately available for update.

Use the version number to access the static file. If you don't want to confuse static folders because you want to save different copies of the same files, you can use query strings to differentiate between versions of the same file. For example, a GIF file can be accessed with a combination of a virtual query string, such as "/static/images/indicator.gif?v=1". When you change the indicator.gif, you can overwrite the original file and then update all references to the file to "/static/images/indicator.gif?v=2". This allows you to repeatedly modify the same file and then use the new version number to update all references to the file.

store cacheable files in different domains . It's always a good idea to store static content in different domains. First, the browser can open additional concurrent connections to download static files. Another benefit is that you do not need to send cookies to static files. If you put a static file in the same domain as your web app, the browser sends all of the ASP. NET cookies and all other cookies generated by your web app. This makes the request header unnecessarily large, wasting bandwidth. You do not need to send these cookies when accessing static files. Therefore, if you put static files in different domains, those cookies will not be sent. For example, put a static file in the www.staticcontent.com domain and run your site in the www.dropthings.com domain. Different domains and means that they must be completely different sites. It may be just an individual name, but physically and the web app shares the same path.

Secure Sockets (SSL) are not cached and are used sparingly. Any SSL-processed content is not cached. Therefore, you need to place static content outside of SSL. In addition, you should try to apply SSL to some security pages, such as the login page or the payment page. The rest should use regular HTTP instead of SSL. SSL encrypts requests and responses, thus increasing the additional burden on the server. The encrypted content is also larger than the original content, thus occupying more bandwidth.

HTTP Post requests are never cached . The cache is only relative to HTTP GET requests. HTTP Post requests are never cached. Therefore, any form of Ajax invocation, if it wants to be cached, needs to be called in the form of an HTTP GET.

generates a content-length response header (Content-length reponse header). when you provide content dynamically through a Web services call or HTTP handlers, make sure that the Content-length header is generated. The browser will know how much content to download by looking at the Content-lenght header of the response, so it will have several optimizations to improve the download speed. If this header information is available, the browser will use the persistent connection more efficiently. This will prevent the browser from opening a new connection for each request. In the absence of Content-lenght header information, the browser does not know how much content to receive from the server, so as long as it gets the byte stream from the server side, it remains open until the connection is closed. As a result, you lose the benefits of continuous connectivity, which can greatly shorten the download time of small files, such as CSS, javascripts, and picture files.

How to configure the caching of static content in IIS . In IIS Manager, the Web Site Properties dialog box has an HTTP Headers page where you can define an expiration header for all requests that are processed by IIS. You can define the content to expire immediately, or expire after a few days, or expire on a specific date. The second option (Expires after) uses a relative expiration, not an absolute expiration. This is useful because it works on every request. Regardless of who requested a static file, IIS calculates the expiration date based on the number of days/months of the expire after option. For dynamic pages that are handled by ASP., a handler can modify the value of the expired header to override the default settings for IIS.

Leverage caching to improve website performance

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.