CFileCache.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * CFileCache class file
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFileCache provides a file-based caching mechanism.
  12. *
  13. * For each data value being cached, CFileCache will use store it in a separate file
  14. * under {@link cachePath} which defaults to 'protected/runtime/cache'.
  15. * CFileCache will perform garbage collection automatically to remove expired cache files.
  16. *
  17. * See {@link CCache} manual for common cache operations that are supported by CFileCache.
  18. *
  19. * @property integer $gCProbability The probability (parts per million) that garbage collection (GC) should be performed
  20. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @package system.caching
  24. */
  25. class CFileCache extends CCache
  26. {
  27. /**
  28. * @var string the directory to store cache files. Defaults to null, meaning
  29. * using 'protected/runtime/cache' as the directory.
  30. */
  31. public $cachePath;
  32. /**
  33. * @var integer the permission to be set for directory to store cache files
  34. * This value will be used by PHP chmod function.
  35. * Defaults to 0777, meaning the directory can be read, written and executed by all users.
  36. * @since 1.1.16
  37. */
  38. public $cachePathMode=0777;
  39. /**
  40. * @var string cache file suffix. Defaults to '.bin'.
  41. */
  42. public $cacheFileSuffix='.bin';
  43. /**
  44. * @var integer the permission to be set for new cache files.
  45. * This value will be used by PHP chmod function.
  46. * Defaults to 0666, meaning the file is read-writable by all users.
  47. * @since 1.1.16
  48. */
  49. public $cacheFileMode=0666;
  50. /**
  51. * @var integer the level of sub-directories to store cache files. Defaults to 0,
  52. * meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+),
  53. * you may want to set this value to be 1 or 2 so that the file system is not over burdened.
  54. * The value of this property should not exceed 16 (less than 3 is recommended).
  55. */
  56. public $directoryLevel=0;
  57. /**
  58. * @var boolean whether cache entry expiration time should be embedded into a physical file.
  59. * Defaults to false meaning that the file modification time will be used to store expire value.
  60. * True value means that first ten bytes of the file would be reserved and used to store expiration time.
  61. * On some systems PHP is not allowed to change file modification time to be in future even with 777
  62. * permissions, so this property could be useful in this case.
  63. * @since 1.1.14
  64. */
  65. public $embedExpiry=false;
  66. private $_gcProbability=100;
  67. private $_gced=false;
  68. /**
  69. * Initializes this application component.
  70. * This method is required by the {@link IApplicationComponent} interface.
  71. */
  72. public function init()
  73. {
  74. parent::init();
  75. if($this->cachePath===null)
  76. $this->cachePath=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache';
  77. if(!is_dir($this->cachePath))
  78. {
  79. mkdir($this->cachePath,$this->cachePathMode,true);
  80. chmod($this->cachePath,$this->cachePathMode);
  81. }
  82. }
  83. /**
  84. * @return integer the probability (parts per million) that garbage collection (GC) should be performed
  85. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  86. */
  87. public function getGCProbability()
  88. {
  89. return $this->_gcProbability;
  90. }
  91. /**
  92. * @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
  93. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  94. * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
  95. */
  96. public function setGCProbability($value)
  97. {
  98. $value=(int)$value;
  99. if($value<0)
  100. $value=0;
  101. if($value>1000000)
  102. $value=1000000;
  103. $this->_gcProbability=$value;
  104. }
  105. /**
  106. * Deletes all values from cache.
  107. * This is the implementation of the method declared in the parent class.
  108. * @return boolean whether the flush operation was successful.
  109. * @since 1.1.5
  110. */
  111. protected function flushValues()
  112. {
  113. $this->gc(false);
  114. return true;
  115. }
  116. /**
  117. * Retrieves a value from cache with a specified key.
  118. * This is the implementation of the method declared in the parent class.
  119. * @param string $key a unique key identifying the cached value
  120. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  121. */
  122. protected function getValue($key)
  123. {
  124. $cacheFile=$this->getCacheFile($key);
  125. if(($time=$this->filemtime($cacheFile))>time())
  126. return @file_get_contents($cacheFile,false,null,$this->embedExpiry ? 10 : -1);
  127. elseif($time>0)
  128. @unlink($cacheFile);
  129. return false;
  130. }
  131. /**
  132. * Stores a value identified by a key in cache.
  133. * This is the implementation of the method declared in the parent class.
  134. *
  135. * @param string $key the key identifying the value to be cached
  136. * @param string $value the value to be cached
  137. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  138. * @return boolean true if the value is successfully stored into cache, false otherwise
  139. */
  140. protected function setValue($key,$value,$expire)
  141. {
  142. if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
  143. {
  144. $this->gc();
  145. $this->_gced=true;
  146. }
  147. if($expire<=0)
  148. $expire=31536000; // 1 year
  149. $expire+=time();
  150. $cacheFile=$this->getCacheFile($key);
  151. if($this->directoryLevel>0)
  152. {
  153. $cacheDir=dirname($cacheFile);
  154. @mkdir($cacheDir,$this->cachePathMode,true);
  155. @chmod($cacheDir,$this->cachePathMode);
  156. }
  157. if(@file_put_contents($cacheFile,$this->embedExpiry ? $expire.$value : $value,LOCK_EX)!==false)
  158. {
  159. @chmod($cacheFile,$this->cacheFileMode);
  160. return $this->embedExpiry ? true : @touch($cacheFile,$expire);
  161. }
  162. else
  163. return false;
  164. }
  165. /**
  166. * Stores a value identified by a key into cache if the cache does not contain this key.
  167. * This is the implementation of the method declared in the parent class.
  168. *
  169. * @param string $key the key identifying the value to be cached
  170. * @param string $value the value to be cached
  171. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  172. * @return boolean true if the value is successfully stored into cache, false otherwise
  173. */
  174. protected function addValue($key,$value,$expire)
  175. {
  176. $cacheFile=$this->getCacheFile($key);
  177. if($this->filemtime($cacheFile)>time())
  178. return false;
  179. return $this->setValue($key,$value,$expire);
  180. }
  181. /**
  182. * Deletes a value with the specified key from cache
  183. * This is the implementation of the method declared in the parent class.
  184. * @param string $key the key of the value to be deleted
  185. * @return boolean if no error happens during deletion
  186. */
  187. protected function deleteValue($key)
  188. {
  189. $cacheFile=$this->getCacheFile($key);
  190. return @unlink($cacheFile);
  191. }
  192. /**
  193. * Returns the cache file path given the cache key.
  194. * @param string $key cache key
  195. * @return string the cache file path
  196. */
  197. protected function getCacheFile($key)
  198. {
  199. if($this->directoryLevel>0)
  200. {
  201. $base=$this->cachePath;
  202. for($i=0;$i<$this->directoryLevel;++$i)
  203. {
  204. if(($prefix=substr($key,$i+$i,2))!==false)
  205. $base.=DIRECTORY_SEPARATOR.$prefix;
  206. }
  207. return $base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  208. }
  209. else
  210. return $this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  211. }
  212. /**
  213. * Removes expired cache files.
  214. * @param boolean $expiredOnly whether only expired cache files should be removed.
  215. * If false, all cache files under {@link cachePath} will be removed.
  216. * @param string $path the path to clean with. If null, it will be {@link cachePath}.
  217. */
  218. public function gc($expiredOnly=true,$path=null)
  219. {
  220. if($path===null)
  221. $path=$this->cachePath;
  222. if(($handle=opendir($path))===false)
  223. return;
  224. while(($file=readdir($handle))!==false)
  225. {
  226. if($file[0]==='.')
  227. continue;
  228. $fullPath=$path.DIRECTORY_SEPARATOR.$file;
  229. if(is_dir($fullPath))
  230. $this->gc($expiredOnly,$fullPath);
  231. elseif($expiredOnly && $this->filemtime($fullPath)<time() || !$expiredOnly)
  232. @unlink($fullPath);
  233. }
  234. closedir($handle);
  235. }
  236. /**
  237. * Returns cache file modification time. {@link $embedExpiry} aware.
  238. * @param string $path to the file, modification time to be retrieved from.
  239. * @return integer file modification time.
  240. */
  241. private function filemtime($path)
  242. {
  243. if($this->embedExpiry)
  244. return (int)@file_get_contents($path,false,null,0,10);
  245. else
  246. return @filemtime($path);
  247. }
  248. }