[Original author]: Omar [original article link]: making best use of cache for high performance website
Ensure consistency when using URLsThe browser caches resources based on URLs. When the URL changes, the browser obtains a new version of the resource from the source server. Changes to the query string parameters are also considered as changes to the URL. For example, "/default. aspx" is cached in the browser. If you requested "/default. aspx? 123 ", the browser will get a new version from the server. For the response to this new URL, if you return the correct cache header, it will still be cached. In this case, change the query string to something similar to "/default. aspx? 456 ", the server returns a new version. Therefore, when you want to get a response cached, make sure that you use consistent URLs everywhere. On the homepage, If you request a file whose URL is "/welcome.gif", make sure that the same URL is used when you request the file on other pages. A common error is that the "www" subdomain is sometimes omitted from the URL. Bytes.
Static content will be cached longerStatic content can be cached for a longer period, for example, a month. If you are considering caching the file for only a few days so that after you modify the file, the user can quickly get the new version, then you are wrong. If a file is cached by setting an expiration header (Expires header), when you update it, new users can get the latest version immediately, the old user can only see the old content until it expires on the browser side. Therefore, as long as you are using an expired header to cache static files, the larger the value, the better. For example, if you have set the expiration header value to three days to cache a file, a user will get the file today and save it in the cache until three days later; another user will get the file tomorrow and cache it for 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 does not help to ensure that all users obtain the latest version in the shortest time. You will have to modify the File URL to ensure that all users can immediately obtain the same file. You can use the IIS manager to set an expiration header for a static file. The following content describes how to set the header.
Use a cache-friendly folder structureStore the content to be cached in a common folder. For example, store all the images on your website in the "/static" folder instead of storing the images in different subfolders. This will help you use consistent URLs across the entire website, because from anywhere you can use "/static/images/somefile.gif ". Later, we will learn that it will be easy to transfer static cache files to a content transfer network after they are placed in a common root folder.
Reuse the same Graphic FileSometimes we store the same graphic file in several different virtual directories so that we can write very short paths. For example, you have an indicator.gif file in the root directory, subdirectories, and CSS directories. This is because you do not have to worry about accessing the path from different places. You only need to use the file name as the relative URL. This does not help caching. Each copy of the file is cached on the browser. Therefore, you should collect all the image files in the project to the "static" folder under the root directory, and use the same URL in all pages and CSS files except for repeated ones.
Change the file name to make the cache expireWhen you change a static file, do not just update the file because it is already cached in the browser of the client. You need to change the file name and update all references so that the browser can get the new version. You can also store the file name in the database or configuration file and dynamically generate a URL through data binding. In this way, you can change the URL in one place, so that the entire site is updated immediately.
Use the version number to access static filesIf you do not want to make static folders messy because you want to save different copies of the same file, you can use query strings to differentiate different versions of the same file. For example, a GIF file can be accessed in combination with a virtual query string, such as "/static/images/indicator.gif? V = 1 ". When you change indicator.gif, You can overwrite the original file and update all references to this file to "/static/images/indicator.gif? V = 2 ". In this way, you can modify the same file repeatedly and use the new version number to update all references to this file.
Store cacheable files in different domainsIt is always a good way to store static content in different domains. First, the browser can open another concurrent connection to download static files. Another benefit is that you do not need to send cookies to static files. If you place static files in the same domain as your web application, the browser will send all ASP. NET cookies and all other cookies generated by your web application. This makes the request header unnecessary and wastes bandwidth. You do not need to send these cookies when accessing static files. Therefore, if you place static files in different domains, those cookies will not be sent. For example, place a static file in the domain www.staticcontent.com and run your website in the domain www.dropthings.com. Different domains mean that they must be completely different websites. It may be just an alias, but physically shares the same path with the Web application.
Secure Sockets (SSL) is not cached and should be used as little as possibleNo SSL-processed content will be cached. Therefore, you need to put static content out of SSL. In addition, you should try to apply SSL to some security pages, such as logon pages or payment pages. 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 larger than the original content, thus occupying more bandwidth.
Http post requests are never cachedThe cache is only relative to the http get request. Http post requests are never cached. Therefore, any form of Ajax call, if you want to be cached, needs to be called in the form of http get.
Generate content length Response Header (Content-Length reponse header)When you use web services or HTTP handlers to dynamically provide content, ensure that the Content-Length header is generated. The browser will know how much content will be downloaded by viewing the response content-lenght header, so that it will have multiple optimization solutions to increase the download speed. If this header information is available, the browser will make more effective use of persistent connections. This will prevent the browser from opening a new connection for each request. Without the content-lenght header information, the browser does not know how much content to receive from the server, so as long as it obtains the byte stream from the server, the connection will remain open until the connection is closed. Therefore, you lose the benefits of continuous connection. It can greatly shorten the download time of some small files, such as CSS, javascripts, and image files.
How to configure static content caching in IISIn the IIS manager, the website Properties dialog box has an "HTTP headers" page, where you can define an expiration header for all requests processed by IIS. Then you can define that the content expires immediately, expire in a few days, or expire on a specific date. The second option (expires after) uses relative expiration instead of absolute expiration. This is very useful because it works for every request. No matter who requests a static file, IIS calculates the expiration date based on the number of days/months of the expire after option.
For dynamic pages processed by ASP. NET, a handler can modify the value of the expired header to overwrite the default settings of IIS.