diff --git a/library/Zend/Cache/Storage/Adapter/AbstractZendServer.php b/library/Zend/Cache/Storage/Adapter/AbstractZendServer.php index e81c10b03f7..3c48de290ac 100644 --- a/library/Zend/Cache/Storage/Adapter/AbstractZendServer.php +++ b/library/Zend/Cache/Storage/Adapter/AbstractZendServer.php @@ -41,10 +41,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 +65,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 +95,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 +109,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 +142,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 +176,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 +192,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/library/Zend/Cache/Storage/Adapter/Apc.php b/library/Zend/Cache/Storage/Adapter/Apc.php index 6b8f29500c6..870f880df9d 100644 --- a/library/Zend/Cache/Storage/Adapter/Apc.php +++ b/library/Zend/Cache/Storage/Adapter/Apc.php @@ -109,7 +109,7 @@ public function getOptions() */ public function getTotalSpace() { - if ($this->totalSpace !== null) { + if ($this->totalSpace === null) { $smaInfo = apc_sma_info(true); $this->totalSpace = $smaInfo['num_seg'] * $smaInfo['seg_size']; } @@ -139,9 +139,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 +174,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 +195,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 +221,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 +243,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 +276,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 +291,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 +326,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 +365,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 +403,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 +427,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 +463,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 +491,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 +527,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 +555,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 +570,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 +604,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 +636,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/library/Zend/Cache/Storage/Adapter/Dba.php b/library/Zend/Cache/Storage/Adapter/Dba.php index 64299dd9dae..364a0a0f2be 100644 --- a/library/Zend/Cache/Storage/Adapter/Dba.php +++ b/library/Zend/Cache/Storage/Adapter/Dba.php @@ -216,6 +216,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 +252,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 +269,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 +290,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 +327,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 +353,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 +374,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 +397,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 +430,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/library/Zend/Cache/Storage/Adapter/Filesystem.php b/library/Zend/Cache/Storage/Adapter/Filesystem.php index 41f69c64841..7c50c79f525 100644 --- a/library/Zend/Cache/Storage/Adapter/Filesystem.php +++ b/library/Zend/Cache/Storage/Adapter/Filesystem.php @@ -140,8 +140,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 +184,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 +204,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 +221,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 +242,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 +314,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 +357,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 +376,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; } @@ -1234,10 +1248,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/library/Zend/Cache/Storage/Adapter/Memory.php b/library/Zend/Cache/Storage/Adapter/Memory.php index 25251623d27..82c9faa3180 100644 --- a/library/Zend/Cache/Storage/Adapter/Memory.php +++ b/library/Zend/Cache/Storage/Adapter/Memory.php @@ -14,6 +14,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; @@ -29,6 +30,7 @@ class Memory extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByPrefixInterface, + ClearByNamespaceInterface, ClearExpiredInterface, FlushableInterface, IterableInterface, @@ -174,6 +176,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 +199,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/library/Zend/Cache/Storage/Adapter/Session.php b/library/Zend/Cache/Storage/Adapter/Session.php index eebb34810f7..fc0d2487f0b 100644 --- a/library/Zend/Cache/Storage/Adapter/Session.php +++ b/library/Zend/Cache/Storage/Adapter/Session.php @@ -117,6 +117,11 @@ public function flush() */ public function clearByPrefix($prefix) { + $prefix = (string) $prefix; + if ($prefix === '') { + throw new Exception\InvalidArgumentException('No prefix given'); + } + $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); diff --git a/library/Zend/Cache/Storage/Adapter/WinCache.php b/library/Zend/Cache/Storage/Adapter/WinCache.php index c81177d20ca..f3708b6bb8c 100644 --- a/library/Zend/Cache/Storage/Adapter/WinCache.php +++ b/library/Zend/Cache/Storage/Adapter/WinCache.php @@ -139,7 +139,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 +160,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 +193,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 +209,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 +236,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 +260,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 +295,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 +319,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 +335,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 +354,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 +382,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 +397,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 +435,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 +452,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/library/Zend/Cache/Storage/Adapter/ZendServerDisk.php b/library/Zend/Cache/Storage/Adapter/ZendServerDisk.php index b33aef951b3..7ff9942266f 100644 --- a/library/Zend/Cache/Storage/Adapter/ZendServerDisk.php +++ b/library/Zend/Cache/Storage/Adapter/ZendServerDisk.php @@ -75,6 +75,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 +93,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 +102,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 +118,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/library/Zend/Cache/Storage/Adapter/ZendServerShm.php b/library/Zend/Cache/Storage/Adapter/ZendServerShm.php index 1c9633d738d..13e6169e34c 100644 --- a/library/Zend/Cache/Storage/Adapter/ZendServerShm.php +++ b/library/Zend/Cache/Storage/Adapter/ZendServerShm.php @@ -65,6 +65,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/tests/ZendTest/Cache/Storage/Adapter/CommonAdapterTest.php b/tests/ZendTest/Cache/Storage/Adapter/CommonAdapterTest.php index ace231e4384..0e46955f53f 100644 --- a/tests/ZendTest/Cache/Storage/Adapter/CommonAdapterTest.php +++ b/tests/ZendTest/Cache/Storage/Adapter/CommonAdapterTest.php @@ -402,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')); @@ -413,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', @@ -937,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)) { @@ -971,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)) { @@ -1049,7 +1073,7 @@ public function testGetTotalSpace() } $totalSpace = $this->_storage->getTotalSpace(); - $this->assertGreaterThan(0, $totalSpace); + $this->assertGreaterThanOrEqual(0, $totalSpace); if ($this->_storage instanceof AvailableSpaceCapableInterface) { $availableSpace = $this->_storage->getAvailableSpace();