Skip to content
Permalink
Browse files

Update exceptions used in cache engines.

  • Loading branch information...
ADmad committed Aug 31, 2014
1 parent f8b941b commit 6d8678702515b2f432b88b32c90a75438483d135
Showing with 82 additions and 40 deletions.
  1. +34 −14 src/Cache/Engine/FileEngine.php
  2. +48 −26 src/Cache/Engine/MemcachedEngine.php
@@ -16,8 +16,8 @@
use Cake\Cache\CacheEngine;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Utility\Inflector;
use Exception;
/**
* File Storage engine for cache. Filestorage is the slowest cache storage
@@ -147,7 +147,9 @@ public function write($key, $data) {
}
$this->_File->rewind();
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
$success = $this->_File->ftruncate(0) &&
$this->_File->fwrite($contents) &&
$this->_File->fflush();
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
@@ -161,7 +163,8 @@ public function write($key, $data) {
* 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
* @return mixed The cached data, or false if the data doesn't exist, has
* expired, or if there was an error fetching it
*/
public function read($key) {
$key = $this->_key($key);
@@ -178,7 +181,9 @@ public function read($key) {
$time = time();
$cachetime = intval($this->_File->current());
if ($cachetime !== false && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
if ($cachetime !== false &&
($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)
) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
@@ -211,7 +216,8 @@ public function read($key) {
* Delete a key from the cache
*
* @param string $key Identifier for the data
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
* @return bool True if the value was successfully deleted, false if it didn't
* exist or couldn't be removed
*/
public function delete($key) {
$key = $this->_key($key);
@@ -249,7 +255,10 @@ public function clear($check) {
$this->_clearDirectory($this->_config['path'], $now, $threshold);
$directory = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
$contents = new \RecursiveIteratorIterator(
$directory,
\RecursiveIteratorIterator::SELF_FIRST
);
$cleared = [];
foreach ($contents as $path) {
if ($path->isFile()) {
@@ -288,7 +297,7 @@ protected function _clearDirectory($path, $now, $threshold) {
try {
$file = new \SplFileObject($path . $entry, 'r');
} catch (\Exception $e) {
} catch (Exception $e) {
continue;
}
@@ -321,7 +330,7 @@ protected function _clearDirectory($path, $now, $threshold) {
* @param string $key The key to decrement
* @param int $offset The number to offset
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \Exception
*/
public function decrement($key, $offset = 1) {
throw new Exception('Files cannot be atomically decremented.');
@@ -333,7 +342,7 @@ public function decrement($key, $offset = 1) {
* @param string $key The key to decrement
* @param int $offset The number to offset
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \Exception
*/
public function increment($key, $offset = 1) {
throw new Exception('Files cannot be atomically incremented.');
@@ -366,13 +375,15 @@ protected function _setKey($key, $createKey = false) {
$exists = file_exists($path->getPathname());
try {
$this->_File = $path->openFile('c+');
} catch (\Exception $e) {
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
return false;
}
unset($path);
if (!$exists && !chmod($this->_File->getPathname(), (int)$this->_config['mask'])) {
if (!$exists &&
!chmod($this->_File->getPathname(), (int)$this->_config['mask'])
) {
trigger_error(sprintf(
'Could not apply permission mask "%s" on cache file "%s"',
$this->_File->getPathname(), $this->_config['mask']
@@ -397,7 +408,9 @@ protected function _active() {
}
if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
$this->_init = false;
trigger_error(sprintf('%s is not writable', $this->_config['path']), E_USER_WARNING);
trigger_error(sprintf(
'%s is not writable', $this->_config['path']
), E_USER_WARNING);
return false;
}
return true;
@@ -414,7 +427,11 @@ public function key($key) {
return false;
}
$key = Inflector::underscore(str_replace([DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'], '_', strval($key)));
$key = Inflector::underscore(str_replace(
[DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'],
'_',
strval($key)
));
return $key;
}
@@ -427,7 +444,10 @@ public function key($key) {
public function clearGroup($group) {
$this->_File = null;
$directoryIterator = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::CHILD_FIRST);
$contents = new \RecursiveIteratorIterator(
$directoryIterator,
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($contents as $object) {
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
$hasPrefix = true;
@@ -15,9 +15,9 @@
namespace Cake\Cache\Engine;
use Cake\Cache\CacheEngine;
use Cake\Core\Exception\Exception;
use Cake\Utility\Inflector;
use \Memcached;
use InvalidArgumentException;
use Memcached;
/**
* Memcached storage engine for cache. Memcached has some limitations in the amount of
@@ -94,7 +94,8 @@ class MemcachedEngine extends CacheEngine {
*
* @param array $config array of setting for the engine
* @return bool True if the engine has been successfully initialized, false if not
* @throws \Cake\Core\Exception\Exception when you try use authentication without Memcached compiled with SASL support
* @throws InvalidArgumentException When you try use authentication without
* Memcached compiled with SASL support
*/
public function init(array $config = []) {
if (!class_exists('Memcached')) {
@@ -128,7 +129,8 @@ public function init(array $config = []) {
return true;
}
$this->_Memcached = new Memcached($this->_config['persistent'] ? (string)$this->_config['persistent'] : null);
$this->_Memcached = new Memcached($this->_config['persistent'] ?
(string)$this->_config['persistent'] : null);
$this->_setOptions();
if (count($this->_Memcached->getServerList())) {
@@ -152,11 +154,14 @@ public function init(array $config = []) {
if ($this->_config['login'] !== null && $this->_config['password'] !== null) {
if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
throw new Exception(
throw new InvalidArgumentException(
'Memcached extension is not build with SASL support'
);
}
$this->_Memcached->setSaslAuthData($this->_config['login'], $this->_config['password']);
$this->_Memcached->setSaslAuthData(
$this->_config['login'],
$this->_config['password']
);
}
return true;
@@ -166,32 +171,46 @@ public function init(array $config = []) {
* Settings the memcached instance
*
* @return void
* @throws \Cake\Core\Exception\Exception when the Memcached extension is not built with the desired serializer engine
* @throws InvalidArgumentException When the Memcached extension is not built
* with the desired serializer engine.
*/
protected function _setOptions() {
$this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$serializer = strtolower($this->_config['serialize']);
if (!isset($this->_serializers[$serializer])) {
throw new Exception(
throw new InvalidArgumentException(
sprintf('%s is not a valid serializer engine for Memcached', $serializer)
);
}
if ($serializer !== 'php' && !constant('Memcached::HAVE_' . strtoupper($serializer))) {
throw new Exception(
if ($serializer !== 'php' &&
!constant('Memcached::HAVE_' . strtoupper($serializer))
) {
throw new InvalidArgumentException(
sprintf('Memcached extension is not compiled with %s support', $serializer)
);
}
$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]);
$this->_Memcached->setOption(
Memcached::OPT_SERIALIZER,
$this->_serializers[$serializer]
);
// Check for Amazon ElastiCache instance
if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) {
$this->_Memcached->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
if (defined('Memcached::OPT_CLIENT_MODE') &&
defined('Memcached::DYNAMIC_CLIENT_MODE')
) {
$this->_Memcached->setOption(
Memcached::OPT_CLIENT_MODE,
Memcached::DYNAMIC_CLIENT_MODE
);
}
$this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool)$this->_config['compress']);
$this->_Memcached->setOption(
Memcached::OPT_COMPRESSION,
(bool)$this->_config['compress']
);
}
/**
@@ -224,8 +243,9 @@ protected function _parseServerString($server) {
/**
* Write data for key into cache. When using memcached as your cache engine
* remember that the Memcached pecl extension does not support cache expiry times greater
* than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
* remember that the Memcached pecl extension does not support cache expiry
* times greater than 30 days in the future. Any duration greater than 30 days
* will be treated as never expiring.
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached
@@ -247,7 +267,8 @@ public function write($key, $value) {
* Write many cache entries to the cache at once
*
* @param array $data An array of data to be stored in the cache
* @return array of bools for each key provided, true if the data was successfully cached, false on failure
* @return array of bools for each key provided, true if the data was
* successfully cached, false on failure
*/
public function writeMany($data) {
$cacheData = array();
@@ -268,7 +289,8 @@ public function writeMany($data) {
* 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
* @return mixed The cached data, or false if the data doesn't exist, has
* expired, or if there was an error fetching it.
*/
public function read($key) {
$key = $this->_key($key);
@@ -280,8 +302,8 @@ public function read($key) {
* Read many keys from the cache at once
*
* @param array $keys An array of identifiers for the data
* @return An array containing, for each of the given $keys, the cached data or false if cached data could not be
* retreived
* @return An array containing, for each of the given $keys, the cached data or
* false if cached data could not be retreived.
*/
public function readMany($keys) {
$cacheKeys = array();
@@ -292,7 +314,8 @@ public function readMany($keys) {
$values = $this->_Memcached->getMulti($cacheKeys);
$return = array();
foreach ($keys as &$key) {
$return[$key] = array_key_exists($this->_key($key), $values) ? $values[$this->_key($key)] : false;
$return[$key] = array_key_exists($this->_key($key), $values) ?
$values[$this->_key($key)] : false;
}
return $return;
}
@@ -303,7 +326,6 @@ public function readMany($keys) {
* @param string $key Identifier for the data
* @param int $offset How much to increment
* @return bool|int New incremented value, false otherwise
* @throws \Cake\Core\Exception\Exception when you try to increment with compress = true
*/
public function increment($key, $offset = 1) {
$key = $this->_key($key);
@@ -317,7 +339,6 @@ public function increment($key, $offset = 1) {
* @param string $key Identifier for the data
* @param int $offset How much to subtract
* @return bool|int New decremented value, false otherwise
* @throws \Cake\Core\Exception\Exception when you try to decrement with compress = true
*/
public function decrement($key, $offset = 1) {
$key = $this->_key($key);
@@ -329,7 +350,8 @@ public function decrement($key, $offset = 1) {
* Delete a key from the cache
*
* @param string $key Identifier for the data
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
* @return bool True if the value was successfully deleted, false if it didn't
* exist or couldn't be removed.
*/
public function delete($key) {
$key = $this->_key($key);
@@ -341,8 +363,8 @@ public function delete($key) {
* Delete many keys from the cache at once
*
* @param array $keys An array of identifiers for the data
* @return array of boolean values that are true if the key was successfully deleted, false if it didn't exist or
* couldn't be removed
* @return array of boolean values that are true if the key was successfully
* deleted, false if it didn't exist or couldn't be removed.
*/
public function deleteMany($keys) {
$cacheKeys = array();

0 comments on commit 6d86787

Please sign in to comment.
You can’t perform that action at this time.