PHP Cache class for websites-PHP Tutorial

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


Class usage:

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


Bytes. Currently, many CMS content management systems frequently use caching mechanisms to improve their systems...

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.