Source for file Files.class.php

Documentation is available at Files.class.php

  1. <?php
  2. /**
  3. * XOAD_Cache Storage Files Provider File.
  4. *
  5. * <p>This file defines the {@link XOAD_Cache_Storage_Files} Class.</p>
  6. * <p>You should not include this file directly. It is used
  7. * by {@link XOAD_Cache} extension.</p>
  8. *
  9. * @author Stanimir Angeloff
  10. *
  11. * @package XOAD
  12. *
  13. * @subpackage XOAD_Cache
  14. *
  15. * @version 0.6.0.0
  16. *
  17. */
  18.  
  19. /**
  20. * XOAD_Cache Storage Files Class.
  21. *
  22. * <p>This class is a {@link XOAD_Cache_Storage} successor.</p>
  23. *
  24. * @author Stanimir Angeloff
  25. *
  26. * @package XOAD
  27. *
  28. * @subpackage XOAD_Cache
  29. *
  30. * @version 0.6.0.0
  31. *
  32. */
  33. class XOAD_Cache_Storage_Files extends XOAD_Cache_Storage
  34. {
  35. /**
  36. * Holds the directory where the cached data is saved.
  37. *
  38. * @access protected
  39. *
  40. * @var string
  41. *
  42. */
  43. var $container = 'cache';
  44.  
  45. /**
  46. * Creates a new instance of the {@link XOAD_Cache_Storage_Files} class.
  47. *
  48. * @access public
  49. *
  50. * @param string $dsn The data source name and parameters to use
  51. * when creating the instance.
  52. *
  53. */
  54. function XOAD_Cache_Storage_Files($dsn)
  55. {
  56. parent::XOAD_Cache_Storage($dsn);
  57. }
  58.  
  59. /**
  60. * Gets the absolute path to the cache file.
  61. *
  62. * @access private
  63. *
  64. * @param string $id The ID of the cached data.
  65. *
  66. * @return string
  67. *
  68. */
  69. function getFileName($id)
  70. {
  71. if (strpos($this->container, DIRECTORY_SEPARATOR) === 0) {
  72.  
  73. return $this->container . DIRECTORY_SEPARATOR . $id;
  74. }
  75.  
  76. if (strlen($this->container) >= 3) {
  77.  
  78. if (
  79. ($this->container{1} == ':') &&
  80. ($this->container{2} == DIRECTORY_SEPARATOR)) {
  81.  
  82. return $this->container . DIRECTORY_SEPARATOR . $id;
  83. }
  84. }
  85.  
  86. return XOAD_BASE . '/var/' . $this->container . '/' . $id;
  87. }
  88.  
  89. /**
  90. * Deletes old data from the cache.
  91. *
  92. * <p>This method is called before calling {@link load} to
  93. * delete all expired data from the cache.</p>
  94. *
  95. * @access public
  96. *
  97. * @return bool true on success, false otherwise.
  98. *
  99. */
  100. function collectGarbage()
  101. {
  102. $directory = $this->getFileName('');
  103.  
  104. clearstatcache();
  105.  
  106. $handle = @dir($directory);
  107.  
  108. if ( ! $handle) {
  109.  
  110. return false;
  111. }
  112.  
  113. $time = time();
  114.  
  115. while (false !== ($fileName = $handle->read())) {
  116.  
  117. if (
  118. ($fileName == '.') ||
  119. ($fileName == '..')) {
  120.  
  121. continue;
  122. }
  123.  
  124. $realFile = $directory . $fileName;
  125.  
  126. if ( ! is_file($realFile)) {
  127.  
  128. continue;
  129. }
  130.  
  131. $fileHandle = @fopen($realFile, 'rb');
  132.  
  133. if ( ! $fileHandle) {
  134.  
  135. continue;
  136. }
  137.  
  138. $expire = fread($fileHandle, 10);
  139.  
  140. fclose($fileHandle);
  141.  
  142. if ($expire < $time) {
  143.  
  144. @unlink($realFile);
  145. }
  146. }
  147.  
  148. $handle->close();
  149.  
  150. return true;
  151. }
  152.  
  153. /**
  154. * Loads data from the cache with a given ID.
  155. *
  156. * @access public
  157. *
  158. * @param string $id The ID of the cached data.
  159. *
  160. * @return mixed The data in the cache with the given ID or null.
  161. *
  162. */
  163. function load($id)
  164. {
  165. $fileName = $this->getFileName($id);
  166.  
  167. clearstatcache();
  168.  
  169. if ( ! file_exists($fileName)) {
  170.  
  171. return null;
  172. }
  173.  
  174. $length = filesize($fileName);
  175.  
  176. $handle = fopen($fileName, 'rb');
  177.  
  178. if ( ! $handle) {
  179.  
  180. return null;
  181. }
  182.  
  183. flock($handle, LOCK_SH);
  184.  
  185. $contents = fread($handle, $length);
  186.  
  187. list($expire, $data) = explode("\t", $contents, 2);
  188.  
  189. flock($handle, LOCK_UN);
  190.  
  191. fclose($handle);
  192.  
  193. if ($expire >= time()) {
  194.  
  195. return $data;
  196. }
  197.  
  198. return null;
  199. }
  200.  
  201. /**
  202. * Saves data in the cache with a given ID and lifetime.
  203. *
  204. * @access public
  205. *
  206. * @param mixed $id The ID to use when saving the data.
  207. *
  208. * @param int $expires The lifetime time in seconds for the
  209. * cached data.
  210. *
  211. * @param mixed $data The data to cache.
  212. *
  213. * @return bool True on success, false otherwise.
  214. *
  215. */
  216. function save($id, $expires, $data)
  217. {
  218. $fileName = $this->getFileName($id);
  219.  
  220. if (empty($expires)) {
  221.  
  222. $expires = XOAD_CACHE_LIFETIME;
  223. }
  224.  
  225. $handle = @fopen($fileName, 'a+b');
  226.  
  227. if ( ! $handle) {
  228.  
  229. return false;
  230. }
  231.  
  232. @ignore_user_abort(true);
  233.  
  234. flock($handle, LOCK_EX);
  235.  
  236. ftruncate($handle, 0);
  237.  
  238. fwrite($handle, time() + $expires . "\t");
  239. fwrite($handle, $data);
  240.  
  241. flock($handle, LOCK_UN);
  242.  
  243. fclose($handle);
  244.  
  245. @ignore_user_abort(false);
  246.  
  247. return true;
  248. }
  249. }
  250. ?>

Documentation generated on Sat, 12 Nov 2005 20:24:10 +0200 by phpDocumentor 1.3.0RC3