PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/ThinkPHP/Lib/Think/Util/Cache/CacheFile.class.php

http://iiccms.googlecode.com/
PHP | 247 lines | 127 code | 12 blank | 108 comment | 29 complexity | 6b6a1434dedd3138edd16c1fbce26fa2 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ???????
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class CacheFile extends Cache
  24. {//?????
  25. /**
  26. +----------------------------------------------------------
  27. * ????
  28. +----------------------------------------------------------
  29. * @access public
  30. +----------------------------------------------------------
  31. */
  32. public function __construct($options='')
  33. {
  34. if(!empty($options['temp'])){
  35. $this->options['temp'] = $options['temp'];
  36. }else {
  37. $this->options['temp'] = C('DATA_CACHE_PATH');
  38. }
  39. $this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
  40. if(substr($this->options['temp'], -1) != "/") $this->options['temp'] .= "/";
  41. $this->connected = is_dir($this->options['temp']) && is_writeable($this->options['temp']);
  42. $this->type = strtoupper(substr(__CLASS__,6));
  43. $this->init();
  44. }
  45. /**
  46. +----------------------------------------------------------
  47. * ?????
  48. +----------------------------------------------------------
  49. * @access private
  50. +----------------------------------------------------------
  51. * @return boolen
  52. +----------------------------------------------------------
  53. */
  54. private function init()
  55. {
  56. $stat = stat($this->options['temp']);
  57. $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
  58. $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
  59. // ????????
  60. if (!is_dir($this->options['temp'])) {
  61. if (! mkdir($this->options['temp']))
  62. return false;
  63. chmod($this->options['temp'], $dir_perms);
  64. }
  65. }
  66. /**
  67. +----------------------------------------------------------
  68. * ????
  69. +----------------------------------------------------------
  70. * @access public
  71. +----------------------------------------------------------
  72. * @return boolen
  73. +----------------------------------------------------------
  74. */
  75. private function isConnected()
  76. {
  77. return $this->connected;
  78. }
  79. /**
  80. +----------------------------------------------------------
  81. * ??????????
  82. +----------------------------------------------------------
  83. * @access private
  84. +----------------------------------------------------------
  85. * @param string $name ?????
  86. +----------------------------------------------------------
  87. * @return string
  88. +----------------------------------------------------------
  89. */
  90. private function filename($name)
  91. {
  92. $name = md5($name);
  93. if(C('DATA_CACHE_SUBDIR')) {
  94. // ?????
  95. $dir ='';
  96. for($i=0;$i<C('DATA_PATH_LEVEL');$i++) {
  97. $dir .= $name{$i}.'/';
  98. }
  99. if(!is_dir($this->options['temp'].$dir)) {
  100. mk_dir($this->options['temp'].$dir);
  101. }
  102. $filename = $dir.$this->prefix.$name.'.php';
  103. }else{
  104. $filename = $this->prefix.$name.'.php';
  105. }
  106. return $this->options['temp'].$filename;
  107. }
  108. /**
  109. +----------------------------------------------------------
  110. * ????
  111. +----------------------------------------------------------
  112. * @access public
  113. +----------------------------------------------------------
  114. * @param string $name ?????
  115. +----------------------------------------------------------
  116. * @return mixed
  117. +----------------------------------------------------------
  118. */
  119. public function get($name)
  120. {
  121. $filename = $this->filename($name);
  122. if (!$this->isConnected() || !is_file($filename)) {
  123. return false;
  124. }
  125. $this->Q(1);
  126. $content = file_get_contents($filename);
  127. if( false !== $content) {
  128. $expire = (int)substr($content,8, 12);
  129. if($expire != -1 && time() > filemtime($filename) + $expire) {
  130. //??????????
  131. unlink($filename);
  132. return false;
  133. }
  134. if(C('DATA_CACHE_CHECK')) {//??????
  135. $check = substr($content,20, 32);
  136. $content = substr($content,52, -3);
  137. if($check != md5($content)) {//????
  138. return false;
  139. }
  140. }else {
  141. $content = substr($content,20, -3);
  142. }
  143. if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  144. //??????
  145. $content = gzuncompress($content);
  146. }
  147. $content = unserialize($content);
  148. return $content;
  149. }
  150. else {
  151. return false;
  152. }
  153. }
  154. /**
  155. +----------------------------------------------------------
  156. * ????
  157. +----------------------------------------------------------
  158. * @access public
  159. +----------------------------------------------------------
  160. * @param string $name ?????
  161. * @param mixed $value ????
  162. * @param int $expire ???? -1 ???
  163. +----------------------------------------------------------
  164. * @return boolen
  165. +----------------------------------------------------------
  166. */
  167. public function set($name,$value,$expire='')
  168. {
  169. $this->W(1);
  170. if('' === $expire) {
  171. $expire = $this->expire;
  172. }
  173. $filename = $this->filename($name);
  174. $data = serialize($value);
  175. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  176. //????
  177. $data = gzcompress($data,3);
  178. }
  179. if(C('DATA_CACHE_CHECK')) {//??????
  180. $check = md5($data);
  181. }else {
  182. $check = '';
  183. }
  184. $data = "<?php\n//".sprintf('%012d',$expire).$check.$data."\n?>";
  185. $result = file_put_contents($filename,$data);
  186. if($result) {
  187. clearstatcache();
  188. return true;
  189. }else {
  190. return false;
  191. }
  192. }
  193. /**
  194. +----------------------------------------------------------
  195. * ????
  196. +----------------------------------------------------------
  197. * @access public
  198. +----------------------------------------------------------
  199. * @param string $name ?????
  200. +----------------------------------------------------------
  201. * @return boolen
  202. +----------------------------------------------------------
  203. */
  204. public function rm($name)
  205. {
  206. return unlink($this->filename($name));
  207. }
  208. /**
  209. +----------------------------------------------------------
  210. * ????
  211. +----------------------------------------------------------
  212. * @access public
  213. +----------------------------------------------------------
  214. * @param string $name ?????
  215. +----------------------------------------------------------
  216. * @return boolen
  217. +----------------------------------------------------------
  218. */
  219. public function clear()
  220. {
  221. $path = $this->options['temp'];
  222. if ( $dir = opendir( $path ) )
  223. {
  224. while ( $file = readdir( $dir ) )
  225. {
  226. $check = is_dir( $file );
  227. if ( !$check )
  228. unlink( $path . $file );
  229. }
  230. closedir( $dir );
  231. return true;
  232. }
  233. }
  234. }//?????
  235. ?>