You are here

public function RenderCacheBackendAdapter::set in Render cache 7.2

Sets one cache entry based on the given $cache_info structure.

Because cache_info supports different caching strategies, this function needs to be able to change the given render array.

Parameters

array &$render: The render array to set the cache for.

array $cache_info: The cache info structure.

Overrides RenderCacheBackendAdapterInterface::set

1 call to RenderCacheBackendAdapter::set()
RenderCacheBackendAdapter::setMultiple in src/Cache/RenderCacheBackendAdapter.php
This sets multiple cache entries based on the cache info map.

File

src/Cache/RenderCacheBackendAdapter.php, line 110
Contains \Drupal\render_cache\Cache\RenderCacheBackendAdapter

Class

RenderCacheBackendAdapter
Defines the render_cache.cache service.

Namespace

Drupal\render_cache\Cache

Code

public function set(array &$render, array $cache_info) {
  $cid = $this
    ->getCacheId($cache_info);
  $bin = 'cache';
  if (isset($cache_info['bin'])) {
    $bin = $cache_info['bin'];
  }
  $expire = RenderCache::CACHE_PERMANENT;
  if (isset($cache_info['expire'])) {
    $expire = $cache_info['expire'];
  }
  $cache_strategy = $cache_info['render_cache_cache_strategy'];

  // Preserve some properties.
  $properties = $this
    ->preserveProperties($render, $cache_info);
  if (!empty($cache_info['render_cache_preserve_original'])) {
    $properties['#render_cache_original'] = $render;
  }

  // Need to first render to markup, else we would need to collect and remove
  // assets twice. This saves a lot performance.
  if ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER) {

    // This internally inc / dec recursion and attaches new out-of-bound assets.
    list($markup, $original) = $this->renderStack
      ->render($render);
    if (!empty($cache_info['render_cache_preserve_original'])) {
      $properties['#render_cache_original'] = $original;
    }
  }

  // This normalizes that all #cache, etc. properties are in the top
  // render element.
  $full_render = array();

  // Ensure that cache_info is processed first.
  $full_render['#cache'] = $cache_info;
  $full_render['render'] =& $render;
  $assets = $this->renderStack
    ->collectAndRemoveAssets($full_render);
  $render = NestedArray::mergeDeep($render, $assets);
  $data = $this->renderStack
    ->convertRenderArrayToD7($render);
  $data['#attached']['render_cache'] += $properties;
  if ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_NO_RENDER) {
    if ($cid) {
      $this
        ->cache($bin)
        ->set($cid, $data, $expire);
    }
  }
  elseif ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER) {
    $attached = $this->renderStack
      ->collectAttached($data);
    $data = array();
    $data['#markup'] =& $markup;
    $data['#attached'] = $attached;
    if ($cid) {
      $this
        ->cache($bin)
        ->set($cid, $data, $expire);
    }
    $render = $this->renderStack
      ->convertRenderArrayFromD7($data);
  }
  elseif ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_LATE_RENDER) {

    // This cache id was invalidated via cache clear if it was not valid
    // before. This prevents drupal_render_cache_get() from getting an item
    // from the cache.
    if ($cid) {
      $render['#cache']['cid'] = $cid;
    }
    else {
      unset($render['#cache']['cid']);
      unset($render['#cache']['keys']);
    }
    $render['#attached']['render_cache'] = $data['#attached']['render_cache'];
  }
  else {

    // This is actually covered, but not seen by xdebug.
    // @codeCoverageIgnoreStart
    throw new \RunTimeException('Unknown caching strategy passed.');

    // @codeCoverageIgnoreEnd
  }
}