PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/yii-1.1.4/framework/caching/CFileCache.php

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