sfFileCache.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Cache class that stores content in files.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFileCache.class.php 15511 2009-02-16 08:31:53Z fabien $
  16. */
  17. class sfFileCache extends sfCache
  18. {
  19. const READ_DATA = 1;
  20. const READ_TIMEOUT = 2;
  21. const READ_LAST_MODIFIED = 4;
  22. const EXTENSION = '.cache';
  23. /**
  24. * Initializes this sfCache instance.
  25. *
  26. * Available options:
  27. *
  28. * * cache_dir: The directory where to put cache files
  29. *
  30. * * see sfCache for options available for all drivers
  31. *
  32. * @see sfCache
  33. */
  34. public function initialize($options = array())
  35. {
  36. parent::initialize($options);
  37. if (!$this->getOption('cache_dir'))
  38. {
  39. throw new sfInitializationException('You must pass a "cache_dir" option to initialize a sfFileCache object.');
  40. }
  41. $this->setcache_dir($this->getOption('cache_dir'));
  42. }
  43. /**
  44. * @see sfCache
  45. */
  46. public function get($key, $default = null)
  47. {
  48. $file_path = $this->getFilePath($key);
  49. if (!file_exists($file_path))
  50. {
  51. return $default;
  52. }
  53. $data = $this->read($file_path, self::READ_DATA);
  54. if ($data[self::READ_DATA] === null) {
  55. return $default;
  56. }
  57. return $data[self::READ_DATA];
  58. }
  59. /**
  60. * @see sfCache
  61. */
  62. public function has($key)
  63. {
  64. $path = $this->getFilePath($key);
  65. return file_exists($path) && $this->isValid($path);
  66. }
  67. /**
  68. * @see sfCache
  69. */
  70. public function set($key, $data, $lifetime = null)
  71. {
  72. if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1)
  73. {
  74. $this->clean(sfCache::OLD);
  75. }
  76. return $this->write($this->getFilePath($key), $data, time() + $this->getLifetime($lifetime));
  77. }
  78. /**
  79. * @see sfCache
  80. */
  81. public function remove($key)
  82. {
  83. return @unlink($this->getFilePath($key));
  84. }
  85. /**
  86. * @see sfCache
  87. */
  88. public function removePattern($pattern)
  89. {
  90. if (false !== strpos($pattern, '**'))
  91. {
  92. $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
  93. $regexp = self::patternToRegexp($pattern);
  94. $paths = array();
  95. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
  96. {
  97. if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
  98. {
  99. $paths[] = $path;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. $paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
  106. }
  107. foreach ($paths as $path)
  108. {
  109. if (is_dir($path))
  110. {
  111. sfToolkit::clearDirectory($path);
  112. }
  113. else
  114. {
  115. @unlink($path);
  116. }
  117. }
  118. }
  119. /**
  120. * @see sfCache
  121. */
  122. public function clean($mode = sfCache::ALL)
  123. {
  124. if (!is_dir($this->getOption('cache_dir')))
  125. {
  126. return true;
  127. }
  128. $result = true;
  129. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $file)
  130. {
  131. if (sfCache::ALL == $mode || !$this->isValid($file))
  132. {
  133. $result = @unlink($file) && $result;
  134. }
  135. }
  136. return $result;
  137. }
  138. /**
  139. * @see sfCache
  140. */
  141. public function getTimeout($key)
  142. {
  143. $path = $this->getFilePath($key);
  144. if (!file_exists($path))
  145. {
  146. return 0;
  147. }
  148. $data = $this->read($path, self::READ_TIMEOUT);
  149. return $data[self::READ_TIMEOUT] < time() ? 0 : $data[self::READ_TIMEOUT];
  150. }
  151. /**
  152. * @see sfCache
  153. */
  154. public function getLastModified($key)
  155. {
  156. $path = $this->getFilePath($key);
  157. if (!file_exists($path))
  158. {
  159. return 0;
  160. }
  161. $data = $this->read($path, self::READ_TIMEOUT | self::READ_LAST_MODIFIED);
  162. if ($data[self::READ_TIMEOUT] < time())
  163. {
  164. return 0;
  165. }
  166. return $data[self::READ_LAST_MODIFIED];
  167. }
  168. protected function isValid($path)
  169. {
  170. $data = $this->read($path, self::READ_TIMEOUT);
  171. return time() < $data[self::READ_TIMEOUT];
  172. }
  173. /**
  174. * Converts a cache key to a full path.
  175. *
  176. * @param string $key The cache key
  177. *
  178. * @return string The full path to the cache file
  179. */
  180. protected function getFilePath($key)
  181. {
  182. return $this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $key).self::EXTENSION;
  183. }
  184. /**
  185. * Reads the cache file and returns the content.
  186. *
  187. * @param string $path The file path
  188. * @param mixed $type The type of data you want to be returned
  189. * sfFileCache::READ_DATA: The cache content
  190. * sfFileCache::READ_TIMEOUT: The timeout
  191. * sfFileCache::READ_LAST_MODIFIED: The last modification timestamp
  192. *
  193. * @return array the (meta)data of the cache file. E.g. $data[sfFileCache::READ_DATA]
  194. *
  195. * @throws sfCacheException
  196. */
  197. protected function read($path, $type = self::READ_DATA)
  198. {
  199. if (!$fp = @fopen($path, 'rb'))
  200. {
  201. throw new sfCacheException(sprintf('Unable to read cache file "%s".', $path));
  202. }
  203. @flock($fp, LOCK_SH);
  204. $data[self::READ_TIMEOUT] = intval(@stream_get_contents($fp, 12, 0));
  205. if ($type != self::READ_TIMEOUT && time() < $data[self::READ_TIMEOUT]) {
  206. if ($type & self::READ_LAST_MODIFIED)
  207. {
  208. $data[self::READ_LAST_MODIFIED] = intval(@stream_get_contents($fp, 12, 12));
  209. }
  210. if ($type & self::READ_DATA)
  211. {
  212. fseek($fp, 0, SEEK_END);
  213. $length = ftell($fp) - 24;
  214. fseek($fp, 24);
  215. $data[self::READ_DATA] = @fread($fp, $length);
  216. }
  217. } else {
  218. $data[self::READ_LAST_MODIFIED] = null;
  219. $data[self::READ_DATA] = null;
  220. }
  221. @flock($fp, LOCK_UN);
  222. @fclose($fp);
  223. return $data;
  224. }
  225. /**
  226. * Writes the given data in the cache file.
  227. *
  228. * @param string $path The file path
  229. * @param string $data The data to put in cache
  230. * @param integer $timeout The timeout timestamp
  231. *
  232. * @return boolean true if ok, otherwise false
  233. *
  234. * @throws sfCacheException
  235. */
  236. protected function write($path, $data, $timeout)
  237. {
  238. $current_umask = umask();
  239. umask(0000);
  240. if (!is_dir(dirname($path)))
  241. {
  242. // create directory structure if needed
  243. mkdir(dirname($path), 0777, true);
  244. }
  245. $tmpFile = $path . '.' . getmypid();
  246. if (!$fp = @fopen($tmpFile, 'wb'))
  247. {
  248. throw new sfCacheException(sprintf('Unable to write cache file "%s".', $tmpFile));
  249. }
  250. @fwrite($fp, str_pad($timeout, 12, 0, STR_PAD_LEFT));
  251. @fwrite($fp, str_pad(time(), 12, 0, STR_PAD_LEFT));
  252. @fwrite($fp, $data);
  253. @fclose($fp);
  254. chmod($tmpFile, 0666);
  255. @unlink($path);
  256. rename($tmpFile, $path);
  257. umask($current_umask);
  258. return true;
  259. }
  260. /**
  261. * Sets the cache root directory.
  262. *
  263. * @param string $cache_dir The directory where to put the cache files
  264. */
  265. protected function setcache_dir($cache_dir)
  266. {
  267. // remove last DIRECTORY_SEPARATOR
  268. if (DIRECTORY_SEPARATOR == substr($cache_dir, -1))
  269. {
  270. $cache_dir = substr($cache_dir, 0, -1);
  271. }
  272. // create cache dir if needed
  273. if (!is_dir($cache_dir))
  274. {
  275. $current_umask = umask(0000);
  276. @mkdir($cache_dir, 0777, true);
  277. umask($current_umask);
  278. }
  279. }
  280. }