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

/cake/libs/cache/file.php

https://github.com/hardsshah/bookmarks
PHP | 270 lines | 167 code | 6 blank | 97 comment | 30 complexity | 3108356641f7aa49c171efa3672758d6 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * File Storage engine for cache
  5. *
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  10. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @filesource
  16. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  17. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  18. * @package cake
  19. * @subpackage cake.cake.libs.cache
  20. * @since CakePHP(tm) v 1.2.0.4933
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  25. */
  26. /**
  27. * File Storage engine for cache
  28. *
  29. * @todo use the File and Folder classes (if it's not a too big performance hit)
  30. * @package cake
  31. * @subpackage cake.cake.libs.cache
  32. */
  33. class FileEngine extends CacheEngine {
  34. /**
  35. * Instance of File class
  36. *
  37. * @var File
  38. * @access private
  39. */
  40. var $__File = null;
  41. /**
  42. * settings
  43. * path = absolute path to cache directory, default => CACHE
  44. * prefix = string prefix for filename, default => cake_
  45. * lock = enable file locking on write, default => false
  46. * serialize = serialize the data, default => true
  47. *
  48. * @var array
  49. * @see CacheEngine::__defaults
  50. * @access public
  51. */
  52. var $settings = array();
  53. /**
  54. * Set to true if FileEngine::init(); and FileEngine::__active(); do not fail.
  55. *
  56. * @var boolean
  57. * @access private
  58. */
  59. var $__active = false;
  60. /**
  61. * True unless FileEngine::__active(); fails
  62. *
  63. * @var boolean
  64. * @access private
  65. */
  66. var $__init = true;
  67. /**
  68. * Initialize the Cache Engine
  69. *
  70. * Called automatically by the cache frontend
  71. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  72. *
  73. * @param array $setting array of setting for the engine
  74. * @return boolean True if the engine has been successfully initialized, false if not
  75. * @access public
  76. */
  77. function init($settings = array()) {
  78. parent::init(array_merge(
  79. array(
  80. 'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false,
  81. 'serialize'=> true, 'isWindows' => false
  82. ),
  83. $settings
  84. ));
  85. if (!isset($this->__File)) {
  86. if (!class_exists('File')) {
  87. require LIBS . 'file.php';
  88. }
  89. $this->__File =& new File($this->settings['path'] . DS . 'cake');
  90. }
  91. if (DIRECTORY_SEPARATOR === '\\') {
  92. $this->settings['isWindows'] = true;
  93. }
  94. $this->settings['path'] = $this->__File->Folder->cd($this->settings['path']);
  95. if (empty($this->settings['path'])) {
  96. return false;
  97. }
  98. return $this->__active();
  99. }
  100. /**
  101. * Garbage collection. Permanently remove all expired and deleted data
  102. *
  103. * @return boolean True if garbage collection was succesful, false on failure
  104. * @access public
  105. */
  106. function gc() {
  107. return $this->clear(true);
  108. }
  109. /**
  110. * Write data for key into cache
  111. *
  112. * @param string $key Identifier for the data
  113. * @param mixed $data Data to be cached
  114. * @param mixed $duration How long to cache the data, in seconds
  115. * @return boolean True if the data was succesfully cached, false on failure
  116. * @access public
  117. */
  118. function write($key, &$data, $duration) {
  119. if ($data === '' || !$this->__init) {
  120. return false;
  121. }
  122. if ($this->__setKey($key) === false) {
  123. return false;
  124. }
  125. $lineBreak = "\n";
  126. if ($this->settings['isWindows']) {
  127. $lineBreak = "\r\n";
  128. }
  129. if (!empty($this->settings['serialize'])) {
  130. if ($this->settings['isWindows']) {
  131. $data = str_replace('\\', '\\\\\\\\', serialize($data));
  132. } else {
  133. $data = serialize($data);
  134. }
  135. }
  136. if ($this->settings['lock']) {
  137. $this->__File->lock = true;
  138. }
  139. $expires = time() + $duration;
  140. $contents = $expires . $lineBreak . $data . $lineBreak;
  141. $success = $this->__File->write($contents);
  142. $this->__File->close();
  143. return $success;
  144. }
  145. /**
  146. * Read a key from the cache
  147. *
  148. * @param string $key Identifier for the data
  149. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  150. * @access public
  151. */
  152. function read($key) {
  153. if ($this->__setKey($key) === false || !$this->__init || !$this->__File->exists()) {
  154. return false;
  155. }
  156. if ($this->settings['lock']) {
  157. $this->__File->lock = true;
  158. }
  159. $time = time();
  160. $cachetime = intval($this->__File->read(11));
  161. if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
  162. $this->__File->close();
  163. $this->__File->delete();
  164. return false;
  165. }
  166. $data = $this->__File->read(true);
  167. if ($data !== '' && !empty($this->settings['serialize'])) {
  168. if ($this->settings['isWindows']) {
  169. $data = str_replace('\\\\\\\\', '\\', $data);
  170. }
  171. $data = unserialize((string)$data);
  172. }
  173. $this->__File->close();
  174. return $data;
  175. }
  176. /**
  177. * Delete a key from the cache
  178. *
  179. * @param string $key Identifier for the data
  180. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  181. * @access public
  182. */
  183. function delete($key) {
  184. if ($this->__setKey($key) === false || !$this->__init) {
  185. return false;
  186. }
  187. return $this->__File->delete();
  188. }
  189. /**
  190. * Delete all values from the cache
  191. *
  192. * @param boolean $check Optional - only delete expired cache items
  193. * @return boolean True if the cache was succesfully cleared, false otherwise
  194. * @access public
  195. */
  196. function clear($check) {
  197. if (!$this->__init) {
  198. return false;
  199. }
  200. $dir = dir($this->settings['path']);
  201. if ($check) {
  202. $now = time();
  203. $threshold = $now - $this->settings['duration'];
  204. }
  205. while (($entry = $dir->read()) !== false) {
  206. if ($this->__setKey($entry) === false) {
  207. continue;
  208. }
  209. if ($check) {
  210. $mtime = $this->__File->lastChange();
  211. if ($mtime === false || $mtime > $threshold) {
  212. continue;
  213. }
  214. $expires = $this->__File->read(11);
  215. $this->__File->close();
  216. if ($expires > $now) {
  217. continue;
  218. }
  219. }
  220. $this->__File->delete();
  221. }
  222. $dir->close();
  223. return true;
  224. }
  225. /**
  226. * Get absolute file for a given key
  227. *
  228. * @param string $key The key
  229. * @return mixed Absolute cache file for the given key or false if erroneous
  230. * @access private
  231. */
  232. function __setKey($key) {
  233. $this->__File->Folder->cd($this->settings['path']);
  234. if ($key !== $this->__File->name) {
  235. $this->__File->name = $key;
  236. $this->__File->path = null;
  237. }
  238. if (!$this->__File->Folder->inPath($this->__File->pwd(), true)) {
  239. return false;
  240. }
  241. }
  242. /**
  243. * Determine is cache directory is writable
  244. *
  245. * @return boolean
  246. * @access private
  247. */
  248. function __active() {
  249. if (!$this->__active && $this->__init && !is_writable($this->settings['path'])) {
  250. $this->__init = false;
  251. trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
  252. } else {
  253. $this->__active = true;
  254. }
  255. return true;
  256. }
  257. }
  258. ?>