diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php index 2e3b9e37e..fbe09a08f 100644 --- a/src/Exception/BadMethodCallException.php +++ b/src/Exception/BadMethodCallException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface { diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 41a322275..b69d5d592 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Exception - */ interface ExceptionInterface { } diff --git a/src/Exception/ExtensionNotLoadedException.php b/src/Exception/ExtensionNotLoadedException.php index bf806c400..da04aaf62 100644 --- a/src/Exception/ExtensionNotLoadedException.php +++ b/src/Exception/ExtensionNotLoadedException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class ExtensionNotLoadedException extends RuntimeException { } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index bc5db03ad..88c6d2ab5 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface { diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php index b69ebbd6f..6851799fc 100644 --- a/src/Exception/LogicException.php +++ b/src/Exception/LogicException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class LogicException extends \LogicException implements ExceptionInterface { } diff --git a/src/Exception/MissingDependencyException.php b/src/Exception/MissingDependencyException.php index c16493689..0b829e470 100644 --- a/src/Exception/MissingDependencyException.php +++ b/src/Exception/MissingDependencyException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class MissingDependencyException extends RuntimeException { } diff --git a/src/Exception/MissingKeyException.php b/src/Exception/MissingKeyException.php index 08c408269..bee42a41f 100644 --- a/src/Exception/MissingKeyException.php +++ b/src/Exception/MissingKeyException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class MissingKeyException extends RuntimeException { } diff --git a/src/Exception/OutOfSpaceException.php b/src/Exception/OutOfSpaceException.php index 86ed66b61..2d410a7b9 100644 --- a/src/Exception/OutOfSpaceException.php +++ b/src/Exception/OutOfSpaceException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class OutOfSpaceException extends \OverflowException implements ExceptionInterface { } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index a7c70fa18..b05df4506 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class RuntimeException extends \RuntimeException implements ExceptionInterface { } diff --git a/src/Exception/UnexpectedValueException.php b/src/Exception/UnexpectedValueException.php index e446fa804..55f88051e 100644 --- a/src/Exception/UnexpectedValueException.php +++ b/src/Exception/UnexpectedValueException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class UnexpectedValueException extends \UnexpectedValueException implements ExceptionInterface { diff --git a/src/Exception/UnsupportedMethodCallException.php b/src/Exception/UnsupportedMethodCallException.php index 3e279eb41..976246cd4 100644 --- a/src/Exception/UnsupportedMethodCallException.php +++ b/src/Exception/UnsupportedMethodCallException.php @@ -5,15 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class UnsupportedMethodCallException extends \BadMethodCallException implements ExceptionInterface { diff --git a/src/Pattern/AbstractPattern.php b/src/Pattern/AbstractPattern.php index 89815b8b6..d987928a4 100644 --- a/src/Pattern/AbstractPattern.php +++ b/src/Pattern/AbstractPattern.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; @@ -13,11 +12,6 @@ use Traversable; use Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ abstract class AbstractPattern implements PatternInterface { /** diff --git a/src/Pattern/CallbackCache.php b/src/Pattern/CallbackCache.php index df78b01c8..013f757bc 100644 --- a/src/Pattern/CallbackCache.php +++ b/src/Pattern/CallbackCache.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; @@ -13,11 +12,6 @@ use Zend\Cache\Exception; use Zend\Stdlib\ErrorHandler; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ class CallbackCache extends AbstractPattern { /** diff --git a/src/Pattern/CaptureCache.php b/src/Pattern/CaptureCache.php index aa7a2fb9b..df18f1d35 100644 --- a/src/Pattern/CaptureCache.php +++ b/src/Pattern/CaptureCache.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; @@ -13,11 +12,6 @@ use Zend\Cache\Exception; use Zend\Stdlib\ErrorHandler; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ class CaptureCache extends AbstractPattern { /** diff --git a/src/Pattern/ClassCache.php b/src/Pattern/ClassCache.php index b38023b8a..32561e723 100644 --- a/src/Pattern/ClassCache.php +++ b/src/Pattern/ClassCache.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; @@ -13,10 +12,6 @@ use Zend\Cache; use Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - */ class ClassCache extends CallbackCache { /** diff --git a/src/Pattern/ObjectCache.php b/src/Pattern/ObjectCache.php index 03a604cf7..ca5f0570d 100644 --- a/src/Pattern/ObjectCache.php +++ b/src/Pattern/ObjectCache.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; use Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ class ObjectCache extends CallbackCache { /** diff --git a/src/Pattern/OutputCache.php b/src/Pattern/OutputCache.php index 6135cd39e..826839b46 100644 --- a/src/Pattern/OutputCache.php +++ b/src/Pattern/OutputCache.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; use Zend\Cache\Exception; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ class OutputCache extends AbstractPattern { /** diff --git a/src/Pattern/PatternInterface.php b/src/Pattern/PatternInterface.php index 19f466d27..ebaf29a90 100644 --- a/src/Pattern/PatternInterface.php +++ b/src/Pattern/PatternInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ interface PatternInterface { /** diff --git a/src/Pattern/PatternOptions.php b/src/Pattern/PatternOptions.php index 803790019..656cf2146 100644 --- a/src/Pattern/PatternOptions.php +++ b/src/Pattern/PatternOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Pattern; @@ -16,11 +15,6 @@ use Zend\Cache\Storage\StorageInterface as Storage; use Zend\Stdlib\AbstractOptions; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Pattern - */ class PatternOptions extends AbstractOptions { /** diff --git a/src/PatternFactory.php b/src/PatternFactory.php index c8d4c78b3..8422edb76 100644 --- a/src/PatternFactory.php +++ b/src/PatternFactory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache; @@ -13,11 +12,7 @@ use Traversable; use Zend\Stdlib\ArrayUtils; -/** - * @category Zend - * @package Zend_Cache - */ -class PatternFactory +abstract class PatternFactory { /** * The pattern manager diff --git a/src/PatternPluginManager.php b/src/PatternPluginManager.php index 4950a163a..d8a33db09 100644 --- a/src/PatternPluginManager.php +++ b/src/PatternPluginManager.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache; @@ -18,9 +17,6 @@ * Enforces that adatpers retrieved are instances of * Pattern\PatternInterface. Additionally, it registers a number of default * patterns available. - * - * @category Zend - * @package Zend_Cache */ class PatternPluginManager extends AbstractPluginManager { diff --git a/src/Service/StorageCacheFactory.php b/src/Service/StorageCacheFactory.php new file mode 100644 index 000000000..0ce181b6d --- /dev/null +++ b/src/Service/StorageCacheFactory.php @@ -0,0 +1,30 @@ +get('Config'); + $cacheConfig = isset($config['cache']) ? $config['cache'] : array(); + $cache = StorageFactory::factory($cacheConfig); + + return $cache; + } +} diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index f6a0f6b71..0892d9eb0 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -25,11 +24,6 @@ use Zend\EventManager\EventManagerInterface; use Zend\EventManager\EventsCapableInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ abstract class AbstractAdapter implements StorageInterface, EventsCapableInterface { /** diff --git a/src/Storage/Adapter/AbstractZendServer.php b/src/Storage/Adapter/AbstractZendServer.php index 6e5725475..0071edf8c 100644 --- a/src/Storage/Adapter/AbstractZendServer.php +++ b/src/Storage/Adapter/AbstractZendServer.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -14,11 +13,6 @@ use Zend\Cache\Exception; use Zend\Cache\Storage\Capabilities; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ abstract class AbstractZendServer extends AbstractAdapter { /** @@ -41,10 +35,10 @@ abstract class AbstractZendServer extends AbstractAdapter */ protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { - $prefix = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR; - $internalKey = $prefix . $normalizedKey; + $namespace = $this->getOptions()->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; - $result = $this->zdcFetch($internalKey); + $result = $this->zdcFetch($prefix . $normalizedKey); if ($result === false) { $success = false; $result = null; @@ -65,16 +59,20 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo */ protected function internalGetItems(array & $normalizedKeys) { - $prefix = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR; - $prefixL = strlen($prefix); + $namespace = $this->getOptions()->getNamespace(); + if ($namespace === '') { + return $this->zdcFetchMulti($normalizedKeys); + } + $prefix = $namespace . self::NAMESPACE_SEPARATOR; $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } - $fetch = $this->zdcFetchMulti($internalKeys); - $result = array(); + $fetch = $this->zdcFetchMulti($internalKeys); + $result = array(); + $prefixL = strlen($prefix); foreach ($fetch as $k => & $v) { $result[ substr($k, $prefixL) ] = $v; } @@ -91,8 +89,8 @@ protected function internalGetItems(array & $normalizedKeys) */ protected function internalHasItem(& $normalizedKey) { - - $prefix = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR; + $namespace = $this->getOptions()->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; return ($this->zdcFetch($prefix . $normalizedKey) !== false); } @@ -105,16 +103,20 @@ protected function internalHasItem(& $normalizedKey) */ protected function internalHasItems(array & $normalizedKeys) { - $prefix = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR; - $prefixL = strlen($prefix); + $namespace = $this->getOptions()->getNamespace(); + if ($namespace === '') { + return array_keys($this->zdcFetchMulti($normalizedKeys)); + } + $prefix = $namespace . self::NAMESPACE_SEPARATOR; $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } - $fetch = $this->zdcFetchMulti($internalKeys); - $result = array(); + $fetch = $this->zdcFetchMulti($internalKeys); + $result = array(); + $prefixL = strlen($prefix); foreach ($fetch as $internalKey => & $value) { $result[] = substr($internalKey, $prefixL); } @@ -134,16 +136,21 @@ protected function internalHasItems(array & $normalizedKeys) */ protected function internalGetMetadatas(array & $normalizedKeys) { - $prefix = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR; - $prefixL = strlen($prefix); + $namespace = $this->getOptions()->getNamespace(); + if ($namespace === '') { + $result = $this->zdcFetchMulti($normalizedKeys); + return array_fill_keys(array_keys($result), array()); + } + $prefix = $namespace . self::NAMESPACE_SEPARATOR; $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } - $fetch = $this->zdcFetchMulti($internalKeys); - $result = array(); + $fetch = $this->zdcFetchMulti($internalKeys); + $result = array(); + $prefixL = strlen($prefix); foreach ($fetch as $internalKey => $value) { $result[ substr($internalKey, $prefixL) ] = array(); } @@ -163,9 +170,10 @@ protected function internalGetMetadatas(array & $normalizedKeys) */ protected function internalSetItem(& $normalizedKey, & $value) { - $options = $this->getOptions(); - $internalKey = $options->getNamespace() . self::NAMESPACE_SEPARATOR . $normalizedKey; - $this->zdcStore($internalKey, $value, $options->getTtl()); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; + $this->zdcStore($prefix . $normalizedKey, $value, $options->getTtl()); return true; } @@ -178,8 +186,9 @@ protected function internalSetItem(& $normalizedKey, & $value) */ protected function internalRemoveItem(& $normalizedKey) { - $internalKey = $this->getOptions()->getNamespace() . self::NAMESPACE_SEPARATOR . $normalizedKey; - return $this->zdcDelete($internalKey); + $namespace = $this->getOptions()->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; + return $this->zdcDelete($prefix . $normalizedKey); } /* status */ diff --git a/src/Storage/Adapter/AdapterOptions.php b/src/Storage/Adapter/AdapterOptions.php index 910045aa8..2d1b1bc30 100644 --- a/src/Storage/Adapter/AdapterOptions.php +++ b/src/Storage/Adapter/AdapterOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -20,10 +19,6 @@ /** * Unless otherwise marked, all options in this class affect all adapters. - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class AdapterOptions extends AbstractOptions { diff --git a/src/Storage/Adapter/Apc.php b/src/Storage/Adapter/Apc.php index 7cc592345..79abe1352 100644 --- a/src/Storage/Adapter/Apc.php +++ b/src/Storage/Adapter/Apc.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -22,11 +21,6 @@ use Zend\Cache\Storage\IterableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; -/** - * @package Zend_Cache - * @subpackage Zend_Cache_Storage - * @subpackage Storage - */ class Apc extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByNamespaceInterface, @@ -139,9 +133,14 @@ public function getAvailableSpace() */ public function getIterator() { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $pattern = '/^' . preg_quote($prefix, '/') . '/'; + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ''; + $pattern = null; + if ($namespace !== '') { + $prefix = $namespace . $options->getNamespaceSeparator(); + $pattern = '/^' . preg_quote($prefix, '/') . '/'; + } $baseIt = new BaseApcIterator('user', $pattern, 0, 1, \APC_LIST_ACTIVE); return new ApcIterator($this, $baseIt, $prefix); @@ -169,9 +168,14 @@ public function flush() */ public function clearByNamespace($namespace) { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + $options = $this->getOptions(); $prefix = $namespace . $options->getNamespaceSeparator(); - $pattern = '/^' . preg_quote($prefix, '/') . '+/'; + $pattern = '/^' . preg_quote($prefix, '/') . '/'; return apc_delete(new BaseApcIterator('user', $pattern, 0, 1, \APC_LIST_ACTIVE)); } @@ -185,9 +189,15 @@ public function clearByNamespace($namespace) */ public function clearByPrefix($prefix) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator() . $prefix; - $pattern = '/^' . preg_quote($prefix, '/') . '+/'; + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $nsPrefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $pattern = '/^' . preg_quote($nsPrefix . $prefix, '/') . '/'; return apc_delete(new BaseApcIterator('user', $pattern, 0, 1, \APC_LIST_ACTIVE)); } @@ -205,7 +215,8 @@ public function clearByPrefix($prefix) protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $result = apc_fetch($internalKey, $success); @@ -226,9 +237,13 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo */ protected function internalGetItems(array & $normalizedKeys) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return apc_fetch($normalizedKeys); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; @@ -255,8 +270,9 @@ protected function internalGetItems(array & $normalizedKeys) */ protected function internalHasItem(& $normalizedKey) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); return apc_exists($prefix . $normalizedKey); } @@ -269,9 +285,14 @@ protected function internalHasItem(& $normalizedKey) */ protected function internalHasItems(array & $normalizedKeys) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + // array_filter with no callback will remove entries equal to FALSE + return array_keys(array_filter(apc_exists($normalizedKeys))); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; @@ -299,7 +320,8 @@ protected function internalHasItems(array & $normalizedKeys) protected function internalGetMetadata(& $normalizedKey) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; // @see http://pecl.php.net/bugs/bug.php?id=22564 @@ -337,12 +359,16 @@ protected function internalGetMetadatas(array & $normalizedKeys) $keysRegExp[] = preg_quote($normalizedKey, '/'); } - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $regexp = '/^' . preg_quote($prefix, '/') . '(' . implode('|', $keysRegExp) . ')' . '$/'; + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + $pattern = '/^(' . implode('|', $keysRegExp) . ')' . '$/'; + } else { + $prefix = $namespace . $options->getNamespaceSeparator(); + $pattern = '/^' . preg_quote($prefix, '/') . '(' . implode('|', $keysRegExp) . ')' . '$/'; + } $format = \APC_ITER_ALL ^ \APC_ITER_VALUE ^ \APC_ITER_TYPE ^ \APC_ITER_REFCOUNT; - - $it = new BaseApcIterator('user', $regexp, $format, 100, \APC_LIST_ACTIVE); + $it = new BaseApcIterator('user', $pattern, $format, 100, \APC_LIST_ACTIVE); $result = array(); $prefixL = strlen($prefix); foreach ($it as $internalKey => $metadata) { @@ -371,7 +397,8 @@ protected function internalGetMetadatas(array & $normalizedKeys) protected function internalSetItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); @@ -394,9 +421,13 @@ protected function internalSetItem(& $normalizedKey, & $value) */ protected function internalSetItems(array & $normalizedKeyValuePairs) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return array_keys(apc_store($normalizedKeyValuePairs, null, $options->getTtl())); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); foreach ($normalizedKeyValuePairs as $normalizedKey => &$value) { $internalKey = $prefix . $normalizedKey; @@ -426,7 +457,8 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) protected function internalAddItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); @@ -453,9 +485,13 @@ protected function internalAddItem(& $normalizedKey, & $value) */ protected function internalAddItems(array & $normalizedKeyValuePairs) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return array_keys(apc_add($normalizedKeyValuePairs, null, $options->getTtl())); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { $internalKey = $prefix . $normalizedKey; @@ -485,14 +521,15 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) protected function internalReplaceItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; - $ttl = $options->getTtl(); if (!apc_exists($internalKey)) { return false; } + $ttl = $options->getTtl(); if (!apc_store($internalKey, $value, $ttl)) { $type = is_object($value) ? get_class($value) : gettype($value); throw new Exception\RuntimeException( @@ -512,10 +549,10 @@ protected function internalReplaceItem(& $normalizedKey, & $value) */ protected function internalRemoveItem(& $normalizedKey) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $internalKey = $prefix . $normalizedKey; - return apc_delete($internalKey); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + return apc_delete($prefix . $normalizedKey); } /** @@ -527,9 +564,13 @@ protected function internalRemoveItem(& $normalizedKey) */ protected function internalRemoveItems(array & $normalizedKeys) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return apc_delete($normalizedKeys); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; @@ -557,7 +598,8 @@ protected function internalRemoveItems(array & $normalizedKeys) protected function internalIncrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); $value = (int) $value; @@ -588,7 +630,8 @@ protected function internalIncrementItem(& $normalizedKey, & $value) protected function internalDecrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $value = (int) $value; $newValue = apc_dec($internalKey, $value); diff --git a/src/Storage/Adapter/ApcIterator.php b/src/Storage/Adapter/ApcIterator.php index d067b8934..66ac993ae 100644 --- a/src/Storage/Adapter/ApcIterator.php +++ b/src/Storage/Adapter/ApcIterator.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -13,11 +12,6 @@ use APCIterator as BaseApcIterator; use Zend\Cache\Storage\IteratorInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class ApcIterator implements IteratorInterface { diff --git a/src/Storage/Adapter/ApcOptions.php b/src/Storage/Adapter/ApcOptions.php index b614aa0bf..4d9d7595c 100644 --- a/src/Storage/Adapter/ApcOptions.php +++ b/src/Storage/Adapter/ApcOptions.php @@ -5,17 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; /** * These are options specific to the APC adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class ApcOptions extends AdapterOptions { diff --git a/src/Storage/Adapter/Dba.php b/src/Storage/Adapter/Dba.php index bb8dad193..2c8fab3fc 100644 --- a/src/Storage/Adapter/Dba.php +++ b/src/Storage/Adapter/Dba.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -216,6 +215,11 @@ public function flush() */ public function clearByNamespace($namespace) { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + $prefix = $namespace . $this->getOptions()->getNamespaceSeparator(); $prefixl = strlen($prefix); $result = true; @@ -247,10 +251,16 @@ public function clearByNamespace($namespace) */ public function clearByPrefix($prefix) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator() . $prefix; - $prefixl = strlen($prefix); - $result = true; + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator() . $prefix; + $prefixL = strlen($prefix); + $result = true; $this->_open(); @@ -258,7 +268,7 @@ public function clearByPrefix($prefix) $recheck = false; $internalKey = dba_firstkey($this->handle); while ($internalKey !== false && $internalKey !== null) { - if (substr($internalKey, 0, $prefixl) === $prefix) { + if (substr($internalKey, 0, $prefixL) === $prefix) { $result = dba_delete($internalKey, $this->handle) && $result; $recheck = true; } @@ -279,8 +289,9 @@ public function clearByPrefix($prefix) */ public function getIterator() { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); return new DbaIterator($this, $this->handle, $prefix); } @@ -315,12 +326,12 @@ public function optimize() */ protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $internalKey = $prefix . $normalizedKey; + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $this->_open(); - $value = dba_fetch($internalKey, $this->handle); + $value = dba_fetch($prefix . $normalizedKey, $this->handle); if ($value === false) { $success = false; @@ -341,12 +352,12 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo */ protected function internalHasItem(& $normalizedKey) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $internalKey = $prefix . $normalizedKey; + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $this->_open(); - return dba_exists($internalKey, $this->handle); + return dba_exists($prefix . $normalizedKey, $this->handle); } /* writing */ @@ -362,7 +373,8 @@ protected function internalHasItem(& $normalizedKey) protected function internalSetItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $this->_open(); @@ -384,7 +396,8 @@ protected function internalSetItem(& $normalizedKey, & $value) protected function internalAddItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $this->_open(); @@ -416,7 +429,8 @@ protected function internalAddItem(& $normalizedKey, & $value) protected function internalRemoveItem(& $normalizedKey) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $this->_open(); diff --git a/src/Storage/Adapter/DbaIterator.php b/src/Storage/Adapter/DbaIterator.php index 11846594f..f6b5ed07d 100644 --- a/src/Storage/Adapter/DbaIterator.php +++ b/src/Storage/Adapter/DbaIterator.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -13,11 +12,6 @@ use Zend\Cache\Exception; use Zend\Cache\Storage\IteratorInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class DbaIterator implements IteratorInterface { /** diff --git a/src/Storage/Adapter/DbaOptions.php b/src/Storage/Adapter/DbaOptions.php index 340e3681f..06ff1856e 100644 --- a/src/Storage/Adapter/DbaOptions.php +++ b/src/Storage/Adapter/DbaOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -14,10 +13,6 @@ /** * These are options specific to the APC adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class DbaOptions extends AdapterOptions { diff --git a/src/Storage/Adapter/Filesystem.php b/src/Storage/Adapter/Filesystem.php index 560ef8714..ba3e9df19 100644 --- a/src/Storage/Adapter/Filesystem.php +++ b/src/Storage/Adapter/Filesystem.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -27,11 +26,6 @@ use Zend\Cache\Storage\TotalSpaceCapableInterface; use Zend\Stdlib\ErrorHandler; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class Filesystem extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByNamespaceInterface, @@ -140,8 +134,9 @@ public function flush() */ public function clearExpired() { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $flags = GlobIterator::SKIP_DOTS | GlobIterator::CURRENT_AS_FILEINFO; $path = $options->getCacheDir() @@ -183,13 +178,18 @@ public function clearExpired() */ public function clearByNamespace($namespace) { - $options = $this->getOptions(); - $nsPrefix = $namespace . $options->getNamespaceSeparator(); + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + + $options = $this->getOptions(); + $prefix = $namespace . $options->getNamespaceSeparator(); $flags = GlobIterator::SKIP_DOTS | GlobIterator::CURRENT_AS_PATHNAME; $path = $options->getCacheDir() - . str_repeat(\DIRECTORY_SEPARATOR . $nsPrefix . '*', $options->getDirLevel()) - . \DIRECTORY_SEPARATOR . $nsPrefix . '*'; + . str_repeat(\DIRECTORY_SEPARATOR . $prefix . '*', $options->getDirLevel()) + . \DIRECTORY_SEPARATOR . $prefix . '*'; $glob = new GlobIterator($path, $flags); ErrorHandler::start(); @@ -198,7 +198,7 @@ public function clearByNamespace($namespace) } $error = ErrorHandler::stop(); if ($error) { - throw new Exception\RuntimeException("Failed to remove file '{$pathname}'", 0, $error); + throw new Exception\RuntimeException("Failed to remove files of '{$path}'", 0, $error); } return true; @@ -215,8 +215,14 @@ public function clearByNamespace($namespace) */ public function clearByPrefix($prefix) { - $options = $this->getOptions(); - $nsPrefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $nsPrefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $flags = GlobIterator::SKIP_DOTS | GlobIterator::CURRENT_AS_PATHNAME; $path = $options->getCacheDir() @@ -230,7 +236,7 @@ public function clearByPrefix($prefix) } $error = ErrorHandler::stop(); if ($error) { - throw new Exception\RuntimeException("Failed to remove file '{$pathname}'", 0, $error); + throw new Exception\RuntimeException("Failed to remove files of '{$path}'", 0, $error); } return true; @@ -302,9 +308,10 @@ public function clearByTags(array $tags, $disjunction = false) return true; } - $tagCount = count($tags); - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $tagCount = count($tags); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $flags = GlobIterator::SKIP_DOTS | GlobIterator::CURRENT_AS_PATHNAME; $path = $options->getCacheDir() @@ -344,9 +351,10 @@ public function clearByTags(array $tags, $disjunction = false) */ public function getIterator() { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $path = $options->getCacheDir() + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $path = $options->getCacheDir() . str_repeat(\DIRECTORY_SEPARATOR . $prefix . '*', $options->getDirLevel()) . \DIRECTORY_SEPARATOR . $prefix . '*.dat'; return new FilesystemIterator($this, $path, $prefix); @@ -362,13 +370,13 @@ public function getIterator() */ public function optimize() { - $baseOptions = $this->getOptions(); - if ($baseOptions->getDirLevel()) { + $options = $this->getOptions(); + if ($options->getDirLevel()) { + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + // removes only empty directories - $this->rmDir( - $baseOptions->getCacheDir(), - $baseOptions->getNamespace() . $baseOptions->getNamespaceSeparator() - ); + $this->rmDir($options->getCacheDir(), $prefix); } return true; } @@ -1243,10 +1251,11 @@ protected function rmDir($dir, $prefix) */ protected function getFileSpec($normalizedKey) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $path = $options->getCacheDir() . \DIRECTORY_SEPARATOR; - $level = $options->getDirLevel(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $path = $options->getCacheDir() . \DIRECTORY_SEPARATOR; + $level = $options->getDirLevel(); $fileSpecId = $path . $prefix . $normalizedKey . '/' . $level; if ($this->lastFileSpecId !== $fileSpecId) { diff --git a/src/Storage/Adapter/FilesystemIterator.php b/src/Storage/Adapter/FilesystemIterator.php index 2a87edfc4..ede9db20d 100644 --- a/src/Storage/Adapter/FilesystemIterator.php +++ b/src/Storage/Adapter/FilesystemIterator.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -13,11 +12,6 @@ use GlobIterator; use Zend\Cache\Storage\IteratorInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class FilesystemIterator implements IteratorInterface { diff --git a/src/Storage/Adapter/FilesystemOptions.php b/src/Storage/Adapter/FilesystemOptions.php index d22eefde0..1d64f2d6b 100644 --- a/src/Storage/Adapter/FilesystemOptions.php +++ b/src/Storage/Adapter/FilesystemOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -15,10 +14,6 @@ /** * These are options specific to the Filesystem adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class FilesystemOptions extends AdapterOptions { diff --git a/src/Storage/Adapter/KeyListIterator.php b/src/Storage/Adapter/KeyListIterator.php index b2069bb98..862995cf2 100644 --- a/src/Storage/Adapter/KeyListIterator.php +++ b/src/Storage/Adapter/KeyListIterator.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -14,11 +13,6 @@ use Zend\Cache\Storage\IteratorInterface; use Zend\Cache\Storage\StorageInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class KeyListIterator implements IteratorInterface, Countable { diff --git a/src/Storage/Adapter/Memcached.php b/src/Storage/Adapter/Memcached.php index f44626d19..a4892a98b 100644 --- a/src/Storage/Adapter/Memcached.php +++ b/src/Storage/Adapter/Memcached.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -19,11 +18,6 @@ use Zend\Cache\Storage\FlushableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; -/** - * @package Zend_Cache - * @subpackage Zend_Cache_Storage - * @subpackage Storage - */ class Memcached extends AbstractAdapter implements AvailableSpaceCapableInterface, FlushableInterface, @@ -37,11 +31,32 @@ class Memcached extends AbstractAdapter implements protected static $extMemcachedMajorVersion; /** - * The memcached resource + * Has this instance be initialized + * + * @var boolean + */ + protected $initialized = false; + + /** + * The memcached resource manager + * + * @var null|MemcachedResourceManager + */ + protected $resourceManager; + + /** + * The memcached resource id + * + * @var null|string + */ + protected $resourceId; + + /** + * The namespace prefix * - * @var MemcachedResource + * @var string */ - protected $memcachedResource; + protected $namespacePrefix = ''; /** * Constructor @@ -61,6 +76,12 @@ public function __construct($options = null) } parent::__construct($options); + + // reset initialized flag on update option(s) + $initialized = & $this->initialized; + $this->getEventManager()->attach('option', function ($event) use (& $initialized) { + $initialized = false; + }); } /** @@ -70,45 +91,26 @@ public function __construct($options = null) */ protected function getMemcachedResource() { - if ($this->memcachedResource) { - return $this->memcachedResource; - } - - $options = $this->getOptions(); + if (!$this->initialized) { + $options = $this->getOptions(); - // use a configured resource or a new one - $memcached = $options->getMemcachedResource() ?: new MemcachedResource(); - - // init lib options - if (static::$extMemcachedMajorVersion > 1) { - $memcached->setOptions($options->getLibOptions()); - } else { - foreach ($options->getLibOptions() as $k => $v) { - $memcached->setOption($k, $v); - } - } - $memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $options->getNamespace()); + // get resource manager and resource id + $this->resourceManager = $options->getResourceManager(); + $this->resourceId = $options->getResourceId(); - // Allow updating namespace - $this->getEventManager()->attach('option', function ($event) use ($memcached) { - $params = $event->getParams(); - if (!isset($params['namespace'])) { - // Cannot set lib options after initialization - return; + // init namespace prefix + $namespace = $options->getNamespace(); + if ($namespace !== '') { + $this->namespacePrefix = $namespace . $options->getNamespaceSeparator(); + } else { + $this->namespacePrefix = ''; } - $memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $params['namespace']); - }); - // init servers - $servers = $options->getServers(); - if ($servers) { - $memcached->addServers($servers); + // update initialized flag + $this->initialized = true; } - // use the initialized resource - $this->memcachedResource = $memcached; - - return $this->memcachedResource; + return $this->resourceManager->getResource($this->resourceId); } /* options */ @@ -210,12 +212,13 @@ public function getAvailableSpace() */ protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { - $memc = $this->getMemcachedResource(); + $memc = $this->getMemcachedResource(); + $internalKey = $this->namespacePrefix . $normalizedKey; if (func_num_args() > 2) { - $result = $memc->get($normalizedKey, null, $casToken); + $result = $memc->get($internalKey, null, $casToken); } else { - $result = $memc->get($normalizedKey); + $result = $memc->get($internalKey); } $success = true; @@ -242,12 +245,27 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo */ protected function internalGetItems(array & $normalizedKeys) { - $memc = $this->getMemcachedResource(); + $memc = $this->getMemcachedResource(); + + foreach ($normalizedKeys as & $normalizedKey) { + $normalizedKey = $this->namespacePrefix . $normalizedKey; + } + $result = $memc->getMulti($normalizedKeys); if ($result === false) { throw $this->getExceptionByResultCode($memc->getResultCode()); } + // remove namespace prefix from result + if ($result && $this->namespacePrefix !== '') { + $tmp = array(); + $nsPrefixLength = strlen($this->namespacePrefix); + foreach ($result as $internalKey => & $value) { + $tmp[ substr($internalKey, $nsPrefixLength) ] = & $value; + } + $result = $tmp; + } + return $result; } @@ -261,7 +279,7 @@ protected function internalGetItems(array & $normalizedKeys) protected function internalHasItem(& $normalizedKey) { $memc = $this->getMemcachedResource(); - $value = $memc->get($normalizedKey); + $value = $memc->get($this->namespacePrefix . $normalizedKey); if ($value === false || $value === null) { $rsCode = $memc->getResultCode(); if ($rsCode == MemcachedResource::RES_SUCCESS) { @@ -285,13 +303,29 @@ protected function internalHasItem(& $normalizedKey) */ protected function internalHasItems(array & $normalizedKeys) { - $memc = $this->getMemcachedResource(); + $memc = $this->getMemcachedResource(); + + foreach ($normalizedKeys as & $normalizedKey) { + $normalizedKey = $this->namespacePrefix . $normalizedKey; + } + $result = $memc->getMulti($normalizedKeys); if ($result === false) { throw $this->getExceptionByResultCode($memc->getResultCode()); } - return array_keys($result); + // Convert to a simgle list + $result = array_keys($result); + + // remove namespace prefix + if ($result && $this->namespacePrefix !== '') { + $nsPrefixLength = strlen($this->namespacePrefix); + foreach ($result as & $internalKey) { + $internalKey = substr($internalKey, $nsPrefixLength); + } + } + + return $result; } /** @@ -303,14 +337,29 @@ protected function internalHasItems(array & $normalizedKeys) */ protected function internalGetMetadatas(array & $normalizedKeys) { - $memc = $this->getMemcachedResource(); + $memc = $this->getMemcachedResource(); + + foreach ($normalizedKeys as & $normalizedKey) { + $normalizedKey = $this->namespacePrefix . $normalizedKey; + } + $result = $memc->getMulti($normalizedKeys); if ($result === false) { throw $this->getExceptionByResultCode($memc->getResultCode()); } - foreach ($result as & $value) { - $value = array(); + // remove namespace prefix and use an empty array as metadata + if ($this->namespacePrefix !== '') { + $tmp = array(); + $nsPrefixLength = strlen($this->namespacePrefix); + foreach (array_keys($result) as $internalKey) { + $tmp[ substr($internalKey, $nsPrefixLength) ] = array(); + } + $result = $tmp; + } else { + foreach ($result as & $value) { + $value = array(); + } } return $result; @@ -330,7 +379,7 @@ protected function internalSetItem(& $normalizedKey, & $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); - if (!$memc->set($normalizedKey, $value, $expiration)) { + if (!$memc->set($this->namespacePrefix . $normalizedKey, $value, $expiration)) { throw $this->getExceptionByResultCode($memc->getResultCode()); } @@ -348,7 +397,13 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); - if (!$memc->setMulti($normalizedKeyValuePairs, $expiration)) { + + $namespacedKeyValuePairs = array(); + foreach ($normalizedKeyValuePairs as $normalizedKey => & $value) { + $namespacedKeyValuePairs[ $this->namespacePrefix . $normalizedKey ] = & $value; + } + + if (!$memc->setMulti($namespacedKeyValuePairs, $expiration)) { throw $this->getExceptionByResultCode($memc->getResultCode()); } @@ -367,7 +422,7 @@ protected function internalAddItem(& $normalizedKey, & $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); - if (!$memc->add($normalizedKey, $value, $expiration)) { + if (!$memc->add($this->namespacePrefix . $normalizedKey, $value, $expiration)) { if ($memc->getResultCode() == MemcachedResource::RES_NOTSTORED) { return false; } @@ -389,7 +444,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); - if (!$memc->replace($normalizedKey, $value, $expiration)) { + if (!$memc->replace($this->namespacePrefix . $normalizedKey, $value, $expiration)) { $rsCode = $memc->getResultCode(); if ($rsCode == MemcachedResource::RES_NOTSTORED) { return false; @@ -415,7 +470,7 @@ protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); - $result = $memc->cas($token, $normalizedKey, $value, $expiration); + $result = $memc->cas($token, $this->namespacePrefix . $normalizedKey, $value, $expiration); if ($result === false) { $rsCode = $memc->getResultCode(); @@ -438,7 +493,7 @@ protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) protected function internalRemoveItem(& $normalizedKey) { $memc = $this->getMemcachedResource(); - $result = $memc->delete($normalizedKey); + $result = $memc->delete($this->namespacePrefix . $normalizedKey); if ($result === false) { $rsCode = $memc->getResultCode(); @@ -466,7 +521,12 @@ protected function internalRemoveItems(array & $normalizedKeys) return parent::internalRemoveItems($normalizedKeys); } - $memc = $this->getMemcachedResource(); + $memc = $this->getMemcachedResource(); + + foreach ($normalizedKeys as & $normalizedKey) { + $normalizedKey = $this->namespacePrefix . $normalizedKey; + } + $rsCodes = $memc->deleteMulti($normalizedKeys); $missingKeys = array(); @@ -479,6 +539,14 @@ protected function internalRemoveItems(array & $normalizedKeys) } } + // remove namespace prefix + if ($missingKeys && $this->namespacePrefix !== '') { + $nsPrefixLength = strlen($this->namespacePrefix); + foreach ($missingKeys as & $missingKey) { + $missingKey = substr($missingKey, $nsPrefixLength); + } + } + return $missingKeys; } @@ -492,9 +560,10 @@ protected function internalRemoveItems(array & $normalizedKeys) */ protected function internalIncrementItem(& $normalizedKey, & $value) { - $memc = $this->getMemcachedResource(); - $value = (int) $value; - $newValue = $memc->increment($normalizedKey, $value); + $memc = $this->getMemcachedResource(); + $internalKey = $this->namespacePrefix . $normalizedKey; + $value = (int) $value; + $newValue = $memc->increment($internalKey, $value); if ($newValue === false) { $rsCode = $memc->getResultCode(); @@ -502,7 +571,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) // initial value if ($rsCode == MemcachedResource::RES_NOTFOUND) { $newValue = $value; - $memc->add($normalizedKey, $newValue, $this->expirationTime()); + $memc->add($internalKey, $newValue, $this->expirationTime()); $rsCode = $memc->getResultCode(); } @@ -524,9 +593,10 @@ protected function internalIncrementItem(& $normalizedKey, & $value) */ protected function internalDecrementItem(& $normalizedKey, & $value) { - $memc = $this->getMemcachedResource(); - $value = (int) $value; - $newValue = $memc->decrement($normalizedKey, $value); + $memc = $this->getMemcachedResource(); + $internalKey = $this->namespacePrefix . $normalizedKey; + $value = (int) $value; + $newValue = $memc->decrement($internalKey, $value); if ($newValue === false) { $rsCode = $memc->getResultCode(); @@ -534,7 +604,7 @@ protected function internalDecrementItem(& $normalizedKey, & $value) // initial value if ($rsCode == MemcachedResource::RES_NOTFOUND) { $newValue = -$value; - $memc->add($normalizedKey, $newValue, $this->expirationTime()); + $memc->add($internalKey, $newValue, $this->expirationTime()); $rsCode = $memc->getResultCode(); } diff --git a/src/Storage/Adapter/MemcachedOptions.php b/src/Storage/Adapter/MemcachedOptions.php index 3a1c72240..4cf8219bb 100644 --- a/src/Storage/Adapter/MemcachedOptions.php +++ b/src/Storage/Adapter/MemcachedOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -15,39 +14,28 @@ /** * These are options specific to the APC adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class MemcachedOptions extends AdapterOptions { /** - * A memcached resource to share - * - * @var null|MemcachedResource + * The namespace separator + * @var string */ - protected $memcachedResource; + protected $namespaceSeparator = ':'; /** - * List of memcached servers to add on initialize + * The memcached resource manager * - * @var string + * @var null|MemcachedResourceManager */ - protected $servers = array( - array( - 'host' => '127.0.0.1', - 'port' => 11211, - 'weight' => 0, - ), - ); + protected $resourceManager; /** - * List of Libmemcached options to set on initialize + * The resource id of the resource manager * - * @var array + * @var string */ - protected $libOptions = array(); + protected $resourceId = 'default'; /** * Set namespace. @@ -72,17 +60,52 @@ public function setNamespace($namespace) return parent::setNamespace($namespace); } + /** + * Set namespace separator + * + * @param string $namespaceSeparator + * @return MemcachedOptions + */ + public function setNamespaceSeparator($namespaceSeparator) + { + $namespaceSeparator = (string) $namespaceSeparator; + if ($this->namespaceSeparator !== $namespaceSeparator) { + $this->triggerOptionEvent('namespace_separator', $namespaceSeparator); + $this->namespaceSeparator = $namespaceSeparator; + } + return $this; + } + + /** + * Get namespace separator + * + * @return string + */ + public function getNamespaceSeparator() + { + return $this->namespaceSeparator; + } + /** * A memcached resource to share * * @param null|MemcachedResource $memcachedResource * @return MemcachedOptions + * @deprecated Please use the resource manager instead */ public function setMemcachedResource(MemcachedResource $memcachedResource = null) { - if ($this->memcachedResource !== $memcachedResource) { + trigger_error( + 'This method is deprecated and will be removed in the feature' + . ', please use the resource manager instead', + E_USER_DEPRECATED + ); + + if ($memcachedResource !== null) { $this->triggerOptionEvent('memcached_resource', $memcachedResource); - $this->memcachedResource = $memcachedResource; + $resourceManager = $this->getResourceManager(); + $resourceId = $this->getResourceId(); + $resourceManager->setResource($resourceId, $memcachedResource); } return $this; } @@ -90,117 +113,133 @@ public function setMemcachedResource(MemcachedResource $memcachedResource = null /** * Get memcached resource to share * - * @return null|MemcachedResource + * @return MemcachedResource + * @deprecated Please use the resource manager instead */ public function getMemcachedResource() { - return $this->memcachedResource; + trigger_error( + 'This method is deprecated and will be removed in the feature' + . ', please use the resource manager instead', + E_USER_DEPRECATED + ); + + return $this->resourceManager->getResource($this->getResourceId()); } /** - * Add a server to the list + * Set the memcached resource manager to use * - * @param string $host - * @param int $port - * @param int $weight + * @param null|MemcachedResourceManager $resourceManager * @return MemcachedOptions */ - public function addServer($host, $port = 11211, $weight = 0) + public function setResourceManager(MemcachedResourceManager $resourceManager = null) { - $new = array( - 'host' => $host, - 'port' => $port, - 'weight' => $weight - ); - - foreach ($this->servers as $server) { - $diff = array_diff($new, $server); - if (empty($diff)) { - // Done -- server is already present - return $this; - } + if ($this->resourceManager !== $resourceManager) { + $this->triggerOptionEvent('resource_manager', $resourceManager); + $this->resourceManager = $resourceManager; } - - $this->servers[] = $new; return $this; } /** - * Set a list of memcached servers to add on initialize + * Get the memcached resource manager * - * @param string|array $servers list of servers - * @return MemcachedOptions - * @throws Exception\InvalidArgumentException + * @return MemcachedResourceManager */ - public function setServers($servers) + public function getResourceManager() { - if (!is_array($servers)) { - return $this->setServers(explode(',', $servers)); + if (!$this->resourceManager) { + $this->resourceManager = new MemcachedResourceManager(); } + return $this->resourceManager; + } - $this->servers = array(); - foreach ($servers as $server) { - // default values - $host = null; - $port = 11211; - $weight = 1; - - if (!is_array($server) && !is_string($server)) { - throw new Exception\InvalidArgumentException('Invalid server specification provided; must be an array or string'); - } - - // parse a single server from an array - if (is_array($server)) { - if (!isset($server[0]) && !isset($server['host'])) { - throw new Exception\InvalidArgumentException("Invalid list of servers given"); - } - - // array(array([, [, ]])[, ...]) - if (isset($server[0])) { - $host = (string) $server[0]; - $port = isset($server[1]) ? (int) $server[1] : $port; - $weight = isset($server[2]) ? (int) $server[2] : $weight; - } - - // array(array('host' => [, 'port' => [, 'weight' => ]])[, ...]) - if (!isset($server[0]) && isset($server['host'])) { - $host = (string) $server['host']; - $port = isset($server['port']) ? (int) $server['port'] : $port; - $weight = isset($server['weight']) ? (int) $server['weight'] : $weight; - } - } + /** + * Get the memcached resource id + * + * @return string + */ + public function getResourceId() + { + return $this->resourceId; + } - // parse a single server from a string - if (!is_array($server)) { - $server = trim($server); - if (strpos($server, '://') === false) { - $server = 'tcp://' . $server; - } + /** + * Set the memcached resource id + * + * @param string $resourceId + * @return MemcachedOptions + */ + public function setResourceId($resourceId) + { + $resourceId = (string) $resourceId; + if ($this->resourceId !== $resourceId) { + $this->triggerOptionEvent('resource_id', $resourceId); + $this->resourceId = $resourceId; + } + return $this; + } - $server = parse_url($server); - if (!$server) { - throw new Exception\InvalidArgumentException("Invalid list of servers given"); - } + /** + * Get the persistent id + * + * @return string + */ + public function getPersistentId() + { + return $this->getResourceManager()->getPersistentId($this->getResourceId()); + } - $host = $server['host']; - $port = isset($server['port']) ? (int) $server['port'] : $port; + /** + * Set the persistent id + * + * @param string $persistentId + * @return MemcachedOptions + */ + public function setPersistentId($persistentId) + { + $this->triggerOptionEvent('persistent_id', $persistentId); + $this->getResourceManager()->setPersistentId($this->getPersistentId(), $persistentId); + return $this; + } - if (isset($server['query'])) { - $query = null; - parse_str($server['query'], $query); - if (isset($query['weight'])) { - $weight = (int) $query['weight']; - } - } - } + /** + * Add a server to the list + * + * @param string $host + * @param int $port + * @param int $weight + * @return MemcachedOptions + * @deprecated Please use the resource manager instead + */ + public function addServer($host, $port = 11211, $weight = 0) + { + trigger_error( + 'This method is deprecated and will be removed in the feature' + . ', please use the resource manager instead', + E_USER_DEPRECATED + ); - if (!$host) { - throw new Exception\InvalidArgumentException('The list of servers must contain a host value.'); - } + $this->getResourceManager()->addServer($this->getResourceId(), array( + 'host' => $host, + 'port' => $port, + 'weight' => $weight + )); - $this->addServer($host, $port, $weight); - } + return $this; + } + /** + * Set a list of memcached servers to add on initialize + * + * @param string|array $servers list of servers + * @return MemcachedOptions + * @throws Exception\InvalidArgumentException + */ + public function setServers($servers) + { + $this->getResourceManager()->setServers($this->getResourceId(), $servers); return $this; } @@ -211,27 +250,19 @@ public function setServers($servers) */ public function getServers() { - return $this->servers; + return $this->getResourceManager()->getServers($this->getResourceId()); } /** - * Set libmemcached options - * - * @param array $libOptions - * @return MemcachedOptions - * @link http://php.net/manual/memcached.constants.php - */ + * Set libmemcached options + * + * @param array $libOptions + * @return MemcachedOptions + * @link http://php.net/manual/memcached.constants.php + */ public function setLibOptions(array $libOptions) { - $normalizedOptions = array(); - foreach ($libOptions as $key => $value) { - $this->normalizeLibOptionKey($key); - $normalizedOptions[$key] = $value; - } - - $this->triggerOptionEvent('lib_options', $normalizedOptions); - $this->libOptions = array_diff_key($this->libOptions, $normalizedOptions) + $normalizedOptions; - + $this->getResourceManager()->setLibOptions($this->getResourceId(), $libOptions); return $this; } @@ -239,16 +270,20 @@ public function setLibOptions(array $libOptions) * Set libmemcached option * * @param string|int $key - * @param mixed $value + * @param mixed $value * @return MemcachedOptions * @link http://php.net/manual/memcached.constants.php + * @deprecated Please use lib_options or the resource manager instead */ public function setLibOption($key, $value) { - $this->normalizeLibOptionKey($key); - $this->triggerOptionEvent('lib_options', array($key, $value)); - $this->libOptions[$key] = $value; + trigger_error( + 'This method is deprecated and will be removed in the feature' + . ', please use "lib_options" or the resource manager instead', + E_USER_DEPRECATED + ); + $this->getResourceManager()->setLibOption($this->getResourceId(), $key, $value); return $this; } @@ -260,41 +295,25 @@ public function setLibOption($key, $value) */ public function getLibOptions() { - return $this->libOptions; + return $this->getResourceManager()->getLibOptions($this->getResourceId()); } /** - * Get libmemcached option - * - * @param string|int $key - * @return mixed - * @link http://php.net/manual/memcached.constants.php - */ + * Get libmemcached option + * + * @param string|int $key + * @return mixed + * @link http://php.net/manual/memcached.constants.php + * @deprecated Please use lib_options or the resource manager instead + */ public function getLibOption($key) { - $this->normalizeLibOptionKey($key); - if (isset($this->libOptions[$key])) { - return $this->libOptions[$key]; - } - return null; - } + trigger_error( + 'This method is deprecated and will be removed in the feature' + . ', please use "lib_options" or the resource manager instead', + E_USER_DEPRECATED + ); - /** - * Normalize libmemcached option name into it's constant value - * - * @param string|int $key - * @throws Exception\InvalidArgumentException - */ - protected function normalizeLibOptionKey(& $key) - { - if (is_string($key)) { - $const = 'Memcached::OPT_' . str_replace(array(' ', '-'), '_', strtoupper($key)); - if (!defined($const)) { - throw new Exception\InvalidArgumentException("Unknown libmemcached option '{$key}' ({$const})"); - } - $key = constant($const); - } else { - $key = (int) $key; - } + return $this->getResourceManager()->getLibOption($this->getResourceId(), $key); } } diff --git a/src/Storage/Adapter/MemcachedResourceManager.php b/src/Storage/Adapter/MemcachedResourceManager.php new file mode 100644 index 000000000..536735648 --- /dev/null +++ b/src/Storage/Adapter/MemcachedResourceManager.php @@ -0,0 +1,549 @@ +resources[$id]); + } + + /** + * Gets a memcached resource + * + * @param string $id + * @return MemcachedResource + * @throws Exception\RuntimeException + */ + public function getResource($id) + { + if (!$this->hasResource($id)) { + throw new Exception\RuntimeException("No resource with id '{$id}'"); + } + + $resource = $this->resources[$id]; + if ($resource instanceof MemcachedResource) { + return $resource; + } + + if ($resource['persistent_id'] !== '') { + $memc = new MemcachedResource($resource['persistent_id']); + } else { + $memc = new MemcachedResource(); + } + + if (method_exists($memc, 'setOptions')) { + $memc->setOptions($resource['lib_options']); + } else { + foreach ($resource['lib_options'] as $k => $v) { + $memc->setOption($k, $v); + } + } + + // merge and add servers (with persistence id servers could be added already) + $servers = array_udiff($resource['servers'], $memc->getServerList(), array($this, 'compareServers')); + if ($servers) { + $memc->addServers($servers); + } + + // buffer and return + $this->resources[$id] = $memc; + return $memc; + } + + /** + * Set a resource + * + * @param string $id + * @param array|Traversable|MemcachedResource $resource + * @return MemcachedResourceManager Fluent interface + */ + public function setResource($id, $resource) + { + $id = (string) $id; + + if ( !($resource instanceof MemcachedResource) ) { + if ($resource instanceof Traversable) { + $resource = ArrayUtils::iteratorToArray($resource); + } elseif (!is_array($resource)) { + throw new Exception\InvalidArgumentException( + 'Resource must be an instance of Memcached or an array or Traversable' + ); + } + + $resource = array_merge(array( + 'persistent_id' => '', + 'lib_options' => array(), + 'servers' => array(), + ), $resource); + + // normalize and validate params + $this->normalizePersistentId($resource['persistent_id']); + $this->normalizeLibOptions($resource['lib_options']); + $this->normalizeServers($resource['servers']); + } + + $this->resources[$id] = $resource; + return $this; + } + + /** + * Remove a resource + * + * @param string $id + * @return MemcachedResourceManager Fluent interface + */ + public function removeResource($id) + { + unset($this->resources[$id]); + return $this; + } + + /** + * Set the persistent id + * + * @param string $id + * @param string $persistentId + * @return MemcachedResourceManager Fluent interface + * @throws Exception\RuntimeException + */ + public function setPersistentId($id, $persistentId) + { + if (!$this->hasResource($id)) { + return $this->setResource($id, array( + 'persistent_id' => $persistentId + )); + } + + $resource = & $this->resources[$id]; + if ($resource instanceof MemcachedResource) { + throw new Exception\RuntimeException( + "Can't change persistent id of resource {$id} after instanziation" + ); + } + + $this->normalizePersistentId($persistentId); + $resource['persistent_id'] = $persistentId; + + return $this; + } + + /** + * Get the persistent id + * + * @param string $id + * @return string + * @throws Exception\RuntimeException + */ + public function getPersistentId($id) + { + if (!$this->hasResource($id)) { + throw new Exception\RuntimeException("No resource with id '{$id}'"); + } + + $resource = & $this->resources[$id]; + + if ($resource instanceof MemcachedResource) { + throw new Exception\RuntimeException( + "Can't get persistent id of an instantiated memcached resource" + ); + } + + return $resource['persistent_id']; + } + + /** + * Normalize the persistent id + * + * @param string $persistentId + */ + protected function normalizePersistentId(& $persistentId) + { + $persistentId = (string) $persistentId; + } + + /** + * Set Libmemcached options + * + * @param string $id + * @param array $libOptions + * @return MemcachedResourceManager Fluent interface + */ + public function setLibOptions($id, array $libOptions) + { + if (!$this->hasResource($id)) { + return $this->setResource($id, array( + 'lib_options' => $libOptions + )); + } + + $this->normalizeLibOptions($libOptions); + + $resource = & $this->resources[$id]; + if ($resource instanceof MemcachedResource) { + if (method_exists($resource, 'setOptions')) { + $resource->setOptions($resource); + } else { + foreach ($libOptions as $key => $value) { + $resource->setOption($key, $value); + } + } + } else { + $resource['lib_options'] = $libOptions; + } + + return $this; + } + + /** + * Get Libmemcached options + * + * @param string $id + * @return array + * @throws Exception\RuntimeException + */ + public function getLibOptions($id) + { + if (!$this->hasResource($id)) { + throw new Exception\RuntimeException("No resource with id '{$id}'"); + } + + $resource = & $this->resources[$id]; + + if ($resource instanceof MemcachedResource) { + $libOptions = array(); + $reflection = new ReflectionClass('Memcached'); + $constants = $reflection->getConstants(); + foreach ($constants as $constName => $constValue) { + if (substr($constName, 0, 4) == 'OPT_') { + $libOptions[ $constValue ] = $resource->getOption($constValue); + } + } + return $libOptions; + } + return $resource['lib_options']; + } + + /** + * Set one Libmemcached option + * + * @param string $id + * @param string|int $key + * @param mixed $value + * @return MemcachedResourceManager Fluent interface + */ + public function setLibOption($id, $key, $value) + { + return $this->setLibOptions($id, array($key => $value)); + } + + /** + * Get one Libmemcached option + * + * @param string $id + * @param string|int $key + * @return mixed + * @throws Exception\RuntimeException + */ + public function getLibOption($id, $key) + { + if (!$this->hasResource($id)) { + throw new Exception\RuntimeException("No resource with id '{$id}'"); + } + + $constValue = $this->normalizeLibOptionKey($key); + $resource = & $this->resources[$id]; + + if ($resource instanceof MemcachedResource) { + return $resource->getOption($constValue); + } + + return isset($resource['lib_options'][$constValue]) ? $resource['lib_options'][$constValue] : null; + } + + /** + * Normalize libmemcached options + * + * @param array|Traversable $libOptions + * @throws Exception\InvalidArgumentException + */ + protected function normalizeLibOptions(& $libOptions) + { + if (!is_array($libOptions) && !($libOptions instanceof Traversable)) { + throw new Exception\InvalidArgumentException( + "Lib-Options must be an array or an instance of Traversable" + ); + } + + $result = array(); + foreach ($libOptions as $key => $value) { + $this->normalizeLibOptionKey($key); + $result[$key] = $value; + } + + $libOptions = $result; + } + + /** + * Convert option name into it's constant value + * + * @param string|int $key + * @throws Exception\InvalidArgumentException + */ + protected function normalizeLibOptionKey(& $key) + { + // convert option name into it's constant value + if (is_string($key)) { + $const = 'Memcached::OPT_' . str_replace(array(' ', '-'), '_', strtoupper($key)); + if (!defined($const)) { + throw new Exception\InvalidArgumentException("Unknown libmemcached option '{$key}' ({$const})"); + } + $key = constant($const); + } else { + $key = (int) $key; + } + } + + /** + * Set servers + * + * $servers can be an array list or a comma separated list of servers. + * One server in the list can be descripted as follows: + * - URI: [tcp://][:][?weight=] + * - Assoc: array('host' => [, 'port' => ][, 'weight' => ]) + * - List: array([, ][, ]) + * + * @param string $id + * @param string|array $servers + * @return MemcachedResourceManager + */ + public function setServers($id, $servers) + { + if (!$this->hasResource($id)) { + return $this->setResource($id, array( + 'servers' => $servers + )); + } + + $this->normalizeServers($servers); + + $resource = & $this->resources[$id]; + if ($resource instanceof MemcachedResource) { + // don't add servers twice + $servers = array_udiff($servers, $resource->getServerList(), array($this, 'compareServers')); + if ($servers) { + $resource->addServers($servers); + } + } else { + $resource['servers'] = $servers; + } + + return $this; + } + + /** + * Get servers + * @param string $id + * @throws Exception\RuntimeException + * @return array array('host' => , 'port' => , 'weight' => ) + */ + public function getServers($id) + { + if (!$this->hasResource($id)) { + throw new Exception\RuntimeException("No resource with id '{$id}'"); + } + + $resource = & $this->resources[$id]; + + if ($resource instanceof MemcachedResource) { + return $resource->getServerList(); + } + return $resource['servers']; + } + + /** + * Add servers + * + * @param string $id + * @param string|array $servers + * @return MemcachedResourceManager + */ + public function addServers($id, $servers) + { + if (!$this->hasResource($id)) { + return $this->setResource($id, array( + 'servers' => $servers + )); + } + + $this->normalizeServers($servers); + + $resource = & $this->resources[$id]; + if ($resource instanceof MemcachedResource) { + // don't add servers twice + $servers = array_udiff($servers, $resource->getServerList(), array($this, 'compareServers')); + if ($servers) { + $resource->addServers($servers); + } + } else { + // don't add servers twice + $resource['servers'] = array_merge( + $resource['servers'], + array_udiff($servers, $resource['servers'], array($this, 'compareServers')) + ); + } + + return $this; + } + + /** + * Add one server + * + * @param string $id + * @param string|array $server + * @return MemcachedResourceManager + */ + public function addServer($id, $server) + { + return $this->addServers($id, array($server)); + } + + /** + * Normalize a list of servers into the following format: + * array(array('host' => , 'port' => , 'weight' => )[, ...]) + * + * @param string|array $servers + */ + protected function normalizeServers(& $servers) + { + if (!is_array($servers) && !$servers instanceof Traversable) { + // Convert string into a list of servers + $servers = explode(',', $servers); + } + + $result = array(); + foreach ($servers as $server) { + $this->normalizeServer($server); + $result[ $server['host'] . ':' . $server['port'] ] = $server; + } + + $servers = array_values($result); + } + + /** + * Normalize one server into the following format: + * array('host' => , 'port' => , 'weight' => ) + * + * @param string|array $server + * @throws Exception\InvalidArgumentException + */ + protected function normalizeServer(& $server) + { + $host = null; + $port = 11211; + $weight = 0; + + // convert a single server into an array + if ($server instanceof Traversable) { + $server = ArrayUtils::iteratorToArray($server); + } + + if (is_array($server)) { + // array([, [, ]]) + if (isset($server[0])) { + $host = (string) $server[0]; + $port = isset($server[1]) ? (int) $server[1] : $port; + $weight = isset($server[2]) ? (int) $server[2] : $weight; + } + + // array('host' => [, 'port' => [, 'weight' => ]]) + if (!isset($server[0]) && isset($server['host'])) { + $host = (string)$server['host']; + $port = isset($server['port']) ? (int) $server['port'] : $port; + $weight = isset($server['weight']) ? (int) $server['weight'] : $weight; + } + + } else { + // parse server from URI host{:?port}{?weight} + $server = trim($server); + if (strpos($server, '://') === false) { + $server = 'tcp://' . $server; + } + + $server = parse_url($server); + if (!$server) { + throw new Exception\InvalidArgumentException("Invalid server given"); + } + + $host = $server['host']; + $port = isset($server['port']) ? (int) $server['port'] : $port; + + if (isset($server['query'])) { + $query = null; + parse_str($server['query'], $query); + if (isset($query['weight'])) { + $weight = (int)$query['weight']; + } + } + } + + if (!$host) { + throw new Exception\InvalidArgumentException('Missing required server host'); + } + + $server = array( + 'host' => $host, + 'port' => $port, + 'weight' => $weight, + ); + } + + /** + * Compare 2 normalized server arrays + * (Compares only the host and the port) + * + * @param array $serverA + * @param array $serverB + * @return int + */ + protected function compareServers(array $serverA, array $serverB) + { + $keyA = $serverA['host'] . ':' . $serverA['port']; + $keyB = $serverB['host'] . ':' . $serverB['port']; + if ($keyA === $keyB) { + return 0; + } + return $keyA > $keyB ? 1 : -1; + } +} diff --git a/src/Storage/Adapter/Memory.php b/src/Storage/Adapter/Memory.php index 30d8d6b32..83e3a9c86 100644 --- a/src/Storage/Adapter/Memory.php +++ b/src/Storage/Adapter/Memory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -14,6 +13,7 @@ use Zend\Cache\Exception; use Zend\Cache\Storage\AvailableSpaceCapableInterface; use Zend\Cache\Storage\Capabilities; +use Zend\Cache\Storage\ClearByNamespaceInterface; use Zend\Cache\Storage\ClearByPrefixInterface; use Zend\Cache\Storage\ClearExpiredInterface; use Zend\Cache\Storage\FlushableInterface; @@ -21,14 +21,10 @@ use Zend\Cache\Storage\TaggableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class Memory extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByPrefixInterface, + ClearByNamespaceInterface, ClearExpiredInterface, FlushableInterface, IterableInterface, @@ -174,6 +170,19 @@ public function clearExpired() return true; } + /* ClearByNamespaceInterface */ + + public function clearByNamespace($namespace) + { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + + unset($this->data[$namespace]); + return true; + } + /* ClearByPrefixInterface */ /** @@ -184,6 +193,11 @@ public function clearExpired() */ public function clearByPrefix($prefix) { + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + $ns = $this->getOptions()->getNamespace(); if (!isset($this->data[$ns])) { return true; diff --git a/src/Storage/Adapter/MemoryOptions.php b/src/Storage/Adapter/MemoryOptions.php index 5e31912c2..203ad9d0c 100644 --- a/src/Storage/Adapter/MemoryOptions.php +++ b/src/Storage/Adapter/MemoryOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -14,10 +13,6 @@ /** * These are options specific to the APC adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class MemoryOptions extends AdapterOptions { diff --git a/src/Storage/Adapter/Session.php b/src/Storage/Adapter/Session.php new file mode 100644 index 000000000..50d445353 --- /dev/null +++ b/src/Storage/Adapter/Session.php @@ -0,0 +1,547 @@ +options) { + $this->setOptions(new SessionOptions()); + } + return $this->options; + } + + /** + * Get the session container + * + * @return SessionContainer + */ + protected function getSessionContainer() + { + $sessionContainer = $this->getOptions()->getSessionContainer(); + if (!$sessionContainer) { + throw new Exception\RuntimeException("No session container configured"); + } + return $sessionContainer; + } + + /* IterableInterface */ + + /** + * Get the storage iterator + * + * @return KeyListIterator + */ + public function getIterator() + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if ($cntr->offsetExists($ns)) { + $keys = array_keys($cntr->offsetGet($ns)); + } else { + $keys = array(); + } + + return new KeyListIterator($this, $keys); + } + + /* FlushableInterface */ + + /** + * Flush the whole session container + * + * @return boolean + */ + public function flush() + { + $this->getSessionContainer()->exchangeArray(array()); + return true; + } + + /* ClearByPrefixInterface */ + + /** + * Remove items matching given prefix + * + * @param string $prefix + * @return boolean + */ + public function clearByPrefix($prefix) + { + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return true; + } + + $data = $cntr->offsetGet($ns); + $prefixL = strlen($prefix); + foreach ($data as $key => & $item) { + if (substr($key, 0, $prefixL) === $prefix) { + unset($data[$key]); + } + } + $cntr->offsetSet($ns, $data); + + return true; + } + + /* reading */ + + /** + * Internal method to get an item. + * + * @param string $normalizedKey + * @param boolean $success + * @param mixed $casToken + * @return mixed Data on success, null on failure + * @throws Exception\ExceptionInterface + */ + protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + $success = false; + return null; + } + + $data = $cntr->offsetGet($ns); + $success = array_key_exists($normalizedKey, $data); + if (!$success) { + return null; + } + + $casToken = $value = $data[$normalizedKey]; + return $value; + } + + /** + * Internal method to get multiple items. + * + * @param array $normalizedKeys + * @return array Associative array of keys and values + * @throws Exception\ExceptionInterface + */ + protected function internalGetItems(array & $normalizedKeys) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return array(); + } + + $data = $cntr->offsetGet($ns); + $result = array(); + foreach ($normalizedKeys as $normalizedKey) { + if (array_key_exists($normalizedKey, $data)) { + $result[$normalizedKey] = $data[$normalizedKey]; + } + } + + return $result; + } + + /** + * Internal method to test if an item exists. + * + * @param string $normalizedKey + * @return boolean + */ + protected function internalHasItem(& $normalizedKey) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return false; + } + + $data = $cntr->offsetGet($ns); + return array_key_exists($normalizedKey, $data); + } + + /** + * Internal method to test multiple items. + * + * @param array $normalizedKeys + * @return array Array of found keys + */ + protected function internalHasItems(array & $normalizedKeys) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return array(); + } + + $data = $cntr->offsetGet($ns); + $result = array(); + foreach ($normalizedKeys as $normalizedKey) { + if (array_key_exists($normalizedKey, $data)) { + $result[] = $normalizedKey; + } + } + + return $result; + } + + /** + * Get metadata of an item. + * + * @param string $normalizedKey + * @return array|boolean Metadata on success, false on failure + * @throws Exception\ExceptionInterface + * + * @triggers getMetadata.pre(PreEvent) + * @triggers getMetadata.post(PostEvent) + * @triggers getMetadata.exception(ExceptionEvent) + */ + protected function internalGetMetadata(& $normalizedKey) + { + return $this->internalHasItem($normalizedKey) ? array() : false; + } + + /* writing */ + + /** + * Internal method to store an item. + * + * @param string $normalizedKey + * @param mixed $value + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalSetItem(& $normalizedKey, & $value) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + $data = $cntr->offsetExists($ns) ? $cntr->offsetGet($ns) : array(); + $data[$normalizedKey] = $value; + $cntr->offsetSet($ns, $data); + return true; + } + + /** + * Internal method to store multiple items. + * + * @param array $normalizedKeyValuePairs + * @return array Array of not stored keys + * @throws Exception\ExceptionInterface + */ + protected function internalSetItems(array & $normalizedKeyValuePairs) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if ($cntr->offsetExists($ns)) { + $data = array_merge($cntr->offsetGet($ns), $normalizedKeyValuePairs); + } else { + $data = $normalizedKeyValuePairs; + } + $cntr->offsetSet($ns, $data); + + return array(); + } + + /** + * Add an item. + * + * @param string $normalizedKey + * @param mixed $value + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalAddItem(& $normalizedKey, & $value) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if ($cntr->offsetExists($ns)) { + $data = $cntr->offsetGet($ns); + + if (array_key_exists($normalizedKey, $data)) { + return false; + } + + $data[$normalizedKey] = $value; + } else { + $data = array($normalizedKey => $value); + } + + $cntr->offsetSet($ns, $data); + return true; + } + + /** + * Internal method to add multiple items. + * + * @param array $normalizedKeyValuePairs + * @return array Array of not stored keys + * @throws Exception\ExceptionInterface + */ + protected function internalAddItems(array & $normalizedKeyValuePairs) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + $result = array(); + if ($cntr->offsetExists($ns)) { + $data = $cntr->offsetGet($ns); + + foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { + if (array_key_exists($normalizedKey, $data)) { + $result[] = $normalizedKey; + } else { + $data[$normalizedKey] = $value; + } + } + } else { + $data = $normalizedKeyValuePairs; + } + + $cntr->offsetSet($ns, $data); + return $result; + } + + /** + * Internal method to replace an existing item. + * + * @param string $normalizedKey + * @param mixed $value + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalReplaceItem(& $normalizedKey, & $value) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return false; + } + + $data = $cntr->offsetGet($ns); + if (!array_key_exists($normalizedKey, $data)) { + return false; + } + $data[$normalizedKey] = $value; + $cntr->offsetSet($ns, $data); + + return true; + } + + /** + * Internal method to replace multiple existing items. + * + * @param array $normalizedKeyValuePairs + * @return array Array of not stored keys + * @throws Exception\ExceptionInterface + */ + protected function internalReplaceItems(array & $normalizedKeyValuePairs) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + if (!$cntr->offsetExists($ns)) { + return array_keys($normalizedKeyValuePairs); + } + + $data = $cntr->offsetGet($ns); + $result = array(); + foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { + if (!array_key_exists($normalizedKey, $data)) { + $result[] = $normalizedKey; + } else { + $data[$normalizedKey] = $value; + } + } + $cntr->offsetSet($ns, $data); + + return $result; + } + + /** + * Internal method to remove an item. + * + * @param string $normalizedKey + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalRemoveItem(& $normalizedKey) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if (!$cntr->offsetExists($ns)) { + return false; + } + + $data = $cntr->offsetGet($ns); + if (!array_key_exists($normalizedKey, $data)) { + return false; + } + + unset($data[$normalizedKey]); + + if (!$data) { + $cntr->offsetUnset($ns); + } else { + $cntr->offsetSet($ns, $data); + } + + return true; + } + + /** + * Internal method to increment an item. + * + * @param string $normalizedKey + * @param int $value + * @return int|boolean The new value on success, false on failure + * @throws Exception\ExceptionInterface + */ + protected function internalIncrementItem(& $normalizedKey, & $value) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if ($cntr->offsetExists($ns)) { + $data = $cntr->offsetGet($ns); + } else { + $data = array(); + } + + if (array_key_exists($normalizedKey, $data)) { + $data[$normalizedKey]+= $value; + $newValue = $data[$normalizedKey]; + } else { + // initial value + $newValue = $value; + $data[$normalizedKey] = $newValue; + } + + $cntr->offsetSet($ns, $data); + return $newValue; + } + + /** + * Internal method to decrement an item. + * + * @param string $normalizedKey + * @param int $value + * @return int|boolean The new value on success, false on failure + * @throws Exception\ExceptionInterface + */ + protected function internalDecrementItem(& $normalizedKey, & $value) + { + $cntr = $this->getSessionContainer(); + $ns = $this->getOptions()->getNamespace(); + + if ($cntr->offsetExists($ns)) { + $data = $cntr->offsetGet($ns); + } else { + $data = array(); + } + + if (array_key_exists($normalizedKey, $data)) { + $data[$normalizedKey]-= $value; + $newValue = $data[$normalizedKey]; + } else { + // initial value + $newValue = -$value; + $data[$normalizedKey] = $newValue; + } + + $cntr->offsetSet($ns, $data); + return $newValue; + } + + /* status */ + + /** + * Internal method to get capabilities of this adapter + * + * @return Capabilities + */ + protected function internalGetCapabilities() + { + if ($this->capabilities === null) { + $this->capabilityMarker = new stdClass(); + $this->capabilities = new Capabilities( + $this, + $this->capabilityMarker, + array( + 'supportedDatatypes' => array( + 'NULL' => true, + 'boolean' => true, + 'integer' => true, + 'double' => true, + 'string' => true, + 'array' => 'array', + 'object' => 'object', + 'resource' => false, + ), + 'supportedMetadata' => array(), + 'minTtl' => 0, + 'maxKeyLength' => 0, + 'namespaceIsPrefix' => false, + 'namespaceSeparator' => '', + ) + ); + } + + return $this->capabilities; + } +} diff --git a/src/Storage/Adapter/SessionOptions.php b/src/Storage/Adapter/SessionOptions.php new file mode 100644 index 000000000..d2529d558 --- /dev/null +++ b/src/Storage/Adapter/SessionOptions.php @@ -0,0 +1,52 @@ +sessionContainer != $sessionContainer) { + $this->triggerOptionEvent('session_container', $sessionContainer); + $this->sessionContainer = $sessionContainer; + } + + return $this; + } + + /** + * Get the session container + * + * @return null|SessionContainer + */ + public function getSessionContainer() + { + return $this->sessionContainer; + } +} diff --git a/src/Storage/Adapter/WinCache.php b/src/Storage/Adapter/WinCache.php index b43124c13..0f6d7fcd1 100644 --- a/src/Storage/Adapter/WinCache.php +++ b/src/Storage/Adapter/WinCache.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -18,11 +17,6 @@ use Zend\Cache\Storage\FlushableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; -/** - * @package Zend_Cache - * @subpackage Zend_Cache_Storage - * @subpackage Storage - */ class WinCache extends AbstractAdapter implements AvailableSpaceCapableInterface, FlushableInterface, @@ -139,7 +133,8 @@ public function flush() protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $result = wincache_ucache_get($internalKey, $success); @@ -159,8 +154,13 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo */ protected function internalGetItems(array & $normalizedKeys) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return wincache_ucache_get($normalizedKeys); + } + + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; @@ -187,8 +187,9 @@ protected function internalGetItems(array & $normalizedKeys) */ protected function internalHasItem(& $normalizedKey) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); return wincache_ucache_exists($prefix . $normalizedKey); } @@ -202,7 +203,8 @@ protected function internalHasItem(& $normalizedKey) protected function internalGetMetadata(& $normalizedKey) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $info = wincache_ucache_info(true, $internalKey); @@ -228,7 +230,8 @@ protected function internalGetMetadata(& $normalizedKey) protected function internalSetItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); @@ -251,19 +254,23 @@ protected function internalSetItem(& $normalizedKey, & $value) */ protected function internalSetItems(array & $normalizedKeyValuePairs) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $prefixL = strlen($prefix); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return wincache_ucache_set($normalizedKeyValuePairs, null, $options->getTtl()); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); - foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { + foreach ($normalizedKeyValuePairs as $normalizedKey => & $value) { $internalKey = $prefix . $normalizedKey; - $internalKeyValuePairs[$internalKey] = $value; + $internalKeyValuePairs[$internalKey] = & $value; } $result = wincache_ucache_set($internalKeyValuePairs, null, $options->getTtl()); // remove key prefic + $prefixL = strlen($prefix); foreach ($result as & $key) { $key = substr($key, $prefixL); } @@ -282,7 +289,8 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) protected function internalAddItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); @@ -305,10 +313,13 @@ protected function internalAddItem(& $normalizedKey, & $value) */ protected function internalAddItems(array & $normalizedKeyValuePairs) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); - $prefixL = strlen($prefix); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + return wincache_ucache_add($normalizedKeyValuePairs, null, $options->getTtl()); + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { $internalKey = $prefix . $normalizedKey; @@ -318,6 +329,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) $result = wincache_ucache_add($internalKeyValuePairs, null, $options->getTtl()); // remove key prefic + $prefixL = strlen($prefix); foreach ($result as & $key) { $key = substr($key, $prefixL); } @@ -336,7 +348,8 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) protected function internalReplaceItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; if (!wincache_ucache_exists($internalKey)) { return false; @@ -363,7 +376,8 @@ protected function internalReplaceItem(& $normalizedKey, & $value) protected function internalRemoveItem(& $normalizedKey) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; return wincache_ucache_delete($internalKey); } @@ -377,9 +391,14 @@ protected function internalRemoveItem(& $normalizedKey) */ protected function internalRemoveItems(array & $normalizedKeys) { - $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + if ($namespace === '') { + $result = wincache_ucache_delete($normalizedKeys); + return ($result === false) ? $normalizedKeys : $result; + } + $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; @@ -410,7 +429,8 @@ protected function internalRemoveItems(array & $normalizedKeys) protected function internalIncrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; return wincache_ucache_inc($internalKey, (int) $value); } @@ -426,7 +446,8 @@ protected function internalIncrementItem(& $normalizedKey, & $value) protected function internalDecrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); - $prefix = $options->getNamespace() . $options->getNamespaceSeparator(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; return wincache_ucache_dec($internalKey, (int) $value); } diff --git a/src/Storage/Adapter/WinCacheOptions.php b/src/Storage/Adapter/WinCacheOptions.php index 235c63796..c42e6c4d1 100644 --- a/src/Storage/Adapter/WinCacheOptions.php +++ b/src/Storage/Adapter/WinCacheOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -13,10 +12,6 @@ /** * These are options specific to the APC adapter - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class WinCacheOptions extends AdapterOptions { diff --git a/src/Storage/Adapter/XCache.php b/src/Storage/Adapter/XCache.php new file mode 100644 index 000000000..d20713c92 --- /dev/null +++ b/src/Storage/Adapter/XCache.php @@ -0,0 +1,529 @@ +options) { + $this->setOptions(new XCacheOptions()); + } + return $this->options; + } + + /* TotalSpaceCapableInterface */ + + /** + * Get total space in bytes + * + * @return int|float + */ + public function getTotalSpace() + { + if ($this->totalSpace === null) { + $this->totalSpace = 0; + + $this->initAdminAuth(); + $cnt = xcache_count(XC_TYPE_VAR); + for ($i=0; $i < $cnt; $i++) { + $info = xcache_info(XC_TYPE_VAR, $i); + $this->totalSpace+= $info['size']; + } + $this->resetAdminAuth(); + } + + return $this->totalSpace; + } + + /* AvailableSpaceCapableInterface */ + + /** + * Get available space in bytes + * + * @return int|float + */ + public function getAvailableSpace() + { + $availableSpace = 0; + + $this->initAdminAuth(); + $cnt = xcache_count(XC_TYPE_VAR); + for ($i = 0; $i < $cnt; $i++) { + $info = xcache_info(XC_TYPE_VAR, $i); + $availableSpace+= $info['avail']; + } + $this->resetAdminAuth(); + + return $availableSpace; + } + + + /* ClearByNamespaceInterface */ + + /** + * Remove items by given namespace + * + * @param string $namespace + * @return boolean + */ + public function clearByNamespace($namespace) + { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + + $options = $this->getOptions(); + $prefix = $namespace . $options->getNamespaceSeparator(); + + xcache_unset_by_prefix($prefix); + return true; + } + + /* ClearByPrefixInterface */ + + /** + * Remove items matching given prefix + * + * @param string $prefix + * @return boolean + */ + public function clearByPrefix($prefix) + { + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator() . $prefix; + + xcache_unset_by_prefix($prefix); + return true; + } + + /* FlushableInterface */ + + /** + * Flush the whole storage + * + * @return boolean + */ + public function flush() + { + $this->initAdminAuth(); + $cnt = xcache_count(XC_TYPE_VAR); + for ($i = 0; $i < $cnt; $i++) { + xcache_clear_cache(XC_TYPE_VAR, $i); + } + $this->resetAdminAuth(); + + return true; + } + + /* IterableInterface */ + + /** + * Get the storage iterator + * + * @return KeyListIterator + */ + public function getIterator() + { + + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $keys = array(); + + $this->initAdminAuth(); + + if ($namespace === '') { + $cnt = xcache_count(XC_TYPE_VAR); + for ($i=0; $i < $cnt; $i++) { + $list = xcache_list(XC_TYPE_VAR, $i); + foreach ($list['cache_list'] as & $item) { + $keys[] = $item['name']; + } + } + } else { + + $prefix = $namespace . $options->getNamespaceSeparator(); + $prefixL = strlen($prefix); + + $cnt = xcache_count(XC_TYPE_VAR); + for ($i=0; $i < $cnt; $i++) { + $list = xcache_list(XC_TYPE_VAR, $i); + foreach ($list['cache_list'] as & $item) { + $keys[] = substr($item['name'], $prefixL); + } + } + } + + $this->resetAdminAuth(); + + return new KeyListIterator($this, $keys); + } + + /* reading */ + + /** + * Internal method to get an item. + * + * @param string $normalizedKey + * @param boolean $success + * @param mixed $casToken + * @return mixed Data on success, null on failure + * @throws Exception\ExceptionInterface + */ + protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + + $result = xcache_get($internalKey); + $success = ($result !== null); + + if ($success) { + $casToken = $result; + } + + return $result; + } + + /** + * Internal method to test if an item exists. + * + * @param string $normalizedKey + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalHasItem(& $normalizedKey) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + return xcache_isset($prefix . $normalizedKey); + } + + /** + * Get metadata of an item. + * + * @param string $normalizedKey + * @return array|boolean Metadata on success, false on failure + * @throws Exception\ExceptionInterface + */ + protected function internalGetMetadata(& $normalizedKey) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + + if (xcache_isset($internalKey)) { + + $this->initAdminAuth(); + $cnt = xcache_count(XC_TYPE_VAR); + for ($i=0; $i < $cnt; $i++) { + $list = xcache_list(XC_TYPE_VAR, $i); + foreach ($list['cache_list'] as & $metadata) { + if ($metadata['name'] === $internalKey) { + $this->normalizeMetadata($metadata); + return $metadata; + } + } + } + $this->resetAdminAuth(); + } + + return false; + } + + /* writing */ + + /** + * Internal method to store an item. + * + * @param string $normalizedKey + * @param mixed $value + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalSetItem(& $normalizedKey, & $value) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($options === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + $ttl = $options->getTtl(); + + if (!xcache_set($internalKey, $value, $ttl)) { + $type = is_object($value) ? get_class($value) : gettype($value); + throw new Exception\RuntimeException( + "xcache_set('{$internalKey}', <{$type}>, {$ttl}) failed" + ); + } + + return true; + } + + /** + * Internal method to remove an item. + * + * @param string $normalizedKey + * @return boolean + * @throws Exception\ExceptionInterface + */ + protected function internalRemoveItem(& $normalizedKey) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + + return xcache_unset($internalKey); + } + + /** + * Internal method to increment an item. + * + * @param string $normalizedKey + * @param int $value + * @return int|boolean The new value on success, false on failure + * @throws Exception\ExceptionInterface + */ + protected function internalIncrementItem(& $normalizedKey, & $value) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + $ttl = $options->getTtl(); + $value = (int) $value; + + return xcache_inc($internalKey, $value, $ttl); + } + + /** + * Internal method to decrement an item. + * + * @param string $normalizedKey + * @param int $value + * @return int|boolean The new value on success, false on failure + * @throws Exception\ExceptionInterface + */ + protected function internalDecrementItem(& $normalizedKey, & $value) + { + $options = $this->getOptions(); + $namespace = $options->getNamespace(); + $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); + $internalKey = $prefix . $normalizedKey; + $ttl = $options->getTtl(); + $value = (int) $value; + + return xcache_dec($internalKey, $value, $ttl); + } + + /* status */ + + /** + * Internal method to get capabilities of this adapter + * + * @return Capabilities + */ + protected function internalGetCapabilities() + { + if ($this->capabilities === null) { + $marker = new stdClass(); + $capabilities = new Capabilities( + $this, + $marker, + array( + 'supportedDatatypes' => array( + 'NULL' => false, + 'boolean' => true, + 'integer' => true, + 'double' => true, + 'string' => true, + 'array' => true, + 'object' => 'object', + 'resource' => false, + ), + 'supportedMetadata' => array( + 'internal_key', + 'size', 'refcount', 'hits', + 'ctime', 'atime', 'hvalue', + ), + 'minTtl' => 1, + 'maxTtl' => (int)ini_get('xcache.var_maxttl'), + 'staticTtl' => true, + 'ttlPrecision' => 1, + 'useRequestTime' => true, + 'expiredRead' => false, + 'maxKeyLength' => 5182, + 'namespaceIsPrefix' => true, + 'namespaceSeparator' => $this->getOptions()->getNamespaceSeparator(), + ) + ); + + // update namespace separator on change option + $this->getEventManager()->attach('option', function ($event) use ($capabilities, $marker) { + $params = $event->getParams(); + + if (isset($params['namespace_separator'])) { + $capabilities->setNamespaceSeparator($marker, $params['namespace_separator']); + } + }); + + $this->capabilities = $capabilities; + $this->capabilityMarker = $marker; + } + + return $this->capabilities; + } + + /* internal */ + + /** + * Init authentication before calling admin functions + * + * @return void + */ + protected function initAdminAuth() + { + $options = $this->getOptions(); + + if ($options->getAdminAuth()) { + $adminUser = $options->getAdminUser(); + $adminPass = $options->getAdminPass(); + + // backup HTTP authentication properties + if (isset($_SERVER['PHP_AUTH_USER'])) { + $this->backupAuth['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER']; + } + if (isset($_SERVER['PHP_AUTH_PW'])) { + $this->backupAuth['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW']; + } + + // set authentication + $_SERVER['PHP_AUTH_USER'] = $adminUser; + $_SERVER['PHP_AUTH_PW'] = $adminPass; + } + } + + /** + * Reset authentication after calling admin functions + * + * @return void + */ + protected function resetAdminAuth() + { + unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); + $_SERVER = $this->backupAuth + $_SERVER; + $this->backupAuth = array(); + } + + /** + * Normalize metadata to work with XCache + * + * @param array $metadata + */ + protected function normalizeMetadata(array & $metadata) + { + $metadata['internal_key'] = &$metadata['name']; + unset($metadata['name']); + } +} diff --git a/src/Storage/Adapter/XCacheOptions.php b/src/Storage/Adapter/XCacheOptions.php new file mode 100644 index 000000000..92e37525a --- /dev/null +++ b/src/Storage/Adapter/XCacheOptions.php @@ -0,0 +1,146 @@ +triggerOptionEvent('namespace_separator', $namespaceSeparator); + $this->namespaceSeparator = $namespaceSeparator; + return $this; + } + + /** + * Get namespace separator + * + * @return string + */ + public function getNamespaceSeparator() + { + return $this->namespaceSeparator; + } + + /** + * Set username to call admin functions + * + * @param null|string $adminUser + * @return XCacheOptions + */ + public function setAdminUser($adminUser) + { + $adminUser = ($adminUser === null) ? null : (string)$adminUser; + if ($this->adminUser !== $adminUser) { + $this->triggerOptionEvent('admin_user', $adminUser); + $this->adminUser = $adminUser; + } + return $this; + } + + /** + * Get username to call admin functions + * + * @return string + */ + public function getAdminUser() + { + return $this->adminUser; + } + + /** + * Enable/Disable admin authentication handling + * + * @param boolean $adminAuth + * @return XCacheOptions + */ + public function setAdminAuth($adminAuth) + { + $adminAuth = (boolean)$adminAuth; + if ($this->adminAuth !== $adminAuth) { + $this->triggerOptionEvent('admin_auth', $adminAuth); + $this->adminAuth = $adminAuth; + } + return $this; + } + + /** + * Get admin authentication enabled + * + * @return boolean + */ + public function getAdminAuth() + { + return $this->adminAuth; + } + + /** + * Set password to call admin functions + * + * @param null|string $adminPass + * @return XCacheOptions + */ + public function setAdminPass($adminPass) + { + $adminPass = ($adminPass === null) ? null : (string)$adminPass; + if ($this->adminPass !== $adminPass) { + $this->triggerOptionEvent('admin_pass', $adminPass); + $this->adminPass = $adminPass; + } + return $this; + } + + /** + * Get password to call admin functions + * + * @return string + */ + public function getAdminPass() + { + return $this->adminPass; + } +} diff --git a/src/Storage/Adapter/ZendServerDisk.php b/src/Storage/Adapter/ZendServerDisk.php index 10aa2ae53..a83abcb26 100644 --- a/src/Storage/Adapter/ZendServerDisk.php +++ b/src/Storage/Adapter/ZendServerDisk.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -17,11 +16,6 @@ use Zend\Cache\Storage\TotalSpaceCapableInterface; use Zend\Stdlib\ErrorHandler; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class ZendServerDisk extends AbstractZendServer implements AvailableSpaceCapableInterface, ClearByNamespaceInterface, @@ -75,6 +69,11 @@ public function flush() */ public function clearByNamespace($namespace) { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + return zend_disk_cache_clear($namespace); } @@ -88,8 +87,8 @@ public function clearByNamespace($namespace) */ public function getTotalSpace() { - if ($this->totalSpace !== null) { - $path = $this->getOptions()->getCacheDir(); + if ($this->totalSpace === null) { + $path = ini_get('zend_datacache.disk.save_path'); ErrorHandler::start(); $total = disk_total_space($path); @@ -97,6 +96,8 @@ public function getTotalSpace() if ($total === false) { throw new Exception\RuntimeException("Can't detect total space of '{$path}'", 0, $error); } + + $this->totalSpace = $total; } return $this->totalSpace; } @@ -111,7 +112,7 @@ public function getTotalSpace() */ public function getAvailableSpace() { - $path = $this->getOptions()->getCacheDir(); + $path = ini_get('zend_datacache.disk.save_path'); ErrorHandler::start(); $avail = disk_free_space($path); diff --git a/src/Storage/Adapter/ZendServerShm.php b/src/Storage/Adapter/ZendServerShm.php index 6994aadff..6ada0dc98 100644 --- a/src/Storage/Adapter/ZendServerShm.php +++ b/src/Storage/Adapter/ZendServerShm.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Adapter; @@ -15,11 +14,6 @@ use Zend\Cache\Storage\FlushableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class ZendServerShm extends AbstractZendServer implements ClearByNamespaceInterface, FlushableInterface, @@ -65,6 +59,11 @@ public function flush() */ public function clearByNamespace($namespace) { + $namespace = (string) $namespace; + if ($namespace === '') { + throw new Exception\InvalidArgumentException('No namespace given'); + } + return zend_shm_cache_clear($namespace); } diff --git a/src/Storage/AdapterPluginManager.php b/src/Storage/AdapterPluginManager.php index c73e675d7..146d68e3a 100644 --- a/src/Storage/AdapterPluginManager.php +++ b/src/Storage/AdapterPluginManager.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -19,10 +18,6 @@ * Enforces that adapters retrieved are instances of * StorageInterface. Additionally, it registers a number of default * adapters available. - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class AdapterPluginManager extends AbstractPluginManager { @@ -33,10 +28,12 @@ class AdapterPluginManager extends AbstractPluginManager */ protected $invokableClasses = array( 'apc' => 'Zend\Cache\Storage\Adapter\Apc', + 'dba' => 'Zend\Cache\Storage\Adapter\Dba', 'filesystem' => 'Zend\Cache\Storage\Adapter\Filesystem', 'memcached' => 'Zend\Cache\Storage\Adapter\Memcached', 'memory' => 'Zend\Cache\Storage\Adapter\Memory', - 'dba' => 'Zend\Cache\Storage\Adapter\Dba', + 'session' => 'Zend\Cache\Storage\Adapter\Session', + 'xcache' => 'Zend\Cache\Storage\Adapter\XCache', 'wincache' => 'Zend\Cache\Storage\Adapter\WinCache', 'zendserverdisk' => 'Zend\Cache\Storage\Adapter\ZendServerDisk', 'zendservershm' => 'Zend\Cache\Storage\Adapter\ZendServerShm', diff --git a/src/Storage/AvailableSpaceCapableInterface.php b/src/Storage/AvailableSpaceCapableInterface.php index 378e93eed..074cafe07 100644 --- a/src/Storage/AvailableSpaceCapableInterface.php +++ b/src/Storage/AvailableSpaceCapableInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface AvailableSpaceCapableInterface { /** diff --git a/src/Storage/Capabilities.php b/src/Storage/Capabilities.php index c2e83e0d8..3c00dd1b1 100644 --- a/src/Storage/Capabilities.php +++ b/src/Storage/Capabilities.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -15,11 +14,6 @@ use Zend\Cache\Exception; use Zend\EventManager\EventsCapableInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class Capabilities { /** diff --git a/src/Storage/ClearByNamespaceInterface.php b/src/Storage/ClearByNamespaceInterface.php index b20de717b..7415936aa 100644 --- a/src/Storage/ClearByNamespaceInterface.php +++ b/src/Storage/ClearByNamespaceInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface ClearByNamespaceInterface { /** diff --git a/src/Storage/ClearByPrefixInterface.php b/src/Storage/ClearByPrefixInterface.php index 24673856b..d50983ad4 100644 --- a/src/Storage/ClearByPrefixInterface.php +++ b/src/Storage/ClearByPrefixInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface ClearByPrefixInterface { /** diff --git a/src/Storage/ClearExpiredInterface.php b/src/Storage/ClearExpiredInterface.php index 879cfde40..a7c8a561b 100644 --- a/src/Storage/ClearExpiredInterface.php +++ b/src/Storage/ClearExpiredInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface ClearExpiredInterface { /** diff --git a/src/Storage/Event.php b/src/Storage/Event.php index 7d8ffffd7..a9e3c2a82 100644 --- a/src/Storage/Event.php +++ b/src/Storage/Event.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -13,11 +12,6 @@ use ArrayObject; use Zend\EventManager\Event as BaseEvent; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class Event extends BaseEvent { /** diff --git a/src/Storage/ExceptionEvent.php b/src/Storage/ExceptionEvent.php index 1998b36ed..ac53fba7d 100644 --- a/src/Storage/ExceptionEvent.php +++ b/src/Storage/ExceptionEvent.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -13,11 +12,6 @@ use ArrayObject; use Exception; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class ExceptionEvent extends PostEvent { /** diff --git a/src/Storage/FlushableInterface.php b/src/Storage/FlushableInterface.php index 64103560f..82ccd2f34 100644 --- a/src/Storage/FlushableInterface.php +++ b/src/Storage/FlushableInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface FlushableInterface { /** diff --git a/src/Storage/IterableInterface.php b/src/Storage/IterableInterface.php index 917206ea3..acb2fa790 100644 --- a/src/Storage/IterableInterface.php +++ b/src/Storage/IterableInterface.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -13,9 +12,6 @@ use IteratorAggregate; /** - * @category Zend - * @package Zend_Cache - * @subpackage Storage * * @method IteratorInterface getIterator() Get the storage iterator */ diff --git a/src/Storage/IteratorInterface.php b/src/Storage/IteratorInterface.php index ff75cc1c1..eb246a3f9 100644 --- a/src/Storage/IteratorInterface.php +++ b/src/Storage/IteratorInterface.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; use Iterator; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface IteratorInterface extends Iterator { diff --git a/src/Storage/OptimizableInterface.php b/src/Storage/OptimizableInterface.php index 32d56c8ab..044040304 100644 --- a/src/Storage/OptimizableInterface.php +++ b/src/Storage/OptimizableInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface OptimizableInterface { /** diff --git a/src/Storage/Plugin/AbstractPlugin.php b/src/Storage/Plugin/AbstractPlugin.php index 32ddf9b74..f269d2d7c 100644 --- a/src/Storage/Plugin/AbstractPlugin.php +++ b/src/Storage/Plugin/AbstractPlugin.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ abstract class AbstractPlugin implements PluginInterface { /** diff --git a/src/Storage/Plugin/ClearExpiredByFactor.php b/src/Storage/Plugin/ClearExpiredByFactor.php index 0e41c6165..b35d74274 100644 --- a/src/Storage/Plugin/ClearExpiredByFactor.php +++ b/src/Storage/Plugin/ClearExpiredByFactor.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -15,11 +14,6 @@ use Zend\Cache\Storage\PostEvent; use Zend\EventManager\EventManagerInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class ClearExpiredByFactor extends AbstractPlugin { /** diff --git a/src/Storage/Plugin/ExceptionHandler.php b/src/Storage/Plugin/ExceptionHandler.php index ab84ce540..3af2432fa 100644 --- a/src/Storage/Plugin/ExceptionHandler.php +++ b/src/Storage/Plugin/ExceptionHandler.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -14,10 +13,6 @@ use Zend\Cache\Storage\ExceptionEvent; use Zend\EventManager\EventManagerInterface; -/** - * @category Zend - * @package Zend_Cache - */ class ExceptionHandler extends AbstractPlugin { /** diff --git a/src/Storage/Plugin/IgnoreUserAbort.php b/src/Storage/Plugin/IgnoreUserAbort.php index a621e5a10..5e6ba9f8d 100644 --- a/src/Storage/Plugin/IgnoreUserAbort.php +++ b/src/Storage/Plugin/IgnoreUserAbort.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -14,11 +13,6 @@ use Zend\Cache\Storage\Event; use Zend\EventManager\EventManagerInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class IgnoreUserAbort extends AbstractPlugin { /** diff --git a/src/Storage/Plugin/OptimizeByFactor.php b/src/Storage/Plugin/OptimizeByFactor.php index 94bb3435c..a24aa4e1a 100644 --- a/src/Storage/Plugin/OptimizeByFactor.php +++ b/src/Storage/Plugin/OptimizeByFactor.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -15,11 +14,6 @@ use Zend\Cache\Storage\PostEvent; use Zend\EventManager\EventManagerInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class OptimizeByFactor extends AbstractPlugin { /** diff --git a/src/Storage/Plugin/PluginInterface.php b/src/Storage/Plugin/PluginInterface.php index 6c2ba1501..2cade27fe 100644 --- a/src/Storage/Plugin/PluginInterface.php +++ b/src/Storage/Plugin/PluginInterface.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; use Zend\EventManager\ListenerAggregateInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage_Plugin - */ interface PluginInterface extends ListenerAggregateInterface { /** diff --git a/src/Storage/Plugin/PluginOptions.php b/src/Storage/Plugin/PluginOptions.php index 949a636f5..a5b8120c1 100644 --- a/src/Storage/Plugin/PluginOptions.php +++ b/src/Storage/Plugin/PluginOptions.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -15,11 +14,6 @@ use Zend\Serializer\Serializer as SerializerFactory; use Zend\Stdlib\AbstractOptions; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class PluginOptions extends AbstractOptions { /** diff --git a/src/Storage/Plugin/Serializer.php b/src/Storage/Plugin/Serializer.php index 8df2a5f26..dec97804e 100644 --- a/src/Storage/Plugin/Serializer.php +++ b/src/Storage/Plugin/Serializer.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage\Plugin; @@ -17,11 +16,6 @@ use Zend\Cache\Storage\PostEvent; use Zend\EventManager\EventManagerInterface; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class Serializer extends AbstractPlugin { /** diff --git a/src/Storage/PluginManager.php b/src/Storage/PluginManager.php index 425e9a04c..0a1693d91 100644 --- a/src/Storage/PluginManager.php +++ b/src/Storage/PluginManager.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; @@ -19,10 +18,6 @@ * Enforces that plugins retrieved are instances of * Plugin\PluginInterface. Additionally, it registers a number of default * plugins available. - * - * @category Zend - * @package Zend_Cache - * @subpackage Storage */ class PluginManager extends AbstractPluginManager { diff --git a/src/Storage/PostEvent.php b/src/Storage/PostEvent.php index 2e0e4babb..9800e0a24 100644 --- a/src/Storage/PostEvent.php +++ b/src/Storage/PostEvent.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; use ArrayObject; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ class PostEvent extends Event { /** diff --git a/src/Storage/StorageInterface.php b/src/Storage/StorageInterface.php index 7b03c7e0d..f1fb22b10 100644 --- a/src/Storage/StorageInterface.php +++ b/src/Storage/StorageInterface.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; use Traversable; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface StorageInterface { /** diff --git a/src/Storage/TaggableInterface.php b/src/Storage/TaggableInterface.php index 410fbdc38..7c3fc6cba 100644 --- a/src/Storage/TaggableInterface.php +++ b/src/Storage/TaggableInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface TaggableInterface { /** diff --git a/src/Storage/TotalSpaceCapableInterface.php b/src/Storage/TotalSpaceCapableInterface.php index 427368ea6..b8fff572c 100644 --- a/src/Storage/TotalSpaceCapableInterface.php +++ b/src/Storage/TotalSpaceCapableInterface.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache\Storage; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ interface TotalSpaceCapableInterface { /** diff --git a/src/StorageFactory.php b/src/StorageFactory.php index c17b31b97..6bde52f31 100644 --- a/src/StorageFactory.php +++ b/src/StorageFactory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Cache */ namespace Zend\Cache; @@ -13,12 +12,7 @@ use Traversable; use Zend\Stdlib\ArrayUtils; -/** - * @category Zend - * @package Zend_Cache - * @subpackage Storage - */ -class StorageFactory +abstract class StorageFactory { /** * Plugin manager for loading adapters diff --git a/test/Service/StorageCacheFactoryTest.php b/test/Service/StorageCacheFactoryTest.php new file mode 100644 index 000000000..2fa9597d7 --- /dev/null +++ b/test/Service/StorageCacheFactoryTest.php @@ -0,0 +1,49 @@ +sm = new ServiceManager(); + $this->sm->setService('Config', array('cache' => array( + 'adapter' => 'Memory', + 'plugins' => array('Serializer', 'ClearExpiredByFactor'), + ))); + $this->sm->setFactory('CacheFactory', 'Zend\Cache\Service\StorageCacheFactory'); + } + + public function tearDown() + { + Cache\StorageFactory::resetAdapterPluginManager(); + Cache\StorageFactory::resetPluginManager(); + } + + public function testCreateServiceCache() + { + $cache = $this->sm->get('CacheFactory'); + $this->assertEquals('Zend\Cache\Storage\Adapter\Memory', get_class($cache)); + } +} diff --git a/test/Storage/Adapter/CommonAdapterTest.php b/test/Storage/Adapter/CommonAdapterTest.php index 003d2e489..eb12f93c1 100644 --- a/test/Storage/Adapter/CommonAdapterTest.php +++ b/test/Storage/Adapter/CommonAdapterTest.php @@ -10,6 +10,7 @@ namespace ZendTest\Cache\Storage\Adapter; +use Zend\Cache\Storage\AvailableSpaceCapableInterface; use Zend\Cache\Storage\IterableInterface; use Zend\Cache\Storage\IteratorInterface; use Zend\Cache\Storage\StorageInterface; @@ -19,8 +20,8 @@ use Zend\Cache\Storage\FlushableInterface; use Zend\Cache\Storage\OptimizableInterface; use Zend\Cache\Storage\TaggableInterface; -use Zend\Cache\Storage\AvailableSpaceCapableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; +use Zend\Http\Header\Expires; use Zend\Stdlib\ErrorHandler; /** @@ -235,7 +236,11 @@ public function testHasItemReturnsFalseOnExpiredItem() $wait = $ttl + $capabilities->getTtlPrecision(); usleep($wait * 2000000); - $this->assertFalse($this->_storage->hasItem('key')); + if (!$capabilities->getUseRequestTime()) { + $this->assertFalse($this->_storage->hasItem('key')); + } else { + $this->assertTrue($this->_storage->hasItem('key')); + } } public function testHasItemNonReadable() @@ -397,8 +402,10 @@ public function testGetMetadatasReturnsEmptyArrayIfNonReadable() $this->assertEquals(array(), $this->_storage->getMetadatas(array('key'))); } - public function testSetGetHasAndRemoveItem() + public function testSetGetHasAndRemoveItemWithoutNamespace() { + $this->_storage->getOptions()->setNamespace(''); + $this->assertTrue($this->_storage->setItem('key', 'value')); $this->assertEquals('value', $this->_storage->getItem('key')); $this->assertTrue($this->_storage->hasItem('key')); @@ -408,8 +415,10 @@ public function testSetGetHasAndRemoveItem() $this->assertNull($this->_storage->getItem('key')); } - public function testSetGetHasAndRemoveItems() + public function testSetGetHasAndRemoveItemsWithoutNamespace() { + $this->_storage->getOptions()->setNamespace(''); + $items = array( 'key1' => 'value1', 'key2' => 'value2', @@ -545,12 +554,14 @@ public function testSetAndGetExpiredItem() $wait = $ttl + $capabilities->getTtlPrecision(); usleep($wait * 2000000); - if (!$capabilities->getUseRequestTime()) { - $this->assertNull($this->_storage->getItem('key')); - } else { + if ($capabilities->getUseRequestTime()) { + // Can't test much more if the request time will be used $this->assertEquals('value', $this->_storage->getItem('key')); + return; } + $this->assertNull($this->_storage->getItem('key')); + $this->_options->setTtl(0); if ($capabilities->getExpiredRead()) { $this->assertEquals('value', $this->_storage->getItem('key')); @@ -930,6 +941,16 @@ public function testClearByPrefix() $this->assertTrue($this->_storage->hasItem('test')); } + public function testClearByPrefixThrowsInvalidArgumentExceptionOnEmptyPrefix() + { + if (!($this->_storage instanceof ClearByPrefixInterface)) { + $this->markTestSkipped("Storage doesn't implement ClearByPrefixInterface"); + } + + $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException'); + $this->_storage->clearByPrefix(''); + } + public function testClearByNamespace() { if (!($this->_storage instanceof ClearByNamespaceInterface)) { @@ -964,6 +985,16 @@ public function testClearByNamespace() $this->assertFalse($this->_storage->hasItem('key2')); } + public function testClearByNamespaceThrowsInvalidArgumentExceptionOnEmptyNamespace() + { + if (!($this->_storage instanceof ClearByNamespaceInterface)) { + $this->markTestSkipped("Storage doesn't implement ClearByNamespaceInterface"); + } + + $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException'); + $this->_storage->clearByNamespace(''); + } + public function testClearExpired() { if (!($this->_storage instanceof ClearExpiredInterface)) { diff --git a/test/Storage/Adapter/MemcachedResourceManagerTest.php b/test/Storage/Adapter/MemcachedResourceManagerTest.php new file mode 100644 index 000000000..fd4656ace --- /dev/null +++ b/test/Storage/Adapter/MemcachedResourceManagerTest.php @@ -0,0 +1,236 @@ +resourceManager = new MemcachedResourceManager(); + } + + /** + * Data provider to test valid resources + * + * Returns an array of the following structure: + * array(array( + * , + * , + * , + * , + * + * )[, ...]) + * + * @return array + */ + public function validResourceProvider() + { + $data = array( + // empty resource + array( + 'testEmptyResource', + array(), + '', + array(), + array(), + ), + + // stringify persistent id + array( + 'testStringifyPersistentId', + array('persistent_id' => 1234), + '1234', + array(), + array(), + ), + + // servers given as string + array( + 'testServersGivenAsString', + array( + 'servers' => '127.0.0.1:1234,127.0.0.1,192.1.0.1?weight=3,localhost,127.0.0.1:11211?weight=0', + ), + '', + array( + array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0), + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0), + array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3), + array('host' => 'localhost', 'port' => 11211, 'weight' => 0), + ), + array(), + ), + + // servers given as list of strings + array( + 'testServersGivenAsListOfStrings', + array( + 'servers' => array( + '127.0.0.1:1234', + '127.0.0.1', + '192.1.0.1?weight=3', + 'localhost', + '127.0.0.1:11211?weight=0' + ), + ), + '', + array( + array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0), + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0), + array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3), + array('host' => 'localhost', 'port' => 11211, 'weight' => 0), + ), + array(), + ), + + // servers given as list of arrays + array( + 'testServersGivenAsListOfArrays', + array( + 'servers' => array( + array('127.0.0.1', 1234), + array('127.0.0.1'), + array('192.1.0.1', 11211, 3), + array('localhost'), + array('127.0.0.1', 11211, 0), + ), + ), + '', + array( + array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0), + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0), + array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3), + array('host' => 'localhost', 'port' => 11211, 'weight' => 0), + ), + array(), + ), + + // servers given as list of assoc arrays + array( + 'testServersGivenAsListOfAssocArrays', + array( + 'servers' => array( + array( + 'host' => '127.0.0.1', + 'port' => 1234, + ), + array( + 'host' => '127.0.0.1', + ), + array( + 'host' => '192.1.0.1', + 'weight' => 3, + ), + array( + 'host' => 'localhost', + ), + array( + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 0, + ), + ), + ), + '', + array( + array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0), + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0), + array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3), + array('host' => 'localhost', 'port' => 11211, 'weight' => 0), + ), + array(), + ), + + // lib options given as name + array( + 'testLibOptionsGivenAsName', + array( + 'lib_options' => array( + 'COMPRESSION' => false, + 'PREFIX_KEY' => 'test_', + ), + ), + '', + array(), + class_exists('Memcached', false) ? array( + \Memcached::OPT_COMPRESSION => false, + \Memcached::OPT_PREFIX_KEY => 'test_', + ) : array(), + ), + + // lib options given as constant value + array( + 'testLibOptionsGivenAsName', + array( + 'lib_options' => class_exists('Memcached', false) ? array( + \Memcached::OPT_COMPRESSION => false, + \Memcached::OPT_PREFIX_KEY => 'test_', + ) : array(), + ), + '', + array(), + class_exists('Memcached', false) ? array( + \Memcached::OPT_COMPRESSION => false, + \Memcached::OPT_PREFIX_KEY => 'test_', + ) : array(), + ), + ); + + return $data; + } + + /** + * @dataProvider validResourceProvider + * @param string $resourceId + * @param mixed $resource + * @param string $expectedPersistentId + * @param array $expectedServers + * @param array $expectedLibOptions + */ + public function testValidResources($resourceId, $resource, $expectedPersistentId, $expectedServers, $expectedLibOptions) + { + // php-memcached is required to set libmemcached options + if (is_array($resource) && isset($resource['lib_options']) && count($resource['lib_options']) > 0) { + if (!class_exists('Memcached', false)) { + $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException', 'Unknown libmemcached option'); + } + } + + $this->assertSame($this->resourceManager, $this->resourceManager->setResource($resourceId, $resource)); + $this->assertTrue($this->resourceManager->hasResource($resourceId)); + + $this->assertSame($expectedPersistentId, $this->resourceManager->getPersistentId($resourceId)); + $this->assertEquals($expectedServers, $this->resourceManager->getServers($resourceId)); + $this->assertEquals($expectedLibOptions, $this->resourceManager->getLibOptions($resourceId)); + + $this->assertSame($this->resourceManager, $this->resourceManager->removeResource($resourceId)); + $this->assertFalse($this->resourceManager->hasResource($resourceId)); + } +} diff --git a/test/Storage/Adapter/MemcachedTest.php b/test/Storage/Adapter/MemcachedTest.php index a67be00a0..d81cb983e 100644 --- a/test/Storage/Adapter/MemcachedTest.php +++ b/test/Storage/Adapter/MemcachedTest.php @@ -31,11 +31,18 @@ public function setUp() $this->markTestSkipped("Memcached extension is not loaded"); } - $this->_options = new Cache\Storage\Adapter\MemcachedOptions(); + $this->_options = new Cache\Storage\Adapter\MemcachedOptions(array( + 'resource_id' => __CLASS__ + )); + if (defined('TESTS_ZEND_CACHE_MEMCACHED_HOST') && defined('TESTS_ZEND_CACHE_MEMCACHED_PORT')) { - $this->_options->addServer(TESTS_ZEND_CACHE_MEMCACHED_HOST, TESTS_ZEND_CACHE_MEMCACHED_PORT); + $this->_options->getResourceManager()->setServers(__CLASS__, array( + array(TESTS_ZEND_CACHE_MEMCACHED_HOST, TESTS_ZEND_CACHE_MEMCACHED_PORT) + )); } elseif (defined('TESTS_ZEND_CACHE_MEMCACHED_HOST')) { - $this->_options->addServer(TESTS_ZEND_CACHE_MEMCACHED_HOST); + $this->_options->getResourceManager()->setServers(__CLASS__, array( + array(TESTS_ZEND_CACHE_MEMCACHED_HOST) + )); } $this->_storage = new Cache\Storage\Adapter\Memcached(); @@ -45,13 +52,25 @@ public function setUp() parent::setUp(); } + /** + * @deprecated + */ public function testOptionsAddServer() { $options = new Cache\Storage\Adapter\MemcachedOptions(); + + $deprecated = false; + set_error_handler(function () use (& $deprecated) { + $deprecated = true; + }, E_USER_DEPRECATED); + $options->addServer('127.0.0.1', 11211); $options->addServer('localhost'); $options->addServer('domain.com', 11215); + restore_error_handler(); + $this->assertTrue($deprecated, 'Missing deprecated error'); + $servers = array( array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0), array('host' => 'localhost', 'port' => 11211, 'weight' => 0), @@ -68,7 +87,7 @@ public function getServersDefinitions() $expectedServers = array( array('host' => '127.0.0.1', 'port' => 12345, 'weight' => 1), array('host' => 'localhost', 'port' => 54321, 'weight' => 2), - array('host' => 'examp.com', 'port' => 11211, 'weight' => 1), + array('host' => 'examp.com', 'port' => 11211, 'weight' => 0), ); return array( @@ -111,7 +130,6 @@ public function getServersDefinitions() } /** - * * @dataProvider getServersDefinitions */ public function testOptionSetServers($servers, $expectedServers) @@ -129,7 +147,9 @@ public function testLibOptionsSet() 'COMPRESSION' => false )); - $this->assertEquals($options->getLibOption(\Memcached::OPT_COMPRESSION), false); + $this->assertEquals($options->getResourceManager()->getLibOption( + $options->getResourceId(), \Memcached::OPT_COMPRESSION + ), false); $memcached = new Cache\Storage\Adapter\Memcached($options); $this->assertEquals($memcached->getOptions()->getLibOptions(), array( @@ -137,17 +157,31 @@ public function testLibOptionsSet() )); } - public function testNoOptionsSetsDefaultServer() + /** + * @deprecated + */ + public function testLibOptionSet() { - $memcached = new Cache\Storage\Adapter\Memcached(); + $options = new Cache\Storage\Adapter\MemcachedOptions(); - $expected = array(array( - 'host' => '127.0.0.1', - 'port' => 11211, - 'weight' => 0, - )); + $deprecated = false; + set_error_handler(function () use (& $deprecated) { + $deprecated = true; + }, E_USER_DEPRECATED); + + $options->setLibOption('COMPRESSION', false); + + restore_error_handler(); + $this->assertTrue($deprecated, 'Missing deprecated error'); - $this->assertEquals($expected, $memcached->getOptions()->getServers()); + $this->assertEquals($options->getResourceManager()->getLibOption( + $options->getResourceId(), \Memcached::OPT_COMPRESSION + ), false); + + $memcached = new Cache\Storage\Adapter\Memcached($options); + $this->assertEquals($memcached->getOptions()->getLibOptions(), array( + \Memcached::OPT_COMPRESSION => false + )); } public function tearDown() diff --git a/test/Storage/Adapter/SessionTest.php b/test/Storage/Adapter/SessionTest.php new file mode 100644 index 000000000..ac3ca03f7 --- /dev/null +++ b/test/Storage/Adapter/SessionTest.php @@ -0,0 +1,49 @@ + 'Zend\Session\Storage\ArrayStorage')); + $sessionManager = $manager = new TestSessionManager($sessionConfig); + $sessionContainer = new SessionContainer('Default', $manager); + + $this->_options = new Cache\Storage\Adapter\SessionOptions(array( + 'session_container' => $sessionContainer + )); + $this->_storage = new Cache\Storage\Adapter\Session(); + $this->_storage->setOptions($this->_options); + + parent::setUp(); + } + + public function tearDown() + { + $_SESSION = array(); + SessionContainer::setDefaultManager(null); + } +} diff --git a/test/Storage/Adapter/XCacheTest.php b/test/Storage/Adapter/XCacheTest.php new file mode 100644 index 000000000..3af38d533 --- /dev/null +++ b/test/Storage/Adapter/XCacheTest.php @@ -0,0 +1,81 @@ +markTestSkipped("Skipped by TestConfiguration (TESTS_ZEND_CACHE_XCACHE_ENABLED)"); + } + + if (!extension_loaded('xcache')) { + try { + new Cache\Storage\Adapter\XCache(); + $this->fail("Expected exception Zend\Cache\Exception\ExtensionNotLoadedException"); + } catch (Cache\Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped($e->getMessage()); + } + } + + if (PHP_SAPI == 'cli') { + try { + new Cache\Storage\Adapter\XCache(); + $this->fail("Expected exception Zend\Cache\Exception\ExtensionNotLoadedException"); + } catch (Cache\Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped($e->getMessage()); + } + } + + if ((int)ini_get('xcache.var_size') <= 0) { + try { + new Cache\Storage\Adapter\XCache(); + $this->fail("Expected exception Zend\Cache\Exception\ExtensionNotLoadedException"); + } catch (Cache\Exception\ExtensionNotLoadedException $e) { + $this->markTestSkipped($e->getMessage()); + } + } + + $this->_options = new Cache\Storage\Adapter\XCacheOptions(array( + 'admin_auth' => defined('TESTS_ZEND_CACHE_XCACHE_ADMIN_AUTH') ? TESTS_ZEND_CACHE_XCACHE_ADMIN_AUTH : false, + 'admin_user' => defined('TESTS_ZEND_CACHE_XCACHE_ADMIN_USER') ? TESTS_ZEND_CACHE_XCACHE_ADMIN_USER : '', + 'admin_pass' => defined('TESTS_ZEND_CACHE_XCACHE_ADMIN_PASS') ? TESTS_ZEND_CACHE_XCACHE_ADMIN_PASS : '', + )); + $this->_storage = new Cache\Storage\Adapter\XCache(); + $this->_storage->setOptions($this->_options); + + // dfsdfdsfsdf + $this->backupServerArray = $_SERVER; + + parent::setUp(); + } + + public function tearDown() + { + if ($this->_storage) { + $this->_storage->flush(); + } + + parent::tearDown(); + } +}