/class/cache/FSCache.php

https://github.com/JeCat/framework · PHP · 200 lines · 121 code · 21 blank · 58 comment · 16 complexity · 97b43cfa1d33b9661bcaed3f4356bd0c MD5 · raw file

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. // 这个文件是 JeCat PHP框架的一部分,该项目和此文件 均遵循 GNU 自由软件协议
  4. //
  5. // Copyleft 2008-2012 JeCat.cn(http://team.JeCat.cn)
  6. //
  7. //
  8. // JeCat PHP框架 的正式全名是:Jellicle Cat PHP Framework。
  9. // “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》(《Prologue:Jellicle Songs for Jellicle Cats》)。
  10. // JeCat 是一个开源项目,它像音乐剧中的猫一样自由,你可以毫无顾忌地使用JCAT PHP框架。JCAT 由中国团队开发维护。
  11. // 正在使用的这个版本是:0.7.1
  12. //
  13. //
  14. //
  15. // 相关的链接:
  16. // [主页] http://www.JeCat.cn
  17. // [源代码] https://github.com/JeCat/framework
  18. // [下载(http)] https://nodeload.github.com/JeCat/framework/zipball/master
  19. // [下载(git)] git clone git://github.com/JeCat/framework.git jecat
  20. // 不很相关:
  21. // [MP3] http://www.google.com/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
  22. // [VCD/DVD] http://www.google.com/search?q=CAT+Andrew+Lloyd+Webber+video
  23. //
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. /*-- Project Introduce --*/
  26. namespace org\jecat\framework\cache ;
  27. use org\jecat\framework\fs\Folder;
  28. class FSCache extends Cache
  29. {
  30. public function __construct($sFolder)
  31. {
  32. $this->sFolderPrefix = rtrim($sFolder,'/').'/' ;
  33. }
  34. public function item($sDataPath)
  35. {
  36. $nExpireTime = $this->expireTime($sDataPath) ;
  37. if( $nExpireTime<0 )
  38. {
  39. return null ;
  40. }
  41. else if( $nExpireTime>0 and $nExpireTime<time() )
  42. {
  43. $this->delete($sDataPath) ;
  44. return null ;
  45. }
  46. $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
  47. // 尝试 .php
  48. if( is_file($sDataPath.'.php') )
  49. {
  50. return include $sDataPath.'.php' ;
  51. }
  52. // 尝试 .data
  53. else if( is_file($sDataPath.'.data') )
  54. {
  55. return unserialize(file_get_contents($sDataPath.'.data')) ;
  56. }
  57. return null ;
  58. }
  59. public function setItem($sDataPath,$data,$nExpire=self::expire_default,$fCreateTimeMicroSec=-1)
  60. {
  61. $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
  62. if(is_object($data))
  63. {
  64. $sSerialize = serialize($data) ;
  65. $sFilePath = $sDataPath.'.data' ;
  66. }
  67. else
  68. {
  69. $sSerialize = "<?php\r\nreturn ".var_export($data,true).' ;' ;
  70. $sFilePath = $sDataPath.'.php' ;
  71. }
  72. $sDataFolder = dirname($sFilePath) ;
  73. if( !is_dir($sDataFolder) and !Folder::mkdir($sDataFolder,0775,true) )
  74. {
  75. return ;
  76. }
  77. file_put_contents( $sFilePath,$sSerialize) ;
  78. // create time
  79. if($fCreateTimeMicroSec<0)
  80. {
  81. $fCreateTimeMicroSec = microtime(true)+600 ;
  82. }
  83. // expire time
  84. if( $nExpire===Cache::expire_allways )
  85. {
  86. $nExpireSec = 0 ;
  87. }
  88. else if( $nExpire===Cache::$expire_default )
  89. {
  90. $nExpireSec = ceil($fCreateTimeMicroSec) + Cache::$expire_default ;
  91. }
  92. else
  93. {
  94. $nExpireSec = ceil($fCreateTimeMicroSec) + $nExpire ;
  95. }
  96. file_put_contents( $sDataPath.'.time', "<?php return array({$fCreateTimeMicroSec},{$nExpireSec}) ;" ) ;
  97. }
  98. /**
  99. * Enter description here ...
  100. *
  101. */
  102. public function delete($sDataPath)
  103. {
  104. // 所有
  105. if( empty($sDataPath) )
  106. {
  107. $this->clear() ;
  108. }
  109. else
  110. {
  111. $sDataPath = $this->sFolderPrefix.trim($sDataPath,'/') ;
  112. // 删除目录
  113. if( is_dir($sDataPath) )
  114. {
  115. Folder::createInstance($sDataPath)->delete(true,true) ;
  116. }
  117. // 删除指定内容
  118. else
  119. {
  120. @unlink($sDataPath.'.data') ;
  121. @unlink($sDataPath.'.php') ;
  122. @unlink($sDataPath.'.time') ;
  123. }
  124. }
  125. }
  126. /**
  127. * Enter description here ...
  128. *
  129. * @return bool
  130. */
  131. public function clear()
  132. {
  133. return Folder::createInstance($this->sFolderPrefix)->delete(true,true) ;
  134. }
  135. /**
  136. * Enter description here ...
  137. *
  138. * @return bool
  139. */
  140. public function isExpire($sDataPath)
  141. {
  142. $nExpireTime = $this->expireTime($sDataPath) ;
  143. return $nExpireTime>0 and $nExpireTime<time() ;
  144. }
  145. /**
  146. * Enter description here ...
  147. *
  148. * @return float
  149. */
  150. public function createTime($sDataPath)
  151. {
  152. $sPath = $this->sFolderPrefix.trim($sDataPath,'/').'.time' ;
  153. if(!is_file($sPath))
  154. {
  155. return 0 ;
  156. }
  157. list($fCreateTime,) = explode(',',file_get_contents($sPath)) ;
  158. return (int)$fCreateTime ;
  159. }
  160. /**
  161. * Enter description here ...
  162. *
  163. * @return int
  164. */
  165. public function expireTime($sDataPath)
  166. {
  167. $sPath = $this->sFolderPrefix.trim($sDataPath,'/').'.time' ;
  168. if(!is_file($sPath))
  169. {
  170. return 0 ;
  171. }
  172. list(,$nExpireTime) = explode(',',file_get_contents($sPath)) ;
  173. return (int)$nExpireTime ;
  174. }
  175. /**
  176. * @var org\jecat\framework\fs\FsFolder
  177. */
  178. private $aFolder ;
  179. }