Home  >  Article  >  Backend Development  >  Another cache class

Another cache class

WBOY
WBOYOriginal
2016-07-25 09:11:15934browse
Cache class
  1. /*
  2. * Cache class cache
  3. * Author: Duo Noob
  4. * Creation time: 2006-05-05
  5. * Example:
  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 in seconds, 0 means no caching
  16. var $cacheLimitTime = 0;
  17. //Cache file name
  18. var $cacheFileName = "";
  19. / /Cache extension
  20. var $cacheFileExt = "php";
  21. /*
  22. * constructor
  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 whether the cache file is within the set update time
  33. * Return: If it is within the update time, return the file content, otherwise it will return 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 (including 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 specified file name (including functions) or all (all)
  65. * Return: true if clearing is successful, otherwise 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 a cache file name based on the current dynamic file
  92. */
  93. function getCacheFileName() {
  94. return $this-> cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
  95. }
  96. /*
  97. * Cache file creation time
  98. * string $fileName cache file name (including relative path) )
  99. * Return: file generation time in seconds, if the 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 the file
  109. * string $fileName file name (including relative path)
  110. * string $text file content
  111. * Return: Returns true on success, false on failure
  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. * Continuously create directories
  130. * string $dir directory string
  131. * int $mode permission number
  132. * return : Return true if created successfully or all have been built, otherwise 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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn