One more cache class.

Source: Internet
Author: User
Cache class
  1. /*
  2. * Cache class Caches
  3. * Author: More rookie
  4. * Date Created: 2006-05-05
  5. Instance
  6. Include ("cache.php");
  7. $cache = new cache (30);
  8. $cache->cachecheck ();
  9. echo Date ("y-m-d h:i:s");
  10. $cache->caching ();
  11. */
  12. Class Cache {
  13. Cache Directory
  14. var $cacheRoot = "./cache/";
  15. Cache update time seconds, 0 for no cache
  16. var $cacheLimitTime = 0;
  17. Cache file Name
  18. var $cacheFileName = "";
  19. Cache extension
  20. var $cacheFileExt = "PHP";
  21. /*
  22. * Constructor function
  23. * int $cacheLimitTime Cache update Time
  24. */
  25. function Cache ($cacheLimitTime) {
  26. if (Intval ($cacheLimitTime))
  27. $this->cachelimittime = $cacheLimitTime;
  28. $this->cachefilename = $this->getcachefilename ();
  29. Ob_start ();
  30. }
  31. /*
  32. * Check if the cache file is within the set update time
  33. * Return: If the contents of the file are returned within the update time, the reverse returns the failure
  34. */
  35. function Cachecheck () {
  36. if (file_exists ($this->cachefilename)) {
  37. $cTime = $this->getfilecreatetime ($this->cachefilename);
  38. if ($cTime + $this->cachelimittime > Time ()) {
  39. Echo file_get_contents ($this->cachefilename);
  40. Ob_end_flush ();
  41. Exit
  42. }
  43. }
  44. return false;
  45. }
  46. /*
  47. * Cache files or output static
  48. * String $staticFileName static file name (with relative path)
  49. */
  50. function caching ($staticFileName = "") {
  51. if ($this->cachefilename) {
  52. $cacheContent = Ob_get_contents ();
  53. Echo $cacheContent;
  54. Ob_end_flush ();
  55. if ($staticFileName) {
  56. $this->savefile ($staticFileName, $cacheContent);
  57. }
  58. if ($this->cachelimittime)
  59. $this->savefile ($this->cachefilename, $cacheContent);
  60. }
  61. }
  62. /*
  63. * Clear Cache files
  64. * String $fileName Specify file name (with function) or all (all)
  65. * Return: Clear successful return true, reverse return false
  66. */
  67. function ClearCache ($fileName = "All") {
  68. if ($fileName! = "All") {
  69. $fileName = $this->cacheroot. Strtoupper (MD5 ($fileName)). ".". $this->cachefileext;
  70. if (file_exists ($fileName)) {
  71. Return @unlink ($fileName);
  72. }else return false;
  73. }
  74. if (Is_dir ($this->cacheroot)) {
  75. if ($dir = @opendir ($this->cacheroot)) {
  76. while ($file = @readdir ($dir)) {
  77. $check = Is_dir ($file);
  78. if (! $check)
  79. @unlink ($this->cacheroot. $file);
  80. }
  81. @closedir ($dir);
  82. return true;
  83. }else{
  84. return false;
  85. }
  86. }else{
  87. return false;
  88. }
  89. }
  90. /*
  91. * Generate cache file names based on current dynamic files
  92. */
  93. function Getcachefilename () {
  94. Return $this->cacheroot. Strtoupper (MD5 ($_server["Request_uri")). ".". $this->cachefileext;
  95. }
  96. /*
  97. * Cache file Settling time
  98. * String $fileName Cache file name (with relative path)
  99. * Return: File generation time in seconds, file does not exist return 0
  100. */
  101. function Getfilecreatetime ($fileName) {
  102. if (! Trim ($fileName)) return 0;
  103. if (file_exists ($fileName)) {
  104. Return Intval (Filemtime ($fileName));
  105. }else return 0;
  106. }
  107. /*
  108. * Save File
  109. * String $fileName file name (with relative path)
  110. * String $text file contents
  111. * Return: Successfully returned ture, failed return false
  112. */
  113. function SaveFile ($fileName, $text) {
  114. if (! $fileName | |! $text) return false;
  115. if ($this->makedir (dirname ($fileName))) {
  116. if ($fp = fopen ($fileName, "w")) {
  117. if (@fwrite ($FP, $text)) {
  118. Fclose ($FP);
  119. return true;
  120. }else {
  121. Fclose ($FP);
  122. return false;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. /*
  129. * Continuous construction of the catalogue
  130. * String $dir directory string
  131. * INT $mode Permission number
  132. * Return: Successfully created or all built returns True, other methods return False
  133. */
  134. function MakeDir ($dir, $mode = "0777") {
  135. if (! $dir) return 0;
  136. $dir = str_replace ("\ \", "/", $dir);
  137. $mdir = "";
  138. foreach (Explode ("/", $dir) as $val) {
  139. $mdir. = $val. " /";
  140. if ($val = = ":" | | $val = = "." | | Trim ($val) = = "") continue;
  141. if (! file_exists ($mdir)) {
  142. if (! @mkdir ($mdir, $mode)) {
  143. return false;
  144. }
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. ?>
Copy Code
  • 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.