PHP website cache Acceleration

Source: Internet
Author: User
Tags gz file php website

Note:
1. the compressed files are cached on the server, and the re-access reduces the re-compression time to reduce the CPU usage.
2. You can set the Client File Cache Time to reduce the number of re-requests by more than 85%.
3. Because the image is already in the compression format, it only sets the client cache time and does not compress the image.

Usage:
1. The server must support the gzip and Rewrite functions.
2. Add the following code under the RewriteBase/line of the. htacess file, as shown in the figure below.
RewriteRule (. *. css $ |. *. js $ |. *. jpg $ |. *. gif $ |. *. png $) gzip. php? $1 [L]
3. Upload gzip. php to the root directory.
4. Create a cache folder in the root directory to ensure read/write.


Copy codeThe Code is as follows:
<? Php
/**
* @ Author Seraphim
* @ Copyright 2012
*/
// <! -- Common subprograms for returning headers -->
Function sendheader ($ last_modified, $ p_type, $ content_length = 0)
{
// Set the Client Cache Validity Period
Header ("Expires:". gmdate ("D, d m y h: I: s", time () + 15360000). "GMT ");
Header ("Cache-Control: max-age = 315360000 ");
Header ("Pragma :");
// Set the last modification time
Header ("Last-Modified:". $ last_modified );
// Set file type information
Header ($ p_type );
Header ("Content-Length:". $ content_length );
}
Define ('abspath', dirname (_ file __).'/');
$ Cache = true;
$ Cachedir = 'cache/'; // directory for storing the gz file to ensure that the file can be written
If (empty ($ _ SERVER ['query _ string'])
Exit ();
$ Gzip = strstr ($ _ SERVER ['HTTP _ ACCEPT_ENCODING '], 'gzip ');
If (empty ($ gzip ))
$ Cache = false;
$ Key = array_shift (explode ('? ', $ _ SERVER ['query _ string']);
$ Key = str_replace ('../', '', $ key );
$ Filename = ABSPATH. $ key;
$ Symbol = '_';
$ Rel_path = str_replace (ABSPATH, '', dirname ($ filename ));
$ Namespace = str_replace ('/', $ symbol, $ rel_path );
$ Cache_filename = ABSPATH. $ cachedir. $ namespace. $ symbol. basename ($ filename ).
'.Gz '; // generate the gz file path
$ Ext = array_pop (explode ('.', $ filename); // you can specify the file type based on the suffix.
$ Type = "Content-type: text/html"; // default file type
Switch ($ ext)
{
Case 'css ':
$ Type = "Content-type: text/css ";
Break;
Case 'js ':
$ Type = "Content-type: text/javascript ";
Break;
Case 'gif ':
$ Cache = false;
$ Type = "Content-type: image/gif ";
Break;
Case 'jpg ':
$ Cache = false;
$ Type = "Content-type: image/jpeg ";
Break;
Case 'png ':
$ Cache = false;
$ Type = "Content-type: image/png ";
Break;
Default:
Exit ();
}
If ($ cache)
{
If (file_exists ($ cache_filename ))
{// If the gz file exists
$ Mtime = filemtime ($ cache_filename );
$ Gmt_mtime = gmdate ('d, d m y h: I: s', $ mtime). 'gmt ';
If (isset ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE ']) & array_shift (explode ('; ', $ _ SERVER ['HTTP _ IF_MODIFIED_SINCE']) =
$ Gmt_mtime ))
{
// The file modification date is the same as that in the browser cache, and 304 is returned.
Header ("HTTP/1.1 304 Not Modified ");
// Send the client header
Header ("Content-Encoding: gzip ");
Sendheader ($ gmt_mtime, $ type );
}
Else
{
// Read the gz file output
$ Content = file_get_contents ($ cache_filename );
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
Header ("Content-Encoding: gzip ");
// Send data
Echo $ content;
}
}
Else
If (file_exists ($ filename ))
{// No corresponding gz File
$ Mtime = mktime ();
$ Gmt_mtime = gmdate ('d, d m y h: I: s', $ mtime). 'gmt ';
// Read the file
$ Content = file_get_contents ($ filename );
// Remove the blank part
// $ Content = ltrim ($ content );
// Compress the File Content
$ Content = gzencode ($ content, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE );
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
Header ("Content-Encoding: gzip ");
// Send data
Echo $ content;
// Write a file
File_put_contents ($ cache_filename, $ content );
}
Else
{
Header ("HTTP/1.0 404 Not Found ");
}
}
Else
{// The output in Gzip mode is not used for processing. The principle is basically the same as above.
If (file_exists ($ filename ))
{
$ Mtime = filemtime ($ filename );
$ Gmt_mtime = gmdate ('d, d m y h: I: s', $ mtime). 'gmt ';
If (isset ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE ']) & array_shift (explode ('; ', $ _ SERVER ['HTTP _ IF_MODIFIED_SINCE']) =
$ Gmt_mtime ))
{
// The file modification date is the same as that in the browser cache, and 304 is returned.
Header ("HTTP/1.1 304 Not Modified ");
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
Header ("Content-Encoding: gzip ");
}
Else
{
// Read file output
$ Content = file_get_contents ($ filename );
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
// Send data
Echo $ content;
}
}
Else
{
Header ("HTTP/1.0 404 Not Found ");
}
}
?>

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.