Cache code sharing for a php website

Source: Internet
Author: User
Tags php website
Cache code sharing for a php website

  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 period
  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 the last modification 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 for storing the gz file to ensure that the file can be written
  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 the gz file path
  35. $ Ext = array_pop (explode ('.', $ filename); // you can specify the file type based on the suffix.
  36. $ Type = "Content-type: text/html"; // 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 the gz file exists
  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. // The file modification date is the same as that in the browser cache, and 304 is returned.
  70. Header ("HTTP/1.1 304 Not Modified ");
  71. // Send the client header
  72. Header ("Content-Encoding: gzip ");
  73. Sendheader ($ gmt_mtime, $ type );
  74. }
  75. Else
  76. {
  77. // Read the gz file output
  78. $ Content = file_get_contents ($ cache_filename );
  79. // Send the 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. {// No corresponding gz file
  89. $ Mtime = mktime ();
  90. $ Gmt_mtime = gmdate ('d, d m y h: I: S', $ mtime). 'gmt ';
  91. // Read the file
  92. $ Content = file_get_contents ($ filename );
  93. // Remove the blank part
  94. // $ Content = ltrim ($ content );
  95. // Compress the file content
  96. $ Content = gzencode ($ content, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE );
  97. // Send the client header
  98. Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
  99. Header ("Content-Encoding: gzip ");
  100. // Send data
  101. Echo $ content;
  102. // Write a 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. {// The output in Gzip mode is not used for processing. The principle is basically the same as above.
  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. // The file modification date is the same as that in the browser cache, and 304 is returned.
  120. Header ("HTTP/1.1 304 Not Modified ");
  121. // Send the 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 the 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. ?>

Appendix -------------- Description: 1. files that have been compressed are cached on the server. access the files again to reduce the compression time and 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 in the line below "RewriteBase/" of the htacess File: 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.

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.