You are here

public function D8Cache::set in Drupal 8 Cache Backport 7

1 call to D8Cache::set()
D8CacheAttachmentsCollector::set in ./d8cache-ac.cache.inc
1 method overrides D8Cache::set()
D8CacheAttachmentsCollector::set in ./d8cache-ac.cache.inc

File

./d8cache.cache.inc, line 123

Class

D8Cache
Defines a Drupal 8 cacheable metadata aware cache backend.

Code

public function set($cid, $data, $expire = CACHE_PERMANENT, $tags = array()) {

  // Allow to override the TTL for a whole bin.
  if (isset($this->configuration['ttl'])) {
    if ($this->configuration['ttl'] == CACHE_MAX_AGE_PERMANENT) {

      // Convert the TTL magic value to the equivilent expire magic value.
      $expire = CACHE_PERMANENT;
    }
    elseif ($this->configuration['ttl'] == CACHE_PERMANENT || $this->configuration['ttl'] == CACHE_TEMPORARY) {

      // Also accept passing the expire magic values directly through.
      $expire = $this->configuration['ttl'];
    }
    else {

      // Convert the TTL to an expiration timestamp.
      $expire = REQUEST_TIME + $this->configuration['ttl'];
    }
  }

  // Allow to set global tags per bin.
  if (isset($this->configuration['tags'])) {
    $tags = drupal_merge_cache_tags($tags, $this->configuration['tags']);
  }

  // Special case cache_page.
  if ($this->bin == 'cache_page') {
    $page_cache_tags =& drupal_static('d8cache_emit_cache_tags', array());
    $page_cache_max_age =& drupal_static('d8cache_emit_cache_max_age', CACHE_MAX_AGE_PERMANENT);
    if (!empty($page_cache_tags)) {
      $tags = drupal_merge_cache_tags($tags, $page_cache_tags);
    }
    $expire = $this
      ->mergeExpireWithMaxAge($expire, $page_cache_max_age);
  }

  // Render arrays.
  if (is_array($data) && isset($data['#attached']) && (isset($data['#attached']['drupal_add_cache_tags']) || isset($data['#attached']['drupal_set_cache_max_age']))) {
    $cacheable_metadata = drupal_get_cacheable_metadata_from_render_array($data);
    if (!empty($cacheable_metadata['tags'])) {
      $tags = drupal_merge_cache_tags($tags, $cacheable_metadata['tags']);

      // Collapse the attached tags into a single attachment before saving.
      $data['#attached']['drupal_add_cache_tags'] = array(
        array(
          0 => $cacheable_metadata['tags'],
        ),
      );
    }
    $expire = $this
      ->mergeExpireWithMaxAge($expire, $cacheable_metadata['max-age']);
    if ($cacheable_metadata['max-age'] != CACHE_MAX_AGE_PERMANENT) {

      // Collapse the attached max-age into a single attachment before saving.
      $data['#attached']['drupal_set_cache_max_age'] = array(
        array(
          0 => $cacheable_metadata['max-age'],
        ),
      );
    }
  }

  // No tags, present, just continue normally.
  if (empty($tags)) {
    $this->backend
      ->set($cid, $data, $expire);
    return;
  }

  // Does the backend support tags natively?
  if ($this->backend instanceof TaggableDrupalCacheInterface) {
    $this->backend
      ->set($cid, $data, $expire, $tags);
  }
  else {
    $checksum = $this
      ->getCurrentChecksum($tags);
    $data = (object) array(
      'd8cache_tags' => $tags,
      'd8cache_checksum' => $checksum,
      'd8cache_expire' => $expire,
      'd8cache_data' => $data,
    );
    $this->backend
      ->set($cid, $data, $expire);
  }
}