A Practical PHP Cache

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 cache. php code is as follows:

 

PHP code
  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. Var $ cachefile;
  9. Var $ cachefilevar;
  10. Function cache (){
  11. // Generate the name of the Cache group for the current page $ this-> cachefilevar and the name $ this-> cachefile
  12. // 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.
  13. $ S = array (".", "/"); $ r = array ("_","");
  14. $ This-> cachefilevar = str_replace ($ s, $ r, $ _ SERVER ["SCRIPT_NAME"]). "_". $ _ GET [_ ActionVar _];
  15. $ This-> cachefile = $ this-> cachefilevar. ".". md5 ($ _ SERVER ["REQUEST_URI"]);
  16. }
  17. // Delete the cache of the current page/module
  18. Function delete (){
  19. // Delete the cache of the current page
  20. $ D = dir (_ CachePath _);
  21. $ Strlen = strlen ($ this-> cachefilevar );
  22. // Return all Cache file groups on the current page
  23. While (false! ==( $ Entry = $ d-> read ())){
  24. If (substr ($ entry, 0, $ strlen) = $ this-> cachefilevar ){
  25. If (! Unlink (_ CachePath _. "/". $ entry) {echo "the Cache directory cannot be written"; exit ;}
  26. }
  27. }
  28. }
  29. // Determine whether or not the Cache has been cached and whether the Cache is required
  30. Function check (){
  31. // If the cache update interval _ ReCacheTime _ is set _
  32. If (_ ReCacheTime _ + 0> 0 ){
  33. // Return the last update time of the current page Cache
  34. $ Var = @ file (_ CachePath _. "/". $ this-> cachefilevar); $ var = $ var [0];
  35. // Delete the Cache file if the update time exceeds the update interval
  36. If (time ()-$ var> _ ReCacheTime _){
  37. $ This-> delete (); $ ischage = true;
  38. }
  39. }
  40. // Return the Cache of the current page
  41. $ File = _ CachePath _. "/". $ this-> cachefile;
  42. // Determine whether the current page Cache exists and whether the Cache function is enabled
  43. Return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange );
  44. }
  45. // Read Cache
  46. Function read (){
  47. // Return the Cache of the current page
  48. $ File = _ CachePath _. "/". $ this-> cachefile;
  49. // Read the content of the Cache file
  50. If (_ CacheEnable _) return readfile ($ file );
  51. Else return false;
  52. }
  53. // Generate Cache
  54. Function write ($ output ){
  55. // Return the Cache of the current page
  56. $ File = _ CachePath _. "/". $ this-> cachefile;
  57. // If the Cache function is enabled
  58. If (_ CacheEnable _){
  59. // Write the output content to the Cache file
  60. $ Fp = @ fopen ($ file, 'w ');
  61. If (! @ Fputs ($ fp, $ output) {echo "template Cache write failed"; exit ;}
  62. @ Fclose ($ fp );
  63. // If the cache update interval _ ReCacheTime _ is set _
  64. If (_ ReCacheTime _ + 0> 0 ){
  65. // Update the last update time of the current page Cache
  66. $ File = _ CachePath _. "/". $ this-> cachefilevar;
  67. $ Fp = @ fopen ($ file, 'w ');
  68. If (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written"; exit ;}
  69. @ Fclose ($ fp );
  70. }
  71. }
  72. }
  73. }
  74. ?>

Usage process:

 

PHP code
  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. $ Template = $ cache-> read ();
  8. } Else {
  9. Ob_start ();
  10. Ob_implicit_flush (0 );
  11. ?>
  12. Page content ....
  13. $ Template = ob_get_contents ();
  14. $ Cache-> write ($ template );
  15. }
  16. ?>

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.


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.