2007-04-30 06:01:54 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* File Storage engine for cache
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, 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-2007, 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$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2007-05-13 21:54:48 +00:00
|
|
|
/**
|
|
|
|
* Included libraries.
|
|
|
|
*
|
|
|
|
*/
|
2007-06-23 04:48:29 +00:00
|
|
|
if (!class_exists('File')) {
|
|
|
|
uses ('file');
|
|
|
|
}
|
2007-05-13 21:54:48 +00:00
|
|
|
if (!class_exists('Folder')) {
|
|
|
|
uses ('folder');
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2007-06-23 04:48:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the cache engine handle prefixes on it's own?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $_usesPrefixes = true;
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-05-13 21:54:48 +00:00
|
|
|
* Cache directory
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @var string
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
var $_dir = '';
|
|
|
|
/**
|
2007-05-13 21:54:48 +00:00
|
|
|
* Cache filename prefix
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @var string
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
var $_prefix = '';
|
|
|
|
/**
|
2007-05-13 21:54:48 +00:00
|
|
|
* Use locking
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @var boolean
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
var $_lock = false;
|
|
|
|
/**
|
|
|
|
* Set up the cache engine
|
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
|
|
|
*
|
|
|
|
* @param array $params Associative array of parameters for the engine
|
|
|
|
* @return boolean True if the engine has been succesfully initialized, false if not
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function init($params) {
|
|
|
|
$dir = CACHE;
|
|
|
|
$prefix = 'cake_';
|
|
|
|
$lock = false;
|
|
|
|
extract($params);
|
|
|
|
$dir = trim($dir);
|
2007-05-13 21:54:48 +00:00
|
|
|
$folder =& new Folder();
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-05-13 21:54:48 +00:00
|
|
|
if (!empty($dir)) {
|
|
|
|
$dir = $folder->slashTerm($dir);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (empty($dir) || !$folder->isAbsolute($dir) || !is_writable($dir)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_dir = $dir;
|
|
|
|
$this->_prefix = strval($prefix);
|
|
|
|
$this->_lock = $lock;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Garbage collection
|
|
|
|
* Permanently remove all expired and deleted data
|
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @return boolean True if garbage collection was succesful, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function gc() {
|
|
|
|
return $this->clear(true);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Write a value in the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value 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
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function write($key, &$value, $duration = CACHE_DEFAULT_DURATION) {
|
|
|
|
$serialized = serialize($value);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$serialized) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$expires = time() + $duration;
|
2007-06-23 04:48:29 +00:00
|
|
|
|
|
|
|
$fileName = $this->_getFilename($key);
|
|
|
|
if ($fileName === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_writeCache($fileName, $serialized, $expires);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get absolute filename for a key
|
|
|
|
*
|
|
|
|
* @param string $key The key
|
2007-06-23 04:48:29 +00:00
|
|
|
* @return mixed Absolute cache filename for the given key or false if erroneous
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function _getFilename($key) {
|
2007-06-23 04:48:29 +00:00
|
|
|
$fullpath = $this->_dir . $key;
|
|
|
|
$directoryName = dirname($fullpath);
|
|
|
|
$fileName = $this->_prefix.basename($fullpath);
|
|
|
|
$fullpath = Folder::realpath($directoryName . DS . $fileName);
|
|
|
|
|
|
|
|
$folder = new Folder($this->_dir);
|
|
|
|
if (!$folder->inPath($fullpath, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $fullpath;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* write serialized data to a file
|
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @param string $filename
|
|
|
|
* @param string $value
|
|
|
|
* @param integer $expires
|
|
|
|
* @return boolean True on success, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function _writeCache(&$filename, &$value, &$expires) {
|
2007-06-23 04:48:29 +00:00
|
|
|
$directoryName = dirname($filename);
|
|
|
|
if (!is_writable($directoryName)) {
|
|
|
|
$folder = new Folder($directoryName);
|
|
|
|
if (!$folder->create($directoryName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
$contents = $expires."\n".$value."\n";
|
|
|
|
return ife(file_put_contents($filename, $contents, ife($this->_lock, LOCK_EX, 0)), true, false);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Read a value 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
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function read($key) {
|
|
|
|
$filename = $this->_getFilename($key);
|
|
|
|
|
2007-06-23 04:48:29 +00:00
|
|
|
if ($filename === false || !is_file($filename) || !is_readable($filename)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$fp = fopen($filename, 'r');
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$fp) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->_lock && !flock($fp, LOCK_SH)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$expires = fgets($fp, 11);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (intval($expires) < time()) {
|
2007-04-30 06:01:54 +00:00
|
|
|
fclose($fp);
|
|
|
|
unlink($filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data = '';
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
while (!feof($fp)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
$data .= fgets($fp, 4096);
|
|
|
|
}
|
|
|
|
$data = trim($data);
|
|
|
|
return unserialize($data);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the expiry time for a cache file
|
|
|
|
*
|
2007-05-13 21:54:48 +00:00
|
|
|
* @param string $filename
|
|
|
|
* @return mixed Expiration timestamp, or false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function _getExpiry($filename) {
|
|
|
|
$fp = fopen($filename, 'r');
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$fp) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->_lock && !flock($fp, LOCK_SH)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$expires = intval(fgets($fp, 11));
|
|
|
|
fclose($fp);
|
|
|
|
return $expires;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Delete a value from the cache
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function delete($key) {
|
|
|
|
$filename = $this->_getFilename($key);
|
2007-06-23 04:48:29 +00:00
|
|
|
if ($filename === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
return unlink($filename);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Delete all values from the cache
|
|
|
|
*
|
|
|
|
* @param boolean $checkExpiry Optional - only delete expired cache items
|
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function clear($checkExpiry = false) {
|
|
|
|
$dir = dir($this->_dir);
|
|
|
|
|
|
|
|
if ($checkExpiry) {
|
|
|
|
$now = time();
|
|
|
|
$threshold = $now - 86400;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
while (($entry = $dir->read()) !== false) {
|
|
|
|
if (strpos($entry, $this->_prefix) !== 0) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$filename = $this->_dir.$entry;
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($checkExpiry) {
|
2007-04-30 06:01:54 +00:00
|
|
|
$mtime = filemtime($filename);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($mtime === false || $mtime > $threshold) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$expires = $this->_getExpiry($filename);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($expires > $now) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unlink($filename);
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
/**
|
|
|
|
* Return the settings for this cache engine
|
|
|
|
*
|
|
|
|
* @return array list of settings for this engine
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 07:38:47 +00:00
|
|
|
*/
|
|
|
|
function settings() {
|
|
|
|
$lock = 'false';
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->_lock) {
|
2007-04-30 07:38:47 +00:00
|
|
|
$lock = 'true';
|
|
|
|
}
|
|
|
|
return array('class' => get_class($this),
|
|
|
|
'directory' => $this->_dir,
|
|
|
|
'prefix' => $this->_prefix,
|
|
|
|
'lock' => $lock);
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
?>
|