PHP cache for websites

Source: Internet
Author: User

Cache is widely used in actual use, which can reduce access to the server database and improve the running speed. Currently, many CMS Content Management systems frequently use caching mechanisms to improve system operation efficiency. The following is a well-written cache class. You can refer to the caching mechanism and writing method.

The cache. php code is as follows:

[Php]View plaincopy
  1. <?
  2. /*
  3. Constants that need to be defined in advance:
  4. _ CachePath _ template cache path
  5. _ CacheEnable _ whether the automatic cache mechanism is enabled. If it is undefined or empty, the automatic cache mechanism is disabled.
  6. _ ReCacheTime _ automatic cache re-caching interval, in seconds. If it is undefined or empty, automatic cache re-caching is disabled.
  7. */
  8. Class cache
  9. {
  10. Var $ cachefile;
  11. Var $ cachefilevar;
  12. Function cache ()
  13. {
  14. // Generate the name of the Cache group for the current page $ this-> cachefilevar and the name $ this-> cachefile
  15. // Different dynamic page parameters correspond to different Cache files, but all the Cache files on each dynamic page have the same file name, but the extensions are different.
  16. $ S = array (".", "/"); $ r = array ("_","");
  17. $ This-> cachefilevar = str_replace ($ s, $ r, $ _ SERVER ["SCRIPT_NAME"]). "_". $ _ GET [_ ActionVar _];
  18. $ This-> cachefile = $ this-> cachefilevar. ".". md5 ($ _ SERVER ["REQUEST_URI"]);
  19. }
  20. // Delete the cache of the current page/Module
  21. Function delete ()
  22. {
  23. // Delete the cache of the current page
  24. $ D = dir (_ CachePath _);
  25. $ Strlen = strlen ($ this-> cachefilevar );
  26. // Return all Cache file groups on the current page
  27. While (false! ==( $ Entry = $ d-> read ()))
  28. {
  29. If (substr ($ entry, 0, $ strlen) = $ this-> cachefilevar)
  30. {
  31. If (! Unlink (_ CachePath _. "/". $ entry) {echo "the Cache directory cannot be written"; exit ;}
  32. }
  33. }
  34. }
  35. // Determine whether or not the Cache has been cached and whether the Cache is required
  36. Function check ()
  37. {
  38. // If the cache Update Interval _ ReCacheTime _ is set _
  39. If (_ ReCacheTime _ + 0> 0)
  40. {
  41. // Return the Last Update Time of the current page Cache
  42. $ Var = @ file (_ CachePath _. "/". $ this-> cachefilevar); $ var = $ var [0];
  43. // Delete the Cache file if the update time exceeds the Update Interval
  44. If (time ()-$ var> _ ReCacheTime _)
  45. {
  46. $ This-> delete (); $ ischage = true;
  47. }
  48. }
  49. // Return the Cache of the current page
  50. $ File = _ CachePath _. "/". $ this-> cachefile;
  51. // Determine whether the current page Cache exists and whether the Cache function is enabled
  52. Return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange );
  53. }
  54. // Read Cache
  55. Function read ()
  56. {
  57. // Return the Cache of the current page
  58. $ File = _ CachePath _. "/". $ this-> cachefile;
  59. // Read the content of the Cache file
  60. If (_ CacheEnable _) return readfile ($ file );
  61. Else return false;
  62. }
  63. // Generate Cache
  64. Function write ($ output)
  65. {
  66. // Return the Cache of the current page
  67. $ File = _ CachePath _. "/". $ this-> cachefile;
  68. // If the Cache function is enabled
  69. If (_ CacheEnable _)
  70. {
  71. // Write the output content to the Cache file
  72. $ Fp = @ fopen ($ file, 'w ');
  73. If (! @ Fputs ($ fp, $ output) {echo "template Cache write failed"; exit ;}
  74. @ Fclose ($ fp );
  75. // If the cache Update Interval _ ReCacheTime _ is set _
  76. If (_ ReCacheTime _ + 0> 0)
  77. {
  78. // Update the Last Update Time of the current page Cache
  79. $ File = _ CachePath _. "/". $ this-> cachefilevar;
  80. $ Fp = @ fopen ($ file, 'w ');
  81. If (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written"; exit ;}
  82. @ Fclose ($ fp );
  83. }
  84. }
  85. }
  86. }
  87. ?>


 

 

Class usage:

[Php]View plaincopy
  1. <? Php
  2. Define ("_ CachePath _", "./cache /");
  3. Define ("_ CacheEnable _", "1 ");
  4. Define ("_ ReCacheTime _", "43200 ");
  5. Include ('cache. php ');
  6. $ Cache = new cache ();
  7. If ($ cache-> check ())
  8. {
  9. $ Template = $ cache-> read ();
  10. }
  11. Else
  12. {
  13. Ob_start ();
  14. Ob_implicit_flush (0 );
  15. ?>
  16. Page content ....
  17. <? Php
  18. $ Template = ob_get_contents ();
  19. $ Cache-> write ($ template );
  20. }
  21. ?>


 

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.