A PHP site cache code sharing

Source: Internet
Author: User
Tags gz file
  1. /**
  2. * @author Seraphim
  3. * @copyright 2012
  4. * @link http://bbs.it-home.org
  5. */
  6. //
  7. function Sendheader ($last _modified, $p _type, $content _length = 0)
  8. {
  9. Set the client cache validity time
  10. Header ("Expires:".) Gmdate ("D, D M Y h:i:s", Time () + 15360000). "GMT");
  11. Header ("cache-control:max-age=315360000");
  12. Header ("Pragma:");
  13. Set Last Modified Time
  14. Header ("last-modified:".) $last _modified);
  15. Set File type information
  16. Header ($p _type);
  17. Header ("Content-length:".) $content _length);
  18. }
  19. Define (' Abspath ', DirName (__file__). '/');
  20. $cache = true;
  21. $cachedir = ' cache/'; directory where GZ files are stored, ensuring writable
  22. if (Empty ($_server[' query_string '))
  23. Exit ();
  24. $gzip = strstr ($_server[' http_accept_encoding '), ' gzip ');
  25. if (empty ($gzip))
  26. $cache = false;
  27. $key = Array_shift (Explode ('? ', $_server[' query_string '));
  28. $key = Str_replace ('.. /', ', $key);
  29. $filename = Abspath. $key;
  30. $symbol = ' _ ';
  31. $rel _path = Str_replace (Abspath, ", DirName ($filename));
  32. $namespace = Str_replace ('/', $symbol, $rel _path);
  33. $cache _filename = Abspath. $cachedir. $namespace. $symbol. BaseName ($filename).
  34. '. Gz '; Generate GZ file path
  35. $ext = Array_pop (Explode ('. ', $filename)); Determine file type information by suffix
  36. $type = "content-type:text/html"; The default file type
  37. Switch ($ext)
  38. {
  39. Case ' CSS ':
  40. $type = "Content-type:text/css";
  41. Break
  42. Case ' JS ':
  43. $type = "Content-type:text/javascript";
  44. Break
  45. Case ' gif ':
  46. $cache = false;
  47. $type = "Content-type:image/gif";
  48. Break
  49. Case ' jpg ':
  50. $cache = false;
  51. $type = "Content-type:image/jpeg";
  52. Break
  53. Case ' PNG ':
  54. $cache = false;
  55. $type = "Content-type:image/png";
  56. Break
  57. Default
  58. Exit ();
  59. }
  60. if ($cache)
  61. {
  62. if (file_exists ($cache _filename))
  63. {//If there is a GZ file
  64. $mtime = Filemtime ($cache _filename);
  65. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  66. if (Isset ($_server[' http_if_modified_since ') && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
  67. $GMT _mtime))
  68. {
  69. Same as the file modification date in the browser cache, returns 304
  70. Header ("http/1.1 304 not Modified");
  71. Send Client Header
  72. Header ("Content-encoding:gzip");
  73. Sendheader ($gmt _mtime, $type);
  74. }
  75. Else
  76. {
  77. Read GZ file output
  78. $content = file_get_contents ($cache _filename);
  79. Send Client Header
  80. Sendheader ($gmt _mtime, $type, strlen ($content));
  81. Header ("Content-encoding:gzip");
  82. Send data
  83. Echo $content;
  84. }
  85. }
  86. Else
  87. if (file_exists ($filename))
  88. {//does not have a corresponding GZ file
  89. $mtime = Mktime ();
  90. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  91. Read file
  92. $content = file_get_contents ($filename);
  93. Remove the blank part
  94. $content = LTrim ($content);
  95. Compress file Contents
  96. $content = Gzencode ($content, 9, $gzip? Force_gzip:force_deflate);
  97. Send Client Header
  98. Sendheader ($gmt _mtime, $type, strlen ($content));
  99. Header ("Content-encoding:gzip");
  100. Send data
  101. Echo $content;
  102. Write file
  103. File_put_contents ($cache _filename, $content);
  104. }
  105. Else
  106. {
  107. Header ("http/1.0 404 Not Found");
  108. }
  109. }
  110. Else
  111. {//processing does not use the output in gzip mode. The principle of basic ibid.
  112. if (file_exists ($filename))
  113. {
  114. $mtime = Filemtime ($filename);
  115. $gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
  116. if (Isset ($_server[' http_if_modified_since ') && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
  117. $GMT _mtime))
  118. {
  119. Same as the file modification date in the browser cache, returns 304
  120. Header ("http/1.1 304 not Modified");
  121. Send Client Header
  122. Sendheader ($gmt _mtime, $type, strlen ($content));
  123. Header ("Content-encoding:gzip");
  124. }
  125. Else
  126. {
  127. Read file output
  128. $content = file_get_contents ($filename);
  129. Send Client Header
  130. Sendheader ($gmt _mtime, $type, strlen ($content));
  131. Send data
  132. Echo $content;
  133. }
  134. }
  135. Else
  136. {
  137. Header ("http/1.0 404 Not Found");
  138. }
  139. }
  140. ?>
Copy Code

Attached--------------Instructions: 1, the server caches the compressed files, re-access to 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.

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

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