You are here

abstract class CacheProvider in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php \Doctrine\Common\Cache\CacheProvider

Base class for cache provider implementations.

@since 2.2 @author Benjamin Eberlei <kontakt@beberlei.de> @author Guilherme Blanco <guilhermeblanco@hotmail.com> @author Jonathan Wage <jonwage@gmail.com> @author Roman Borschel <roman@code-factory.org> @author Fabio B. Silva <fabio.bat.silva@gmail.com>

Hierarchy

Expanded class hierarchy of CacheProvider

File

vendor/doctrine/cache/lib/Doctrine/Common/Cache/CacheProvider.php, line 32

Namespace

Doctrine\Common\Cache
View source
abstract class CacheProvider implements Cache, FlushableCache, ClearableCache, MultiGetCache {
  const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';

  /**
   * The namespace to prefix all cache ids with.
   *
   * @var string
   */
  private $namespace = '';

  /**
   * The namespace version.
   *
   * @var integer|null
   */
  private $namespaceVersion;

  /**
   * Sets the namespace to prefix all cache ids with.
   *
   * @param string $namespace
   *
   * @return void
   */
  public function setNamespace($namespace) {
    $this->namespace = (string) $namespace;
    $this->namespaceVersion = null;
  }

  /**
   * Retrieves the namespace that prefixes all cache ids.
   *
   * @return string
   */
  public function getNamespace() {
    return $this->namespace;
  }

  /**
   * {@inheritdoc}
   */
  public function fetch($id) {
    return $this
      ->doFetch($this
      ->getNamespacedId($id));
  }

  /**
   * {@inheritdoc}
   */
  public function fetchMultiple(array $keys) {

    // note: the array_combine() is in place to keep an association between our $keys and the $namespacedKeys
    $namespacedKeys = array_combine($keys, array_map(array(
      $this,
      'getNamespacedId',
    ), $keys));
    $items = $this
      ->doFetchMultiple($namespacedKeys);
    $foundItems = array();

    // no internal array function supports this sort of mapping: needs to be iterative
    // this filters and combines keys in one pass
    foreach ($namespacedKeys as $requestedKey => $namespacedKey) {
      if (isset($items[$namespacedKey])) {
        $foundItems[$requestedKey] = $items[$namespacedKey];
      }
    }
    return $foundItems;
  }

  /**
   * {@inheritdoc}
   */
  public function contains($id) {
    return $this
      ->doContains($this
      ->getNamespacedId($id));
  }

  /**
   * {@inheritdoc}
   */
  public function save($id, $data, $lifeTime = 0) {
    return $this
      ->doSave($this
      ->getNamespacedId($id), $data, $lifeTime);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($id) {
    return $this
      ->doDelete($this
      ->getNamespacedId($id));
  }

  /**
   * {@inheritdoc}
   */
  public function getStats() {
    return $this
      ->doGetStats();
  }

  /**
   * {@inheritDoc}
   */
  public function flushAll() {
    return $this
      ->doFlush();
  }

  /**
   * {@inheritDoc}
   */
  public function deleteAll() {
    $namespaceCacheKey = $this
      ->getNamespaceCacheKey();
    $namespaceVersion = $this
      ->getNamespaceVersion() + 1;
    $this->namespaceVersion = $namespaceVersion;
    return $this
      ->doSave($namespaceCacheKey, $namespaceVersion);
  }

  /**
   * Prefixes the passed id with the configured namespace value.
   *
   * @param string $id The id to namespace.
   *
   * @return string The namespaced id.
   */
  private function getNamespacedId($id) {
    $namespaceVersion = $this
      ->getNamespaceVersion();
    return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
  }

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

  /**
   * Returns the namespace version.
   *
   * @return integer
   */
  private function getNamespaceVersion() {
    if (null !== $this->namespaceVersion) {
      return $this->namespaceVersion;
    }
    $namespaceCacheKey = $this
      ->getNamespaceCacheKey();
    $namespaceVersion = $this
      ->doFetch($namespaceCacheKey);
    if (false === $namespaceVersion) {
      $namespaceVersion = 1;
      $this
        ->doSave($namespaceCacheKey, $namespaceVersion);
    }
    $this->namespaceVersion = $namespaceVersion;
    return $this->namespaceVersion;
  }

  /**
   * Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it.
   *
   * @param array $keys Array of keys to retrieve from cache
   * @return array Array of values retrieved for the given keys.
   */
  protected function doFetchMultiple(array $keys) {
    $returnValues = array();
    foreach ($keys as $index => $key) {
      if (false !== ($item = $this
        ->doFetch($key))) {
        $returnValues[$key] = $item;
      }
    }
    return $returnValues;
  }

  /**
   * Fetches an entry from the cache.
   *
   * @param string $id The id of the cache entry to fetch.
   *
   * @return mixed|boolean The cached data or FALSE, if no cache entry exists for the given id.
   */
  protected abstract function doFetch($id);

  /**
   * Tests if an entry exists in the cache.
   *
   * @param string $id The cache id of the entry to check for.
   *
   * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
   */
  protected abstract function doContains($id);

  /**
   * Puts data into the cache.
   *
   * @param string $id       The cache id.
   * @param string $data     The cache entry/data.
   * @param int    $lifeTime The lifetime. If != 0, sets a specific lifetime for this
   *                           cache entry (0 => infinite lifeTime).
   *
   * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
   */
  protected abstract function doSave($id, $data, $lifeTime = 0);

  /**
   * Deletes a cache entry.
   *
   * @param string $id The cache id.
   *
   * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
   */
  protected abstract function doDelete($id);

  /**
   * Flushes all cache entries.
   *
   * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise.
   */
  protected abstract function doFlush();

  /**
   * Retrieves cached information from the data store.
   *
   * @since 2.2
   *
   * @return array|null An associative array with server's statistics if available, NULL otherwise.
   */
  protected abstract function doGetStats();

}

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 in the current cache namespace. Overrides ClearableCache::deleteAll
CacheProvider::doContains abstract protected function Tests if an entry exists in the cache. 17
CacheProvider::DOCTRINE_NAMESPACE_CACHEKEY constant
CacheProvider::doDelete abstract protected function Deletes a cache entry. 16
CacheProvider::doFetch abstract protected function Fetches an entry from the cache. 17
CacheProvider::doFetchMultiple protected function Default implementation of doFetchMultiple. Each driver that supports multi-get should owerwrite it. 4
CacheProvider::doFlush abstract protected function Flushes all cache entries. 16
CacheProvider::doGetStats abstract protected function Retrieves cached information from the data store. 16
CacheProvider::doSave abstract protected function Puts data into the cache. 17
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, globally. 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
CacheProvider::setNamespace public function Sets the namespace to prefix all cache ids with. 1