再来一个缓存类

原创
2016-07-25 09:11:15 563浏览
缓存类
  1. /*
  2. * 缓存类 cache
  3. * 作 者:多菜鸟
  4. * 创建时间:2006-05-05
  5. * 实 例:
  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. //缓存目录
  14. var $cacheRoot = "./cache/";
  15. //缓存更新时间秒数,0为不缓存
  16. var $cacheLimitTime = 0;
  17. //缓存文件名
  18. var $cacheFileName = "";
  19. //缓存扩展名
  20. var $cacheFileExt = "php";
  21. /*
  22. * 构造函数
  23. * int $cacheLimitTime 缓存更新时间
  24. */
  25. function cache( $cacheLimitTime ) {
  26. if( intval( $cacheLimitTime ) )
  27. $this->cacheLimitTime = $cacheLimitTime;
  28. $this->cacheFileName = $this->getCacheFileName();
  29. ob_start();
  30. }
  31. /*
  32. * 检查缓存文件是否在设置更新时间之内
  33. * 返回:如果在更新时间之内则返回文件内容,反之则返回失败
  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. * 缓存文件或者输出静态
  48. * string $staticFileName 静态文件名(含相对路径)
  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. * 清除缓存文件
  64. * string $fileName 指定文件名(含函数)或者all(全部)
  65. * 返回:清除成功返回true,反之返回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. * 根据当前动态文件生成缓存文件名
  92. */
  93. function getCacheFileName() {
  94. return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
  95. }
  96. /*
  97. * 缓存文件建立时间
  98. * string $fileName 缓存文件名(含相对路径)
  99. * 返回:文件生成时间秒数,文件不存在返回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. * 保存文件
  109. * string $fileName 文件名(含相对路径)
  110. * string $text 文件内容
  111. * 返回:成功返回ture,失败返回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. * 连续建目录
  130. * string $dir 目录字符串
  131. * int $mode 权限数字
  132. * 返回:顺利创建或者全部已建返回true,其它方式返回false
  133. */
  134. function makeDir( $dir, $mode = "0777" ) {
  135. if( ! $dir ) return 0;
  136. $dir = str_replace( "\\", "//m.sbmmt.com/m/", $dir );
  137. $mdir = "";
  138. foreach( explode( "//m.sbmmt.com/m/", $dir ) as $val ) {
  139. $mdir .= $val."//m.sbmmt.com/m/";
  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. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:PHP中文字符串截取函数 下一条:COOKIE加密函数

相关文章

查看更多