PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/craft/app/framework/caching/CFileCache.php

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