2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* File Storage engine for cache
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-31 02:46:14 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-31 02:46:14 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-02-23 04:21:30 +00:00
|
|
|
if (!class_exists('File')) {
|
|
|
|
require LIBS . 'file.php';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* File Storage engine for cache
|
|
|
|
*
|
|
|
|
* @todo use the File and Folder classes (if it's not a too big performance hit)
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class FileEngine extends CacheEngine {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Instance of File class
|
|
|
|
*
|
2009-03-17 21:10:28 +00:00
|
|
|
* @var File
|
2010-02-23 04:36:47 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-02-23 04:36:47 +00:00
|
|
|
var $_File = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-21 21:09:06 +00:00
|
|
|
* 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
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see CacheEngine::__defaults
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $settings = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* True unless FileEngine::__active(); fails
|
|
|
|
*
|
|
|
|
* @var boolean
|
2010-02-23 04:36:47 +00:00
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-02-23 04:36:47 +00:00
|
|
|
var $_init = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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()) {
|
2008-06-24 01:09:54 +00:00
|
|
|
parent::init(array_merge(
|
|
|
|
array(
|
|
|
|
'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false,
|
|
|
|
'serialize'=> true, 'isWindows' => false
|
|
|
|
),
|
|
|
|
$settings
|
|
|
|
));
|
2010-02-23 04:36:47 +00:00
|
|
|
if (!isset($this->_File)) {
|
|
|
|
$this->_File =& new File($this->settings['path'] . DS . 'cake');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-24 01:09:54 +00:00
|
|
|
|
2008-09-14 17:17:49 +00:00
|
|
|
if (DIRECTORY_SEPARATOR === '\\') {
|
2008-06-24 01:09:54 +00:00
|
|
|
$this->settings['isWindows'] = true;
|
|
|
|
}
|
|
|
|
|
2010-02-23 04:36:47 +00:00
|
|
|
$path = $this->_File->Folder->cd($this->settings['path']);
|
2010-02-23 04:21:30 +00:00
|
|
|
if ($path) {
|
|
|
|
$this->settings['path'] = $path;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $this->__active();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($data === '' || !$this->_init) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($this->_setKey($key) === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lineBreak = "\n";
|
|
|
|
|
2008-06-24 01:09:54 +00:00
|
|
|
if ($this->settings['isWindows']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$lineBreak = "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->settings['serialize'])) {
|
2008-06-24 01:09:54 +00:00
|
|
|
if ($this->settings['isWindows']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = str_replace('\\', '\\\\\\\\', serialize($data));
|
|
|
|
} else {
|
|
|
|
$data = serialize($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$expires = time() + $duration;
|
|
|
|
$contents = $expires . $lineBreak . $data . $lineBreak;
|
2011-05-10 18:27:01 +00:00
|
|
|
|
2011-08-26 02:12:33 +00:00
|
|
|
if (!$handle = fopen($this->_File->path, 'a')) {
|
2011-05-10 18:27:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->settings['lock']) {
|
|
|
|
flock($handle, LOCK_EX);
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = ftruncate($handle, 0) && fwrite($handle, $contents) && fflush($handle);
|
|
|
|
|
|
|
|
if ($this->settings['lock']) {
|
|
|
|
flock($handle, LOCK_UN);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($handle);
|
2008-05-30 11:40:08 +00:00
|
|
|
return $success;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($this->_setKey($key) === false || !$this->_init || !$this->_File->exists()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->settings['lock']) {
|
2010-02-23 04:36:47 +00:00
|
|
|
$this->_File->lock = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-26 20:00:20 +00:00
|
|
|
$time = time();
|
2010-02-23 04:36:47 +00:00
|
|
|
$cachetime = intval($this->_File->read(11));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-26 20:00:20 +00:00
|
|
|
if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
|
2010-02-23 04:36:47 +00:00
|
|
|
$this->_File->close();
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
$data = $this->_File->read(true);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($data !== '' && !empty($this->settings['serialize'])) {
|
2008-06-24 01:09:54 +00:00
|
|
|
if ($this->settings['isWindows']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = str_replace('\\\\\\\\', '\\', $data);
|
|
|
|
}
|
2008-07-24 02:23:46 +00:00
|
|
|
$data = unserialize((string)$data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
$this->_File->close();
|
2008-05-30 11:40:08 +00:00
|
|
|
return $data;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($this->_setKey($key) === false || !$this->_init) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
return $this->_File->delete();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2010-02-23 04:36:47 +00:00
|
|
|
if (!$this->_init) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$dir = dir($this->settings['path']);
|
|
|
|
if ($check) {
|
|
|
|
$now = time();
|
|
|
|
$threshold = $now - $this->settings['duration'];
|
|
|
|
}
|
2010-05-27 04:01:17 +00:00
|
|
|
$prefixLength = strlen($this->settings['prefix']);
|
2008-05-30 11:40:08 +00:00
|
|
|
while (($entry = $dir->read()) !== false) {
|
2010-05-27 04:01:17 +00:00
|
|
|
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($this->_setKey($entry) === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($check) {
|
2010-02-23 04:36:47 +00:00
|
|
|
$mtime = $this->_File->lastChange();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($mtime === false || $mtime > $threshold) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-23 04:36:47 +00:00
|
|
|
$expires = $this->_File->read(11);
|
|
|
|
$this->_File->close();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($expires > $now) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
$this->_File->delete();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-02-23 04:36:47 +00:00
|
|
|
function _setKey($key) {
|
|
|
|
$this->_File->Folder->cd($this->settings['path']);
|
|
|
|
if ($key !== $this->_File->name) {
|
|
|
|
$this->_File->name = $key;
|
|
|
|
$this->_File->path = null;
|
2008-11-05 19:32:45 +00:00
|
|
|
}
|
2010-02-23 04:36:47 +00:00
|
|
|
if (!$this->_File->Folder->inPath($this->_File->pwd(), true)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determine is cache directory is writable
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __active() {
|
2010-02-23 04:36:47 +00:00
|
|
|
if ($this->_init && !is_writable($this->settings['path'])) {
|
|
|
|
$this->_init = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
|
2010-09-19 16:20:07 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|