2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-03-12 17:14:43 +00:00
|
|
|
* File Storage engine for cache. Filestorage is the slowest cache storage
|
|
|
|
* to read and write. However, it is good for servers that don't have other storage
|
|
|
|
* engine available, or have content which is not performance sensitive.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-03-12 17:14:43 +00:00
|
|
|
* You can configure a FileEngine cache, using Cache::config()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, 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.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, 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
|
|
|
* @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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* File Storage engine for cache. Filestorage is the slowest cache storage
|
|
|
|
* to read and write. However, it is good for servers that don't have other storage
|
|
|
|
* engine available, or have content which is not performance sensitive.
|
|
|
|
*
|
|
|
|
* You can configure a FileEngine cache, using Cache::config()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Cache.Engine
|
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
|
|
|
/**
|
2010-04-27 02:12:16 +00:00
|
|
|
* Instance of SplFileObject class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @var File
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_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
|
2011-03-19 16:58:47 +00:00
|
|
|
*
|
2009-11-21 21:09:06 +00:00
|
|
|
* - 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
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $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-04-04 06:36:12 +00:00
|
|
|
protected $_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());
|
|
|
|
*
|
2011-04-17 06:13:03 +00:00
|
|
|
* @param array $settings array of setting for the engine
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True if the engine has been successfully initialized, false if not
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function init($settings = array()) {
|
2008-06-24 01:09:54 +00:00
|
|
|
parent::init(array_merge(
|
|
|
|
array(
|
2011-12-01 07:21:31 +00:00
|
|
|
'engine' => 'File', 'path' => CACHE, 'prefix' => 'cake_', 'lock' => true,
|
|
|
|
'serialize' => true, 'isWindows' => false, 'mask' => 0664
|
2008-06-24 01:09:54 +00:00
|
|
|
),
|
|
|
|
$settings
|
|
|
|
));
|
|
|
|
|
2010-07-01 22:18:12 +00:00
|
|
|
if (DS === '\\') {
|
2008-06-24 01:09:54 +00:00
|
|
|
$this->settings['isWindows'] = true;
|
|
|
|
}
|
2010-07-01 22:18:12 +00:00
|
|
|
if (substr($this->settings['path'], -1) !== DS) {
|
|
|
|
$this->settings['path'] .= DS;
|
|
|
|
}
|
2010-04-26 07:02:32 +00:00
|
|
|
return $this->_active();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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
|
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* @return boolean True if garbage collection was successful, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function gc() {
|
2008-05-30 11:40:08 +00:00
|
|
|
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
|
2011-04-17 06:13:03 +00:00
|
|
|
* @return boolean True if the data was successfully cached, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-18 04:38:36 +00:00
|
|
|
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-04-26 07:02:32 +00:00
|
|
|
if ($this->_setKey($key, true) === 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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->settings['lock']) {
|
2011-12-19 13:18:28 +00:00
|
|
|
$this->_File->flock(LOCK_EX);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-27 02:12:16 +00:00
|
|
|
|
2011-10-19 01:21:05 +00:00
|
|
|
$this->_File->rewind();
|
2011-10-11 11:26:03 +00:00
|
|
|
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
|
2010-04-27 02:12:16 +00:00
|
|
|
|
|
|
|
if ($this->settings['lock']) {
|
2011-12-19 13:18:28 +00:00
|
|
|
$this->_File->flock(LOCK_UN);
|
2010-04-27 02:12:16 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($key) {
|
2010-04-26 07:02:32 +00:00
|
|
|
if (!$this->_init || $this->_setKey($key) === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-27 02:12:16 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->settings['lock']) {
|
2010-04-27 02:12:16 +00:00
|
|
|
$this->_File->flock(LOCK_SH);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-27 02:12:16 +00:00
|
|
|
|
2010-04-26 07:02:32 +00:00
|
|
|
$this->_File->rewind();
|
2008-07-26 20:00:20 +00:00
|
|
|
$time = time();
|
2010-04-26 07:02:32 +00:00
|
|
|
$cachetime = intval($this->_File->current());
|
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)) {
|
2011-09-25 13:26:32 +00:00
|
|
|
if ($this->settings['lock']) {
|
|
|
|
$this->_File->flock(LOCK_UN);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-19 16:58:47 +00:00
|
|
|
|
2010-04-26 07:02:32 +00:00
|
|
|
$data = '';
|
|
|
|
$this->_File->next();
|
|
|
|
while ($this->_File->valid()) {
|
|
|
|
$data .= $this->_File->current();
|
|
|
|
$this->_File->next();
|
|
|
|
}
|
2010-04-27 02:12:16 +00:00
|
|
|
|
|
|
|
if ($this->settings['lock']) {
|
2010-07-08 02:36:13 +00:00
|
|
|
$this->_File->flock(LOCK_UN);
|
2010-04-27 02:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 07:02:32 +00:00
|
|
|
$data = trim($data);
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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-07-08 02:36:13 +00:00
|
|
|
$path = $this->_File->getRealPath();
|
|
|
|
$this->_File = null;
|
|
|
|
return unlink($path);
|
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
|
2011-04-17 06:13:03 +00:00
|
|
|
* @return boolean True if the cache was successfully cleared, false otherwise
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
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-04-26 07:02:32 +00:00
|
|
|
$mtime = $this->_File->getMTime();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-04-26 07:02:32 +00:00
|
|
|
if ($mtime > $threshold) {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-26 07:02:32 +00:00
|
|
|
$expires = (int)$this->_File->current();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($expires > $now) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-07-08 02:36:13 +00:00
|
|
|
$path = $this->_File->getRealPath();
|
|
|
|
$this->_File = null;
|
2011-08-27 15:52:29 +00:00
|
|
|
if (file_exists($path)) {
|
|
|
|
unlink($path);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-18 04:38:36 +00:00
|
|
|
/**
|
|
|
|
* Not implemented
|
|
|
|
*
|
2011-07-30 21:02:25 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param integer $offset
|
2010-04-18 04:38:36 +00:00
|
|
|
* @return void
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CacheException
|
2010-04-18 04:38:36 +00:00
|
|
|
*/
|
|
|
|
public function decrement($key, $offset = 1) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CacheException(__d('cake_dev', 'Files cannot be atomically decremented.'));
|
2010-04-18 04:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not implemented
|
|
|
|
*
|
2011-07-30 21:02:25 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param integer $offset
|
2010-04-18 04:38:36 +00:00
|
|
|
* @return void
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CacheException
|
2010-04-18 04:38:36 +00:00
|
|
|
*/
|
|
|
|
public function increment($key, $offset = 1) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CacheException(__d('cake_dev', 'Files cannot be atomically incremented.'));
|
2010-04-18 04:38:36 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-11 11:26:03 +00:00
|
|
|
* Sets the current cache key this class is managing, and creates a writable SplFileObject
|
2011-12-02 05:58:09 +00:00
|
|
|
* for the cache file the key is referring to.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key The key
|
2010-04-27 02:12:16 +00:00
|
|
|
* @param boolean $createKey Whether the key should be created if it doesn't exists, or not
|
|
|
|
* @return boolean true if the cache key could be set, false otherwise
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 07:02:32 +00:00
|
|
|
protected function _setKey($key, $createKey = false) {
|
2010-07-01 22:18:12 +00:00
|
|
|
$path = new SplFileInfo($this->settings['path'] . $key);
|
2010-04-26 07:02:32 +00:00
|
|
|
|
|
|
|
if (!$createKey && !$path->isFile()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-26 07:02:32 +00:00
|
|
|
if (empty($this->_File) || $this->_File->getBaseName() !== $key) {
|
2011-10-11 11:26:03 +00:00
|
|
|
$exists = file_exists($path->getPathname());
|
|
|
|
try {
|
|
|
|
$this->_File = $path->openFile('c+');
|
|
|
|
} catch (Exception $e) {
|
2011-10-16 22:51:02 +00:00
|
|
|
trigger_error($e->getMessage(), E_USER_WARNING);
|
2011-10-11 11:26:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unset($path);
|
2010-04-27 02:12:16 +00:00
|
|
|
|
2012-03-03 22:34:13 +00:00
|
|
|
if (!$exists && !chmod($this->_File->getPathname(), (int)$this->settings['mask'])) {
|
2011-10-11 11:26:03 +00:00
|
|
|
trigger_error(__d(
|
|
|
|
'cake_dev', 'Could not apply permission mask "%s" on cache file "%s"',
|
|
|
|
array($this->_File->getPathname(), $this->settings['mask'])), E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2010-04-27 02:12:16 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determine is cache directory is writable
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-04-26 07:02:32 +00:00
|
|
|
protected function _active() {
|
|
|
|
$dir = new SplFileInfo($this->settings['path']);
|
|
|
|
if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
|
2010-02-23 04:36:47 +00:00
|
|
|
$this->_init = false;
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', '%s is not writable', $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;
|
|
|
|
}
|
2012-03-03 22:34:13 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|