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