You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.7 KiB

4 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. defined('THINK_PATH') or exit();
  14. /**
  15. * 文件类型缓存类
  16. */
  17. class File extends Cache {
  18. /**
  19. * 架构函数
  20. * @access public
  21. */
  22. public function __construct($options=array()) {
  23. if(!empty($options)) {
  24. $this->options = $options;
  25. }
  26. $this->options['temp'] = !empty($options['temp'])? $options['temp'] : C('DATA_CACHE_PATH');
  27. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  28. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  29. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  30. if(substr($this->options['temp'], -1) != '/') $this->options['temp'] .= '/';
  31. $this->init();
  32. }
  33. /**
  34. * 初始化检查
  35. * @access private
  36. * @return boolean
  37. */
  38. private function init() {
  39. // 创建应用缓存目录
  40. if (!is_dir($this->options['temp'])) {
  41. mkdir($this->options['temp']);
  42. }
  43. }
  44. /**
  45. * 取得变量的存储文件名
  46. * @access private
  47. * @param string $name 缓存变量名
  48. * @return string
  49. */
  50. private function filename($name) {
  51. $name = md5(C('DATA_CACHE_KEY').$name);
  52. if(C('DATA_CACHE_SUBDIR')) {
  53. // 使用子目录
  54. $dir ='';
  55. for($i=0;$i<C('DATA_PATH_LEVEL');$i++) {
  56. $dir .= $name[$i].'/';
  57. }
  58. if(!is_dir($this->options['temp'].$dir)) {
  59. mkdir($this->options['temp'].$dir,0755,true);
  60. }
  61. $filename = $dir.$this->options['prefix'].$name.'.php';
  62. }else{
  63. $filename = $this->options['prefix'].$name.'.php';
  64. }
  65. return $this->options['temp'].$filename;
  66. }
  67. /**
  68. * 读取缓存
  69. * @access public
  70. * @param string $name 缓存变量名
  71. * @return mixed
  72. */
  73. public function get($name) {
  74. $filename = $this->filename($name);
  75. if (!is_file($filename)) {
  76. return false;
  77. }
  78. N('cache_read',1);
  79. $content = file_get_contents($filename);
  80. if( false !== $content) {
  81. $expire = (int)substr($content,8, 12);
  82. if($expire != 0 && time() > filemtime($filename) + $expire) {
  83. //缓存过期删除缓存文件
  84. unlink($filename);
  85. return false;
  86. }
  87. if(C('DATA_CACHE_CHECK')) {//开启数据校验
  88. $check = substr($content,20, 32);
  89. $content = substr($content,52, -3);
  90. if($check != md5($content)) {//校验错误
  91. return false;
  92. }
  93. }else {
  94. $content = substr($content,20, -3);
  95. }
  96. if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  97. //启用数据压缩
  98. $content = gzuncompress($content);
  99. }
  100. $content = unserialize($content);
  101. return $content;
  102. }
  103. else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * 写入缓存
  109. * @access public
  110. * @param string $name 缓存变量名
  111. * @param mixed $value 存储数据
  112. * @param int $expire 有效时间 0为永久
  113. * @return boolean
  114. */
  115. public function set($name,$value,$expire=null) {
  116. N('cache_write',1);
  117. if(is_null($expire)) {
  118. $expire = $this->options['expire'];
  119. }
  120. $filename = $this->filename($name);
  121. $data = serialize($value);
  122. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  123. //数据压缩
  124. $data = gzcompress($data,3);
  125. }
  126. if(C('DATA_CACHE_CHECK')) {//开启数据校验
  127. $check = md5($data);
  128. }else {
  129. $check = '';
  130. }
  131. $data = "<?php\n//".sprintf('%012d',$expire).$check.$data."\n?>";
  132. $result = file_put_contents($filename,$data);
  133. if($result) {
  134. if($this->options['length']>0) {
  135. // 记录缓存队列
  136. $this->queue($name);
  137. }
  138. clearstatcache();
  139. return true;
  140. }else {
  141. return false;
  142. }
  143. }
  144. /**
  145. * 删除缓存
  146. * @access public
  147. * @param string $name 缓存变量名
  148. * @return boolean
  149. */
  150. public function rm($name) {
  151. return unlink($this->filename($name));
  152. }
  153. /**
  154. * 清除缓存
  155. * @access public
  156. * @param string $name 缓存变量名
  157. * @return boolean
  158. */
  159. public function clear() {
  160. $path = $this->options['temp'];
  161. $files = scandir($path);
  162. if($files){
  163. foreach($files as $file){
  164. if ($file != '.' && $file != '..' && is_dir($path.$file) ){
  165. array_map( 'unlink', glob( $path.$file.'/*.*' ) );
  166. }elseif(is_file($path.$file)){
  167. unlink( $path . $file );
  168. }
  169. }
  170. return true;
  171. }
  172. return false;
  173. }
  174. }