Skip to content

Commit

Permalink
Consolidate Cache classes to use _config instead of settings.
Browse files Browse the repository at this point in the history
This also makes the properties protected instead of public, which feels
like a net win to me.

Refs #2298
  • Loading branch information
markstory committed Nov 15, 2013
1 parent b3d93ad commit dda40ff
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 116 deletions.
28 changes: 14 additions & 14 deletions Cake/Cache/CacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class CacheEngine {
*
* @var array
*/
public $settings = [];
protected $_config = [];

/**
* Contains the compiled string with all groups
Expand All @@ -42,23 +42,23 @@ abstract class CacheEngine {
*
* Called automatically by the cache frontend
*
* @param array $settings Associative array of parameters for the engine
* @param array $config Associative array of parameters for the engine
* @return boolean True if the engine has been successfully initialized, false if not
*/
public function init($settings = []) {
$settings += $this->settings + [
public function init($config = []) {
$config += $this->_config + [
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'groups' => []
];
$this->settings = $settings;
if (!empty($this->settings['groups'])) {
sort($this->settings['groups']);
$this->_groupPrefix = str_repeat('%s_', count($this->settings['groups']));
$this->_config = $config;
if (!empty($this->_config['groups'])) {
sort($this->_config['groups']);
$this->_groupPrefix = str_repeat('%s_', count($this->_config['groups']));
}
if (!is_numeric($this->settings['duration'])) {
$this->settings['duration'] = strtotime($this->settings['duration']) - time();
if (!is_numeric($this->_config['duration'])) {
$this->_config['duration'] = strtotime($this->_config['duration']) - time();
}
return true;
}
Expand Down Expand Up @@ -146,16 +146,16 @@ public function clearGroup($group) {
* @return array
*/
public function groups() {
return $this->settings['groups'];
return $this->_config['groups'];
}

/**
* Cache Engine settings
* Cache Engine config
*
* @return array settings
* @return array config
*/
public function settings() {
return $this->settings;
return $this->_config;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Cake/Cache/CacheRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ protected function _create($class, $alias, $settings) {
);
}

if ($instance->settings['probability'] && time() % $instance->settings['probability'] === 0) {
$settings = $instance->settings();
if ($settings['probability'] && time() % $settings['probability'] === 0) {
$instance->gc();
}
return $instance;
Expand Down
65 changes: 32 additions & 33 deletions Cake/Cache/Engine/FileEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ class FileEngine extends CacheEngine {
* - serialize = serialize the data, default => true
*
* @var array
* @see CacheEngine::__defaults
*/
public $settings = [];
protected $_config = [];

/**
* True unless FileEngine::__active(); fails
Expand All @@ -69,25 +68,25 @@ class FileEngine extends CacheEngine {
*
* Called automatically by the cache frontend.
*
* @param array $settings array of setting for the engine
* @param array $config array of setting for the engine
* @return boolean True if the engine has been successfully initialized, false if not
*/
public function init($settings = []) {
$settings += [
public function init($config = []) {
$config += [
'path' => CACHE,
'prefix' => 'cake_',
'lock' => true,
'serialize' => true,
'isWindows' => false,
'mask' => 0664
];
parent::init($settings);
parent::init($config);

if (DS === '\\') {
$this->settings['isWindows'] = true;
$this->_config['isWindows'] = true;
}
if (substr($this->settings['path'], -1) !== DS) {
$this->settings['path'] .= DS;
if (substr($this->_config['path'], -1) !== DS) {
$this->_config['path'] .= DS;
}
if (!empty($this->_groupPrefix)) {
$this->_groupPrefix = str_replace('_', DS, $this->_groupPrefix);
Expand Down Expand Up @@ -124,12 +123,12 @@ public function write($key, $data, $duration) {

$lineBreak = "\n";

if ($this->settings['isWindows']) {
if ($this->_config['isWindows']) {
$lineBreak = "\r\n";
}

if (!empty($this->settings['serialize'])) {
if ($this->settings['isWindows']) {
if (!empty($this->_config['serialize'])) {
if ($this->_config['isWindows']) {
$data = str_replace('\\', '\\\\\\\\', serialize($data));
} else {
$data = serialize($data);
Expand All @@ -139,14 +138,14 @@ public function write($key, $data, $duration) {
$expires = time() + $duration;
$contents = $expires . $lineBreak . $data . $lineBreak;

if ($this->settings['lock']) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_EX);
}

$this->_File->rewind();
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();

if ($this->settings['lock']) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}

Expand All @@ -164,16 +163,16 @@ public function read($key) {
return false;
}

if ($this->settings['lock']) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_SH);
}

$this->_File->rewind();
$time = time();
$cachetime = intval($this->_File->current());

if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
if ($this->settings['lock']) {
if ($cachetime !== false && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
return false;
Expand All @@ -186,14 +185,14 @@ public function read($key) {
$this->_File->next();
}

if ($this->settings['lock']) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}

$data = trim($data);

if ($data !== '' && !empty($this->settings['serialize'])) {
if ($this->settings['isWindows']) {
if ($data !== '' && !empty($this->_config['serialize'])) {
if ($this->_config['isWindows']) {
$data = str_replace('\\\\\\\\', '\\', $data);
}
$data = unserialize((string)$data);
Expand Down Expand Up @@ -234,12 +233,12 @@ public function clear($check) {
$threshold = $now = false;
if ($check) {
$now = time();
$threshold = $now - $this->settings['duration'];
$threshold = $now - $this->_config['duration'];
}

$this->_clearDirectory($this->settings['path'], $now, $threshold);
$this->_clearDirectory($this->_config['path'], $now, $threshold);

$directory = new \RecursiveDirectoryIterator($this->settings['path']);
$directory = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
$cleared = [];
foreach ($contents as $path) {
Expand All @@ -265,15 +264,15 @@ public function clear($check) {
* @return void
*/
protected function _clearDirectory($path, $now, $threshold) {
$prefixLength = strlen($this->settings['prefix']);
$prefixLength = strlen($this->_config['prefix']);

if (!is_dir($path)) {
return;
}

$dir = dir($path);
while (($entry = $dir->read()) !== false) {
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
if (substr($entry, 0, $prefixLength) !== $this->_config['prefix']) {
continue;
}
$filePath = $path . $entry;
Expand Down Expand Up @@ -342,7 +341,7 @@ protected function _setKey($key, $createKey = false) {
if (!empty($this->_groupPrefix)) {
$groups = vsprintf($this->_groupPrefix, $this->groups());
}
$dir = $this->settings['path'] . $groups;
$dir = $this->_config['path'] . $groups;

if (!is_dir($dir)) {
mkdir($dir, 0775, true);
Expand All @@ -362,10 +361,10 @@ protected function _setKey($key, $createKey = false) {
}
unset($path);

if (!$exists && !chmod($this->_File->getPathname(), (int)$this->settings['mask'])) {
if (!$exists && !chmod($this->_File->getPathname(), (int)$this->_config['mask'])) {
trigger_error(__d(
'cake_dev', 'Could not apply permission mask "%s" on cache file "%s"',
[$this->_File->getPathname(), $this->settings['mask']]), E_USER_WARNING);
[$this->_File->getPathname(), $this->_config['mask']]), E_USER_WARNING);
}
}
return true;
Expand All @@ -377,7 +376,7 @@ protected function _setKey($key, $createKey = false) {
* @return boolean
*/
protected function _active() {
$dir = new \SplFileInfo($this->settings['path']);
$dir = new \SplFileInfo($this->_config['path']);
if (Configure::read('debug')) {
$path = $dir->getPathname();
if (!is_dir($path)) {
Expand All @@ -386,7 +385,7 @@ protected function _active() {
}
if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
$this->_init = false;
trigger_error(__d('cake_dev', '%s is not writable', $this->settings['path']), E_USER_WARNING);
trigger_error(__d('cake_dev', '%s is not writable', $this->_config['path']), E_USER_WARNING);
return false;
}
return true;
Expand Down Expand Up @@ -414,13 +413,13 @@ public function key($key) {
*/
public function clearGroup($group) {
$this->_File = null;
$directoryIterator = new \RecursiveDirectoryIterator($this->settings['path']);
$directoryIterator = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($contents as $object) {
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
$hasPrefix = true;
if (strlen($this->settings['prefix']) !== 0) {
$hasPrefix = strpos($object->getBaseName(), $this->settings['prefix']) === 0;
if (strlen($this->_config['prefix']) !== 0) {
$hasPrefix = strpos($object->getBaseName(), $this->_config['prefix']) === 0;
}
if ($object->isFile() && $containsGroup && $hasPrefix) {
$path = $object->getPathName();
Expand Down
Loading

0 comments on commit dda40ff

Please sign in to comment.