A PHP Cache class

Source: Internet
Author: User
  1. cache.inc.php:
  2. Class Cache {
  3. /**
  4. * $dir: Cache file storage Directory
  5. * $lifetime: Cache file validity, in seconds
  6. * $cacheid: Cache file path, including file name
  7. * $ext: Cache file extension (not available), used here for convenient viewing of files
  8. */
  9. Private $dir;
  10. Private $lifetime;
  11. Private $cacheid;
  12. Private $ext;
  13. /**
  14. * destructor, check whether the cache directory is valid, default assignment
  15. */
  16. function __construct ($dir = ", $lifetime =1800) {
  17. if ($this->dir_isvalid ($dir)) {
  18. $this->dir = $dir;
  19. $this->lifetime = $lifetime;
  20. $this->ext = '. PHP ';
  21. $this->cacheid = $this->getcacheid ();
  22. }
  23. }
  24. /**
  25. * Check if the cache is valid
  26. */
  27. Private Function IsValid () {
  28. if (!file_exists ($this->cacheid)) return false;
  29. if (! ( @ $mtime = filemtime ($this->cacheid))) return false;
  30. if (Mktime ()-$mtime > $this->lifetime) return false;
  31. return true;
  32. }
  33. /**
  34. * Write Cache
  35. * $mode = = 0, get page content in browser cache
  36. * $mode = = 1, get the page content in a direct assignment (received via the $content parameter)
  37. * $mode = = 2, get the page content in a local read (fopen ile_get_contents) way (it seems that this way is not necessary)
  38. */
  39. Public Function Write ($mode =0, $content = ") {
  40. Switch ($mode) {
  41. Case 0:
  42. $content = Ob_get_contents ();
  43. Break
  44. Default
  45. Break
  46. }
  47. Ob_end_flush ();
  48. try {
  49. File_put_contents ($this->cacheid, $content);
  50. }
  51. catch (Exception $e) {
  52. $this->error (' Write cache failed! Please check directory permissions! ');
  53. }
  54. }
  55. /**
  56. * Load Cache
  57. * EXIT () to terminate the execution of the original page program after loading the cache, and run the original page program generation cache if the cache is invalid
  58. * Ob_start () Open browser cache to get page content at the end of the page
  59. */
  60. Public function load () {
  61. if ($this->isvalid ()) {
  62. echo "This isCache. ";
  63. Here are two ways to do it?????
  64. Require_once ($this->cacheid);
  65. Echo file_get_contents ($this->cacheid);
  66. Exit ();
  67. }
  68. else {
  69. Ob_start ();
  70. }
  71. }
  72. /**
  73. * Clear Cache
  74. */
  75. Public Function Clean () {
  76. try {
  77. Unlink ($this->cacheid);
  78. }
  79. catch (Exception $e) {
  80. $this->error (' Erase cache file failed! Check directory permissions! ');
  81. }
  82. }
  83. /**
  84. * Get cache file path
  85. */
  86. Private Function Getcacheid () {
  87. return $this->dir.md5 ($this->geturl ()). $this->ext;
  88. }
  89. /**
  90. * Check whether the directory exists or can be created
  91. */
  92. Private Function Dir_isvalid ($dir) {
  93. if (Is_dir ($dir)) return true;
  94. try {
  95. mkdir ($dir, 0777);
  96. }
  97. catch (Exception $e) {
  98. $this->error (' Set cache directory does not exist and failed to create! Please check the directory permissions! ');
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * Get the full URL of the current page
  105. */
  106. Private Function Geturl () {
  107. $url = ";
  108. if (Isset ($_server[' Request_uri ')) {
  109. $url = $_server[' Request_uri ');
  110. }
  111. else {
  112. $url = $_server[' php_self ');
  113. $url. = Empty ($_server[' query_string ')? ': '? '. $_server[' query_string '];
  114. }
  115. return $url;
  116. }
  117. /**
  118. * Output error message
  119. */
  120. Private Function Error ($STR) {
  121. Echo '. $str. ';
  122. }
  123. }
  124. ?>
Copy Code
  1. demo.php:
  2. /*
  3. * Can be freely reproduced use, please keep the copyright information, thank you for using!
  4. * Class Name:cache (for PHP5)
  5. * version:1.0
  6. * Description: Dynamic cache class for controlling the page to automatically generate cache, call cache, update cache, delete cache.
  7. * Author:jiangjun8528@163.com,junin
  8. * Author page:http://blog.csdn.net/sdts/
  9. * Last Modify:2007-8-22
  10. * Remark:
  11. 1. This version is PHP5 version, I do not write PHP4 version, if necessary, please refer to the modification (easier, not so lazy, hehe!).
  12. 2. This version is UTF-8 encoding, if the website uses other encoding, please convert, Windows system with Notepad open Save As, select the appropriate encoding (general ANSI), under Linux use the appropriate editing software or ICONV command line.
  13. 3. Copy and paste the above 2nd is not the tube.
  14. * Some thoughts about caching:
  15. * The fundamental difference between a dynamic cache and a static cache is that it is automatic, and the user's access to the page process is the process of generating the cache, browsing the cache, updating the cache, without manual intervention.
  16. * Static cache refers to the generation of static pages, related operations are generally done in the background of the site, manual operation (that is, manually generated).
  17. */
  18. /*
  19. * Examples of how to use
  20. ------------------------------------Demo1-------------------------------------------
  21. Require_once (' cache.inc.php ');
  22. $cachedir = './cache/'; Set Cache Directory
  23. $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
  24. if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
  25. $cache->load (); Load cache, cache is valid do not execute the following page code
  26. Page code Start
  27. echo Date (' h:i:s JS F ');
  28. Page code End
  29. $cache->write (); First run or cache expiration, generating cache
  30. ------------------------------------Demo2-------------------------------------------
  31. Require_once (' cache.inc.php ');
  32. $cachedir = './cache/'; Set Cache Directory
  33. $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
  34. if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
  35. $cache->load (); Load cache, cache is valid do not execute the following page code
  36. Page code Start
  37. $content = Date (' h:i:s JS F ');
  38. Echo $content;
  39. Page code End
  40. $cache->write (1, $content); First run or cache expiration, generating cache
  41. ------------------------------------Demo3-------------------------------------------
  42. Require_once (' cache.inc.php ');
  43. Define (' cacheenable ', true);
  44. if (cacheenable) {
  45. $cachedir = './cache/'; Set Cache Directory
  46. $cache = new Cache ($cachedir, 10); The default setting for omitting parameters is $cache = new cache ($CACHEDIR);
  47. if ($_get[' cacheact ']! = ' rewrite ')//Here is a trick, through XX. Php?cacheact=rewrite update the cache, and so on, you can also set some other actions
  48. $cache->load (); Load cache, cache is valid do not execute the following page code
  49. }
  50. Page code Start
  51. $content = Date (' h:i:s JS F ');
  52. Echo $content;
  53. Page code End
  54. if (cacheenable)
  55. $cache->write (1, $content); First run or cache expiration, generating cache
  56. */
  57. ?>
Copy Code
Php
  • 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.