[Posting] Using gzip caching technology to accelerate webpage Loading

Source: Internet
Author: User
Tags xslt

Gzip is a technology that uses the compression technology on HTTP. Currently, Gzip technology is supported by mainstream browsers, which saves traffic by consuming server performance.

It is easy to say, it is to let the server compress the file content and send it to the browser. After the browser accepts the file, it decompress it and then uses it for parsing. In this case, it saves network traffic costs, there are a lot of text transmitted by the browser, and text compression can usually reach 20% of the original file. In this way, the webpage loading speed can be improved.

 

When I test the cache file generation, I compress the text to gzip to save it. In this way, the size of my cache file changes from around 15 kb to 2-3 kb, this greatly saves the website space. When a user accesses this file, I make a judgment. If the browser supports gzip, I will directly return the byte stream of the cached file to the browser, if the browser does not support it (this is a relatively low possibility), I need to decompress the file and return the extracted byte stream to the client.
As we can see from the above, I did save the website space and traffic, and because more than 95% of the current browsers (conservatively estimated, because I didn't hear which one is not supported) Support gzip, in fact, the performance of the website's server has been improved, which is known as the benefit of 51.
How can I determine whether the browser supports gzip? When sending an HTTP request, the browser will have a header field named accept-encoding, which represents the returned encoding format supported by the browser. Usually the following values (deflate stands for plaintext ):
Accept-encoding: gzip, deflate
How does the browser determine whether the data returned by the server is in plaintext or GZIP format? It also uses the head field of the server, for example:
Content-encoding: Gzip
If the value is not gzip, it indicates plain text, and this field may also represent the encoding of text files, such as UTF-8
The following describes how to use gzip. First, the write process of the cache:
Filestream = file. Create (PATH); // create a cache file
Stream writer = new gzipoutputstream (filestream); // writes cached file streams using Gzip
Xsltargumentlist list = new xsltargumentlist (); // my website uses XML + XSLT to generate pages.
XSLT. Transform (XML, list, writer, null); // transmits the content generated by XSLT to the gzip compression program
Writer. Close (); // finish writing
Filestream. Close (); // close the file
The second is the process of reading the cache:
If (acceptencoding! = NULL & acceptencoding. indexof ("gzip")> = 0) // determine whether Gzip is supported by the HTTP head of the request.
{// If Gzip is supported, GZIP format is returned.
Response. addheader ("content-encoding", "gzip"); // notify the browser that the file is compressed by gzip.
Response. transmitfile (PATH); // directly return the file
}
Else
{// Return the plaintext format. In this case, we need to extract the file content from the ZIP file without sending the content-encoding head, because the content is in plaintext by default.
Stream reader = new gzipinputstream (file. openread (PATH); // open the file
Byte [] buffer = new byte [1024]; // create a file read Cache
Int P;
While (P = reader. Read (buffer,)> 0) // The content is returned every time the corresponding cache is read.
{
Response. outputstream. Write (buffer, 0, P );
Response. Flush ();
}
}

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.