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-08-05 16:47:03 +00:00
|
|
|
if (!class_exists('folder')) {
|
2007-05-13 21:54:48 +00:00
|
|
|
uses ('folder');
|
|
|
|
}
|
2007-08-28 23:34:14 +00:00
|
|
|
if (!class_exists('file')) {
|
|
|
|
uses ('file');
|
|
|
|
}
|
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-09-30 07:45:34 +00:00
|
|
|
* instance of Folder class
|
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
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $__Folder = null;
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* instance of File class
|
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
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $__File = null;
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +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
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @see var __defaults
|
|
|
|
* @var array
|
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $settings = array();
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Initialize the Cache Engine
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
2007-09-30 07:45:34 +00:00
|
|
|
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param array $setting array of setting for the engine
|
|
|
|
* @return boolean True if the engine has been successfully initialized, false if not
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function init($settings = array()) {
|
|
|
|
parent::init($settings);
|
|
|
|
$defaults = array('path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false, 'serialize'=> true);
|
|
|
|
$this->settings = am($this->settings, $defaults, $settings);
|
|
|
|
$this->__Folder =& new Folder($this->settings['path']);
|
|
|
|
$this->settings['path'] = $this->__Folder->pwd();
|
|
|
|
if (!is_writable($this->settings['path'])) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Write data for key into cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param mixed $data Data to be cached
|
2007-04-30 06:01:54 +00:00
|
|
|
* @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
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function write($key, &$data, $duration) {
|
2007-08-28 23:34:14 +00:00
|
|
|
if (!$data) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($duration == null) {
|
|
|
|
$duration = $this->settings['duration'];
|
2007-06-23 04:48:29 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if (isset($this->settings['serialize'])) {
|
|
|
|
$data = serialize($data);
|
|
|
|
}
|
|
|
|
if (!$data) {
|
2007-06-23 04:48:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$file = $this->fullpath($key);
|
|
|
|
if ($file === false) {
|
|
|
|
return false;
|
2007-06-23 04:48:29 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$expires = time() + $duration;
|
|
|
|
return $this->__write($file, $data, $expires);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Read a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @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) {
|
2007-09-30 07:45:34 +00:00
|
|
|
$file = $this->fullpath($key);
|
|
|
|
if ($file === false || !is_file($file) || !is_readable($file)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$fp = fopen($file, 'r');
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!$fp) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($this->settings['lock'] && !flock($fp, LOCK_SH)) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-08-05 16:47:03 +00:00
|
|
|
$cachetime = fgets($fp, 11);
|
|
|
|
if (intval($cachetime) < time()) {
|
2007-04-30 06:01:54 +00:00
|
|
|
fclose($fp);
|
2007-09-30 07:45:34 +00:00
|
|
|
unlink($file);
|
2007-04-30 06:01:54 +00:00
|
|
|
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);
|
2007-09-30 07:45:34 +00:00
|
|
|
if (isset($this->settings['serialize'])) {
|
|
|
|
return unserialize($data);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
return $data;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Delete a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2007-09-30 07:45:34 +00:00
|
|
|
* @return boolean True if the value was successfully 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) {
|
2007-09-30 07:45:34 +00:00
|
|
|
$file = $this->fullpath($key);
|
|
|
|
if ($file === false) {
|
2007-06-23 04:48:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
return unlink($file);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Delete all values from the cache
|
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param boolean $check Optional - only delete expired cache items
|
2007-04-30 06:01:54 +00:00
|
|
|
* @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
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function clear($check) {
|
|
|
|
$dir = dir($this->settings['path']);
|
|
|
|
if ($check) {
|
2007-04-30 06:01:54 +00:00
|
|
|
$now = time();
|
|
|
|
$threshold = $now - 86400;
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
while (($entry = $dir->read()) !== false) {
|
2007-09-30 07:45:34 +00:00
|
|
|
if (strpos($entry, $this->settings['prefix']) !== 0) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$file = $this->settings['path'] . $entry;
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($check) {
|
|
|
|
$mtime = filemtime($file);
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($mtime === false || $mtime > $threshold) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$expires = $this->__expires($file);
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($expires > $now) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
unlink($file);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Get absolute file for a given key
|
2007-04-30 07:38:47 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param string $key The key
|
|
|
|
* @return mixed Absolute cache file for the given key or false if erroneous
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function fullpath($key) {
|
|
|
|
if (!isset($this->__File)) {
|
|
|
|
$this->__File =& new File($this->settings['path']);
|
|
|
|
}
|
|
|
|
$parts = array_map(array($this->__File , 'safe'), explode(DS, $key));
|
|
|
|
$key = array_pop($parts);
|
|
|
|
$dir = implode(DS, $parts) . DS;
|
|
|
|
$path = str_replace(DS . DS, DS, $this->settings['path'] . $dir);
|
|
|
|
$fullpath = $this->__Folder->realpath($path . $this->settings['prefix'] . $key);
|
|
|
|
if (!$this->__Folder->inPath($fullpath, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $fullpath;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* write data to a file
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $value
|
|
|
|
* @param integer $expires
|
|
|
|
* @return boolean True on success, false on failure
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __write(&$file, &$data, &$expires) {
|
|
|
|
$dir = dirname($file);
|
|
|
|
if (!is_writable($dir)) {
|
|
|
|
if (!$this->__Folder->create($dir)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$contents = $expires."\n".$data."\n";
|
|
|
|
return ife(file_put_contents($file, $contents, ife($this->settings['lock'], LOCK_EX, 0)), true, false);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the time to live for cache
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return mixed Expiration timestamp, or false on failure
|
|
|
|
* @access private
|
2007-04-30 07:38:47 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function __expires($file) {
|
|
|
|
$fp = fopen($file, 'r');
|
|
|
|
if (!$fp) {
|
|
|
|
return false;
|
2007-04-30 07:38:47 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($this->settings['lock'] && !flock($fp, LOCK_SH)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$expires = intval(fgets($fp, 11));
|
|
|
|
fclose($fp);
|
|
|
|
return $expires;
|
2007-04-30 07:38:47 +00:00
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
?>
|