2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-12-22 22:48:15 +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
|
2011-03-12 17:14:43 +00:00
|
|
|
* 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
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +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
|
2011-12-08 15:35:02 +00:00
|
|
|
* 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_
|
2013-10-24 13:57:56 +00:00
|
|
|
* - lock = enable file locking on write, default => true
|
2009-11-21 21:09:06 +00:00
|
|
|
* - 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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if the engine has been successfully initialized, false if not
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function init($settings = array()) {
|
2012-04-24 03:59:32 +00:00
|
|
|
$settings += array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'path' => CACHE,
|
|
|
|
'prefix' => 'cake_',
|
|
|
|
'lock' => true,
|
|
|
|
'serialize' => true,
|
|
|
|
'isWindows' => false,
|
|
|
|
'mask' => 0664
|
|
|
|
);
|
|
|
|
parent::init($settings);
|
2008-06-24 01:09:54 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2012-04-23 01:03:07 +00:00
|
|
|
if (!empty($this->_groupPrefix)) {
|
|
|
|
$this->_groupPrefix = str_replace('_', DS, $this->_groupPrefix);
|
2012-03-26 03:30:28 +00:00
|
|
|
}
|
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
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $expires [optional] An expires timestamp, invalidating all data before.
|
|
|
|
* @return bool True if garbage collection was successful, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-03-24 20:32:31 +00:00
|
|
|
public function gc($expires = null) {
|
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
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $duration How long to cache the data, in seconds
|
|
|
|
* @return bool True if the data was successfully cached, false on failure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2015-12-28 20:34:04 +00:00
|
|
|
public function write($key, $data, $duration) {
|
2015-12-28 03:25:23 +00:00
|
|
|
if (!$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;
|
2016-04-30 17:11:34 +00:00
|
|
|
$contents = implode([$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();
|
2014-09-10 14:29:23 +00:00
|
|
|
$cachetime = (int)$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
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
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;
|
2013-09-06 16:19:23 +00:00
|
|
|
|
|
|
|
//@codingStandardsIgnoreStart
|
2013-09-06 13:46:01 +00:00
|
|
|
return @unlink($path);
|
2013-09-06 16:19:23 +00:00
|
|
|
//@codingStandardsIgnoreEnd
|
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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $check Optional - only delete expired cache items
|
|
|
|
* @return bool 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;
|
|
|
|
}
|
2013-08-09 12:33:21 +00:00
|
|
|
$this->_File = null;
|
|
|
|
|
2013-07-28 02:20:40 +00:00
|
|
|
$threshold = $now = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($check) {
|
|
|
|
$now = time();
|
|
|
|
$threshold = $now - $this->settings['duration'];
|
|
|
|
}
|
2013-08-02 21:56:11 +00:00
|
|
|
|
2013-07-28 02:20:40 +00:00
|
|
|
$this->_clearDirectory($this->settings['path'], $now, $threshold);
|
2013-08-02 21:56:11 +00:00
|
|
|
|
|
|
|
$directory = new RecursiveDirectoryIterator($this->settings['path']);
|
|
|
|
$contents = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
|
2013-08-09 12:33:21 +00:00
|
|
|
$cleared = array();
|
2013-08-02 21:56:11 +00:00
|
|
|
foreach ($contents as $path) {
|
|
|
|
if ($path->isFile()) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-09 12:33:21 +00:00
|
|
|
|
|
|
|
$path = $path->getRealPath() . DS;
|
|
|
|
if (!in_array($path, $cleared)) {
|
|
|
|
$this->_clearDirectory($path, $now, $threshold);
|
|
|
|
$cleared[] = $path;
|
|
|
|
}
|
2013-07-28 02:20:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to clear a directory of matching files.
|
|
|
|
*
|
|
|
|
* @param string $path The path to search.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $now The current timestamp
|
|
|
|
* @param int $threshold Any file not modified after this value will be deleted.
|
2013-07-28 02:20:40 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _clearDirectory($path, $now, $threshold) {
|
2010-05-27 04:01:17 +00:00
|
|
|
$prefixLength = strlen($this->settings['prefix']);
|
2013-08-02 21:56:11 +00:00
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir = dir($path);
|
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;
|
|
|
|
}
|
2014-08-20 21:35:32 +00:00
|
|
|
|
2014-08-19 08:32:25 +00:00
|
|
|
try {
|
|
|
|
$file = new SplFileObject($path . $entry, 'r');
|
2014-08-20 21:35:32 +00:00
|
|
|
} catch (Exception $e) {
|
2014-08-19 08:32:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-02 21:56:11 +00:00
|
|
|
|
2013-07-28 02:20:40 +00:00
|
|
|
if ($threshold) {
|
2013-08-02 21:56:11 +00:00
|
|
|
$mtime = $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;
|
|
|
|
}
|
2013-08-02 21:56:11 +00:00
|
|
|
$expires = (int)$file->current();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($expires > $now) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 21:56:11 +00:00
|
|
|
if ($file->isFile()) {
|
2013-09-06 16:19:23 +00:00
|
|
|
$filePath = $file->getRealPath();
|
2013-08-09 12:33:21 +00:00
|
|
|
$file = null;
|
2013-09-06 16:19:23 +00:00
|
|
|
|
|
|
|
//@codingStandardsIgnoreStart
|
|
|
|
@unlink($filePath);
|
|
|
|
//@codingStandardsIgnoreEnd
|
2011-08-27 15:52:29 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-18 04:38:36 +00:00
|
|
|
/**
|
|
|
|
* Not implemented
|
|
|
|
*
|
2014-06-01 02:02:55 +00:00
|
|
|
* @param string $key The key to decrement
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $offset The number to 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
|
|
|
|
*
|
2014-06-01 02:02:55 +00:00
|
|
|
* @param string $key The key to decrement
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $offset The number to 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
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $createKey Whether the key should be created if it doesn't exists, or not
|
|
|
|
* @return bool 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) {
|
2012-03-26 03:30:28 +00:00
|
|
|
$groups = null;
|
2012-04-23 01:03:07 +00:00
|
|
|
if (!empty($this->_groupPrefix)) {
|
|
|
|
$groups = vsprintf($this->_groupPrefix, $this->groups());
|
2012-03-26 03:30:28 +00:00
|
|
|
}
|
|
|
|
$dir = $this->settings['path'] . $groups;
|
|
|
|
|
|
|
|
if (!is_dir($dir)) {
|
2013-08-07 11:24:45 +00:00
|
|
|
mkdir($dir, 0775, true);
|
2012-03-26 03:30:28 +00:00
|
|
|
}
|
|
|
|
$path = new SplFileInfo($dir . $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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-26 07:02:32 +00:00
|
|
|
protected function _active() {
|
|
|
|
$dir = new SplFileInfo($this->settings['path']);
|
2013-08-07 11:24:45 +00:00
|
|
|
if (Configure::read('debug')) {
|
|
|
|
$path = $dir->getPathname();
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
mkdir($path, 0775, true);
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 07:02:32 +00:00
|
|
|
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
|
|
|
|
2012-03-26 03:30:28 +00:00
|
|
|
/**
|
|
|
|
* Generates a safe key for use with cache engine storage engines.
|
|
|
|
*
|
|
|
|
* @param string $key the key passed over
|
|
|
|
* @return mixed string $key or false
|
|
|
|
*/
|
|
|
|
public function key($key) {
|
|
|
|
if (empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-24 21:54:37 +00:00
|
|
|
$key = Inflector::underscore(str_replace(array(DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'), '_', strval($key)));
|
2012-03-26 03:30:28 +00:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2012-03-26 04:03:37 +00:00
|
|
|
/**
|
|
|
|
* Recursively deletes all files under any directory named as $group
|
|
|
|
*
|
2014-06-01 02:02:55 +00:00
|
|
|
* @param string $group The group to clear.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool success
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-03-26 03:30:28 +00:00
|
|
|
public function clearGroup($group) {
|
2013-08-09 12:33:21 +00:00
|
|
|
$this->_File = null;
|
2012-03-26 04:03:37 +00:00
|
|
|
$directoryIterator = new RecursiveDirectoryIterator($this->settings['path']);
|
|
|
|
$contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
|
|
|
|
foreach ($contents as $object) {
|
|
|
|
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
|
2013-09-10 02:24:18 +00:00
|
|
|
$hasPrefix = true;
|
|
|
|
if (strlen($this->settings['prefix']) !== 0) {
|
|
|
|
$hasPrefix = strpos($object->getBaseName(), $this->settings['prefix']) === 0;
|
|
|
|
}
|
2013-06-09 02:41:46 +00:00
|
|
|
if ($object->isFile() && $containsGroup && $hasPrefix) {
|
2013-09-06 16:19:23 +00:00
|
|
|
$path = $object->getPathName();
|
|
|
|
$object = null;
|
|
|
|
//@codingStandardsIgnoreStart
|
|
|
|
@unlink($path);
|
|
|
|
//@codingStandardsIgnoreEnd
|
2012-03-26 04:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-03-26 03:30:28 +00:00
|
|
|
}
|
2015-07-27 21:42:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data for key into cache if it doesn't exist already.
|
|
|
|
* If it already exists, it fails and returns false.
|
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data.
|
|
|
|
* @param mixed $value Data to be cached.
|
|
|
|
* @param int $duration How long to cache the data, in seconds.
|
|
|
|
* @return bool True if the data was successfully cached, false on failure.
|
|
|
|
*/
|
|
|
|
public function add($key, $value, $duration) {
|
|
|
|
$cachedValue = $this->read($key);
|
|
|
|
if ($cachedValue === false) {
|
|
|
|
return $this->write($key, $value, $duration);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|