You are here

class RenderCache in RESTful 7.2

Class RenderCache.

@package Drupal\restful\RenderCache

Hierarchy

Expanded class hierarchy of RenderCache

8 files declare their use of RenderCache
CacheDecoratedDataProvider.php in src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php
Contains \Drupal\restful\Plugin\resource\DataProvider\CacheDecoratedDataProvider.
CacheDecoratedResource.php in src/Plugin/resource/Decorators/CacheDecoratedResource.php
Contains \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource
Formatter.php in src/Plugin/formatter/Formatter.php
Contains \Drupal\restful\Plugin\formatter\Formatter
restful.cache.inc in ./restful.cache.inc
Procedural implementations for cache related features.
restful.entity.inc in ./restful.entity.inc
Contains entity related code.

... See full list

File

src/RenderCache/RenderCache.php, line 18
Contains \Drupal\restful\RenderCache\RenderCache.

Namespace

Drupal\restful\RenderCache
View source
class RenderCache implements RenderCacheInterface {

  /**
   * Stores the default cache bin.
   *
   * @var string
   */
  const CACHE_BIN = 'cache_restful';

  /**
   * The hash for the cache id.
   */
  protected $hash;

  /**
   * The cache fragments.
   *
   * @var ArrayCollection
   */
  protected $cacheFragments;

  /**
   * The cache object.
   *
   * @var \DrupalCacheInterface
   */
  protected $cacheObject;

  /**
   * Create an object of type RenderCache.
   *
   * @param ArrayCollection $cache_fragments
   *   The cache fragments.
   * @param string $hash
   *   The hash for the cache object.
   * @param \DrupalCacheInterface $cache_object
   *   The cache backend to use with this object.
   */
  public function __construct(ArrayCollection $cache_fragments, $hash, \DrupalCacheInterface $cache_object) {
    $this->cacheFragments = $cache_fragments;

    /* @var CacheFragmentController $controller */
    $controller = entity_get_controller('cache_fragment');
    $this->hash = $hash ?: $controller
      ->generateCacheHash($cache_fragments);
    $this->cacheObject = $cache_object;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ArrayCollection $cache_fragments, \DrupalCacheInterface $cache_object) {

    /* @var CacheFragmentController $controller */
    $controller = entity_get_controller('cache_fragment');
    return new static($cache_fragments, $controller
      ->generateCacheHash($cache_fragments), $cache_object);
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    $cid = $this
      ->generateCacheId();
    $query = new \EntityFieldQuery();
    $count = $query
      ->entityCondition('entity_type', 'cache_fragment')
      ->propertyCondition('hash', $cid)
      ->count()
      ->execute();
    if ($count) {
      return $this->cacheObject
        ->get($cid);
    }

    // If there are no cache fragments for the given hash then clear the cache
    // and return NULL.
    $this->cacheObject
      ->clear($cid);
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function set($value) {

    /* @var CacheFragmentController $controller */
    $controller = entity_get_controller('cache_fragment');
    if (!$controller
      ->createCacheFragments($this->cacheFragments)) {
      return;
    }
    $this->cacheObject
      ->set($this
      ->generateCacheId(), $value);
  }

  /**
   * {@inheritdoc}
   */
  public function clear() {

    // Remove the cache.
    $this->cacheObject
      ->clear($this
      ->generateCacheId());

    // Delete all cache fragments for that hash.
    $query = new \EntityFieldQuery();
    $results = $query
      ->entityCondition('entity_type', 'cache_fragment')
      ->propertyCondition('hash', $this
      ->generateCacheId())
      ->execute();
    if (empty($results['cache_fragment'])) {
      return;
    }

    // Delete the actual entities.
    entity_delete_multiple('cache_fragment', array_keys($results['cache_fragment']));
  }

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

  /**
   * Generates the cache id based on the hash and the fragment IDs.
   *
   * @return string
   *   The cid.
   */
  protected function generateCacheId() {
    return $this->hash;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RenderCache::$cacheFragments protected property The cache fragments.
RenderCache::$cacheObject protected property The cache object.
RenderCache::$hash protected property The hash for the cache id.
RenderCache::CACHE_BIN constant Stores the default cache bin.
RenderCache::clear public function Clears the cache for the given cache object. Overrides RenderCacheInterface::clear
RenderCache::create public static function Factory function to create a new RenderCacheInterface object. Overrides RenderCacheInterface::create
RenderCache::generateCacheId protected function Generates the cache id based on the hash and the fragment IDs.
RenderCache::get public function Get the cache. Overrides RenderCacheInterface::get
RenderCache::getCid public function Get the cache ID (aka cache hash). Overrides RenderCacheInterface::getCid
RenderCache::set public function Set the cache. Overrides RenderCacheInterface::set
RenderCache::__construct public function Create an object of type RenderCache.