PHP version of the site cache to speed up the way to share _php tutorial

Source: Internet
Author: User
Tags gz file
Description
1, the server caches the compressed files, re-accesses reduce the re-compression time, reduce CPU utilization.
2, by setting the client file cache time, reduce the number of re-requests, can be reduced by more than 85%.
3, image because it is already compressed format, just set the client cache time, do not do compression processing.

How to use:
1, the server must support the Gzip,rewrite feature.
2, add the following code to the "Rewritebase/" line of the. htacess file, as shown in figure
Rewriterule (. *.css$|. *.js$|. *.jpg$|. *.gif$|. *.png$) gzip.php?$1 [L]
3, upload gzip.php to root directory
4, the cache folder is built in the root directory, guaranteed to read and write.


Copy CodeThe code is as follows:
/**
* @author Seraphim
* @copyright 2012
*/
//
function Sendheader ($last _modified, $p _type, $content _length = 0)
{
Set the client cache validity time
Header ("Expires:".) Gmdate ("D, D M Y h:i:s", Time () + 15360000). "GMT");
Header ("cache-control:max-age=315360000");
Header ("Pragma:");
Set Last Modified 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 where GZ files are stored, ensuring writable
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 GZ file path
$ext = Array_pop (Explode ('. ', $filename)); Determine file type information by suffix
$type = "content-type:text/html"; The 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 there is a GZ file
$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))
{
Same as the file modification date in the browser cache, returns 304
Header ("http/1.1 304 not Modified");
Send Client Header
Header ("Content-encoding:gzip");
Sendheader ($gmt _mtime, $type);
}
Else
{
Read GZ file output
$content = file_get_contents ($cache _filename);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
Send data
Echo $content;
}
}
Else
if (file_exists ($filename))
{//does not have a corresponding GZ file
$mtime = Mktime ();
$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
Read file
$content = file_get_contents ($filename);
Remove the blank part
$content = LTrim ($content);
Compress file Contents
$content = Gzencode ($content, 9, $gzip? Force_gzip:force_deflate);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
Send data
Echo $content;
Write file
File_put_contents ($cache _filename, $content);
}
Else
{
Header ("http/1.0 404 Not Found");
}
}
Else
{//processing does not use the output in gzip mode. The principle of basic ibid.
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))
{
Same as the file modification date in the browser cache, returns 304
Header ("http/1.1 304 not Modified");
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
}
Else
{
Read file output
$content = file_get_contents ($filename);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Send data
Echo $content;
}
}
Else
{
Header ("http/1.0 404 Not Found");
}
}
?>

http://www.bkjia.com/PHPjc/325405.html www.bkjia.com true http://www.bkjia.com/PHPjc/325405.html techarticle Description: 1, the server caches the compressed files, re-accesses reduce the re-compression time, reduce CPU utilization. 2, reduce the number of requests by setting the client file cache time ...

  • 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.