Skip to content

Commit

Permalink
Merge branch 'hotfix/cache-empty-namespace' of git://github.com/marc-…
Browse files Browse the repository at this point in the history
…mabe/zf2 into feature/cache-empty-namespace
  • Loading branch information
weierophinney committed Dec 13, 2012
2 parents d00606f + fc58f83 commit 8da51d2
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 131 deletions.
59 changes: 37 additions & 22 deletions library/Zend/Cache/Storage/Adapter/AbstractZendServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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();
}
Expand All @@ -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;
}

Expand All @@ -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 */
Expand Down
123 changes: 86 additions & 37 deletions library/Zend/Cache/Storage/Adapter/Apc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 8da51d2

Please sign in to comment.