You are here

class ChainCache in Plug 7

Cache provider that allows to easily chain multiple cache providers

@author Michaël Gallego <mic.gallego@gmail.com>

Hierarchy

Expanded class hierarchy of ChainCache

1 file declares its use of ChainCache
ChainCacheTest.php in lib/doctrine/cache/tests/Doctrine/Tests/Common/Cache/ChainCacheTest.php

File

lib/doctrine/cache/lib/Doctrine/Common/Cache/ChainCache.php, line 27

Namespace

Doctrine\Common\Cache
View source
class ChainCache extends CacheProvider {

  /**
   * @var CacheProvider[]
   */
  private $cacheProviders = array();

  /**
   * Constructor
   *
   * @param CacheProvider[] $cacheProviders
   */
  public function __construct($cacheProviders = array()) {
    $this->cacheProviders = $cacheProviders;
  }

  /**
   * {@inheritDoc}
   */
  public function setNamespace($namespace) {
    parent::setNamespace($namespace);
    foreach ($this->cacheProviders as $cacheProvider) {
      $cacheProvider
        ->setNamespace($namespace);
    }
  }

  /**
   * {@inheritDoc}
   */
  protected function doFetch($id) {
    foreach ($this->cacheProviders as $key => $cacheProvider) {
      if ($cacheProvider
        ->doContains($id)) {
        $value = $cacheProvider
          ->doFetch($id);

        // We populate all the previous cache layers (that are assumed to be faster)
        for ($subKey = $key - 1; $subKey >= 0; $subKey--) {
          $this->cacheProviders[$subKey]
            ->doSave($id, $value);
        }
        return $value;
      }
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  protected function doContains($id) {
    foreach ($this->cacheProviders as $cacheProvider) {
      if ($cacheProvider
        ->doContains($id)) {
        return true;
      }
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  protected function doSave($id, $data, $lifeTime = 0) {
    $stored = true;
    foreach ($this->cacheProviders as $cacheProvider) {
      $stored = $cacheProvider
        ->doSave($id, $data, $lifeTime) && $stored;
    }
    return $stored;
  }

  /**
   * {@inheritDoc}
   */
  protected function doDelete($id) {
    $deleted = true;
    foreach ($this->cacheProviders as $cacheProvider) {
      $deleted = $cacheProvider
        ->doDelete($id) && $deleted;
    }
    return $deleted;
  }

  /**
   * {@inheritDoc}
   */
  protected function doFlush() {
    $flushed = true;
    foreach ($this->cacheProviders as $cacheProvider) {
      $flushed = $cacheProvider
        ->doFlush() && $flushed;
    }
    return $flushed;
  }

  /**
   * {@inheritDoc}
   */
  protected function doGetStats() {

    // We return all the stats from all adapters
    $stats = array();
    foreach ($this->cacheProviders as $cacheProvider) {
      $stats[] = $cacheProvider
        ->doGetStats();
    }
    return $stats;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Cache::STATS_HITS constant
Cache::STATS_MEMORY_AVAILABLE constant
Cache::STATS_MEMORY_AVAILIABLE constant Only for backward compatibility (may be removed in next major release)
Cache::STATS_MEMORY_USAGE constant
Cache::STATS_MISSES constant
Cache::STATS_UPTIME constant
CacheProvider::$namespace private property The namespace to prefix all cache ids with.
CacheProvider::$namespaceVersion private property The namespace version.
CacheProvider::contains public function Tests if an entry exists in the cache. Overrides Cache::contains
CacheProvider::delete public function Deletes a cache entry. Overrides Cache::delete
CacheProvider::deleteAll public function Deletes all cache entries. Overrides ClearableCache::deleteAll
CacheProvider::DOCTRINE_NAMESPACE_CACHEKEY constant
CacheProvider::doFetchMultiple protected function Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. 4
CacheProvider::fetch public function Fetches an entry from the cache. Overrides Cache::fetch
CacheProvider::fetchMultiple public function Returns an associative array of values for keys is found in cache. Overrides MultiGetCache::fetchMultiple
CacheProvider::flushAll public function Flushes all cache entries. Overrides FlushableCache::flushAll
CacheProvider::getNamespace public function Retrieves the namespace that prefixes all cache ids.
CacheProvider::getNamespaceCacheKey private function Returns the namespace cache key.
CacheProvider::getNamespacedId private function Prefixes the passed id with the configured namespace value.
CacheProvider::getNamespaceVersion private function Returns the namespace version.
CacheProvider::getStats public function Retrieves cached information from the data store. Overrides Cache::getStats
CacheProvider::save public function Puts data into the cache. Overrides Cache::save
ChainCache::$cacheProviders private property
ChainCache::doContains protected function Tests if an entry exists in the cache. Overrides CacheProvider::doContains
ChainCache::doDelete protected function Deletes a cache entry. Overrides CacheProvider::doDelete
ChainCache::doFetch protected function Fetches an entry from the cache. Overrides CacheProvider::doFetch
ChainCache::doFlush protected function Flushes all cache entries. Overrides CacheProvider::doFlush
ChainCache::doGetStats protected function Retrieves cached information from the data store. Overrides CacheProvider::doGetStats
ChainCache::doSave protected function Puts data into the cache. Overrides CacheProvider::doSave
ChainCache::setNamespace public function Sets the namespace to prefix all cache ids with. Overrides CacheProvider::setNamespace
ChainCache::__construct public function Constructor