Skip to content

Commit

Permalink
CacheProvider: memoize namespace version value.
Browse files Browse the repository at this point in the history
Prior implementation for CacheProvider was seriously flawed
from performance point of view as each fetch() call results
in 2 additional cache storage calls due to $this->getNamespaceId(),
which always checks for $namespaceCacheKey (and fetches it if
exists, which will be the case for 2nd and next calls).
  • Loading branch information
sobstel committed Mar 28, 2012
1 parent 7ec510d commit 7bad040
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions lib/Doctrine/Common/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ abstract class CacheProvider implements Cache
*/
private $namespace = '';

/**
* @var string The namespace version
*/
private $namespaceVersion = NULL;

/**
* Set the namespace to prefix all cache ids with.
*
Expand Down Expand Up @@ -117,10 +122,11 @@ public function flushAll()
*/
public function deleteAll()
{
$namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
$namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1;
$namespaceCacheKey = $this->getNamespaceCacheKey();
$namespaceVersion = $this->getNamespaceVersion() + 1;
$this->namespaceVersion = $namespaceVersion;

return $this->doSave($namespaceCacheKey, $namespaceVersion + 1);
return $this->doSave($namespaceCacheKey, $namespaceVersion);
}

/**
Expand All @@ -131,12 +137,37 @@ public function deleteAll()
*/
private function getNamespacedId($id)
{
$namespaceCacheKey = sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
$namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1;
$namespaceVersion = $this->getNamespaceVersion();

return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
}

/**
* Namespace cache key
*
* @return string $namespaceCacheKey
*/
private function getNamespaceCacheKey()
{
return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
}

/**
* Namespace version
*
* @return string $namespaceVersion
*/
private function getNamespaceVersion()
{
if (NULL === $this->namespaceVersion)
{
$namespaceCacheKey = $this->getNamespaceCacheKey();
$this->namespaceVersion = ($this->doContains($namespaceCacheKey)) ? $this->doFetch($namespaceCacheKey) : 1;
}

return $this->namespaceVersion;
}

/**
* Fetches an entry from the cache.
*
Expand Down

0 comments on commit 7bad040

Please sign in to comment.