PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/a10/lib/yii-1.1.10/caching/CFileCache.php

http://chenjin.googlecode.com/
PHP | 222 lines | 110 code | 14 blank | 98 comment | 24 complexity | b0da4924d3075a72946419aaaf0fe462 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CFileCache class file
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 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. * @version $Id: CFileCache.php 242 2012-03-29 15:18:01Z mole1230 $
  24. * @package system.caching
  25. */
  26. class CFileCache extends CCache
  27. {
  28. /**
  29. * @var string the directory to store cache files. Defaults to null, meaning
  30. * using 'protected/runtime/cache' as the directory.
  31. */
  32. public $cachePath;
  33. /**
  34. * @var string cache file suffix. Defaults to '.bin'.
  35. */
  36. public $cacheFileSuffix='.bin';
  37. /**
  38. * @var integer the level of sub-directories to store cache files. Defaults to 0,
  39. * meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+),
  40. * you may want to set this value to be 1 or 2 so that the file system is not over burdened.
  41. * The value of this property should not exceed 16 (less than 3 is recommended).
  42. */
  43. public $directoryLevel=0;
  44. private $_gcProbability=100;
  45. private $_gced=false;
  46. /**
  47. * Initializes this application component.
  48. * This method is required by the {@link IApplicationComponent} interface.
  49. * It checks the availability of memcache.
  50. * @throws CException if APC cache extension is not loaded or is disabled.
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. if($this->cachePath===null)
  56. $this->cachePath=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache';
  57. if(!is_dir($this->cachePath))
  58. mkdir($this->cachePath,0777,true);
  59. }
  60. /**
  61. * @return integer the probability (parts per million) that garbage collection (GC) should be performed
  62. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  63. */
  64. public function getGCProbability()
  65. {
  66. return $this->_gcProbability;
  67. }
  68. /**
  69. * @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
  70. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  71. * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
  72. */
  73. public function setGCProbability($value)
  74. {
  75. $value=(int)$value;
  76. if($value<0)
  77. $value=0;
  78. if($value>1000000)
  79. $value=1000000;
  80. $this->_gcProbability=$value;
  81. }
  82. /**
  83. * Deletes all values from cache.
  84. * This is the implementation of the method declared in the parent class.
  85. * @return boolean whether the flush operation was successful.
  86. * @since 1.1.5
  87. */
  88. protected function flushValues()
  89. {
  90. $this->gc(false);
  91. return true;
  92. }
  93. /**
  94. * Retrieves a value from cache with a specified key.
  95. * This is the implementation of the method declared in the parent class.
  96. * @param string $key a unique key identifying the cached value
  97. * @return string the value stored in cache, false if the value is not in the cache or expired.
  98. */
  99. protected function getValue($key)
  100. {
  101. $cacheFile=$this->getCacheFile($key);
  102. if(($time=@filemtime($cacheFile))>time())
  103. return @file_get_contents($cacheFile);
  104. else if($time>0)
  105. @unlink($cacheFile);
  106. return false;
  107. }
  108. /**
  109. * Stores a value identified by a key in cache.
  110. * This is the implementation of the method declared in the parent class.
  111. *
  112. * @param string $key the key identifying the value to be cached
  113. * @param string $value the value to be cached
  114. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  115. * @return boolean true if the value is successfully stored into cache, false otherwise
  116. */
  117. protected function setValue($key,$value,$expire)
  118. {
  119. if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
  120. {
  121. $this->gc();
  122. $this->_gced=true;
  123. }
  124. if($expire<=0)
  125. $expire=31536000; // 1 year
  126. $expire+=time();
  127. $cacheFile=$this->getCacheFile($key);
  128. if($this->directoryLevel>0)
  129. @mkdir(dirname($cacheFile),0777,true);
  130. if(@file_put_contents($cacheFile,$value,LOCK_EX)!==false)
  131. {
  132. @chmod($cacheFile,0777);
  133. return @touch($cacheFile,$expire);
  134. }
  135. else
  136. return false;
  137. }
  138. /**
  139. * Stores a value identified by a key into cache if the cache does not contain this key.
  140. * This is the implementation of the method declared in the parent class.
  141. *
  142. * @param string $key the key identifying the value to be cached
  143. * @param string $value the value to be cached
  144. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  145. * @return boolean true if the value is successfully stored into cache, false otherwise
  146. */
  147. protected function addValue($key,$value,$expire)
  148. {
  149. $cacheFile=$this->getCacheFile($key);
  150. if(@filemtime($cacheFile)>time())
  151. return false;
  152. return $this->setValue($key,$value,$expire);
  153. }
  154. /**
  155. * Deletes a value with the specified key from cache
  156. * This is the implementation of the method declared in the parent class.
  157. * @param string $key the key of the value to be deleted
  158. * @return boolean if no error happens during deletion
  159. */
  160. protected function deleteValue($key)
  161. {
  162. $cacheFile=$this->getCacheFile($key);
  163. return @unlink($cacheFile);
  164. }
  165. /**
  166. * Returns the cache file path given the cache key.
  167. * @param string $key cache key
  168. * @return string the cache file path
  169. */
  170. protected function getCacheFile($key)
  171. {
  172. if($this->directoryLevel>0)
  173. {
  174. $base=$this->cachePath;
  175. for($i=0;$i<$this->directoryLevel;++$i)
  176. {
  177. if(($prefix=substr($key,$i+$i,2))!==false)
  178. $base.=DIRECTORY_SEPARATOR.$prefix;
  179. }
  180. return $base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  181. }
  182. else
  183. return $this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  184. }
  185. /**
  186. * Removes expired cache files.
  187. * @param boolean $expiredOnly whether to removed expired cache files only. If true, all cache files under {@link cachePath} will be removed.
  188. * @param string $path the path to clean with. If null, it will be {@link cachePath}.
  189. */
  190. public function gc($expiredOnly=true,$path=null)
  191. {
  192. if($path===null)
  193. $path=$this->cachePath;
  194. if(($handle=opendir($path))===false)
  195. return;
  196. while(($file=readdir($handle))!==false)
  197. {
  198. if($file[0]==='.')
  199. continue;
  200. $fullPath=$path.DIRECTORY_SEPARATOR.$file;
  201. if(is_dir($fullPath))
  202. $this->gc($expiredOnly,$fullPath);
  203. else if($expiredOnly && @filemtime($fullPath)<time() || !$expiredOnly)
  204. @unlink($fullPath);
  205. }
  206. closedir($handle);
  207. }
  208. }