You are here

protected function RestfulBase::generateCacheId in RESTful 7

Generate a cache identifier for the request and the current context.

This cache ID may be used by all RestfulDataProviderInterface.

Parameters

array $context: An associative array with additional information to build the cache ID.

Return value

string The cache identifier.

3 calls to RestfulBase::generateCacheId()
RestfulBase::clearRenderedCache in plugins/restful/RestfulBase.php
Clear an entry from the rendered cache.
RestfulBase::getRenderedCache in plugins/restful/RestfulBase.php
Get an entry from the rendered cache.
RestfulBase::setRenderedCache in plugins/restful/RestfulBase.php
Store an entry in the rendered cache.

File

plugins/restful/RestfulBase.php, line 1353
Contains RestfulBase.

Class

RestfulBase
Class \RestfulBase

Code

protected function generateCacheId(array $context = array()) {

  // For performance reasons create the request part and cache it, then add
  // the context part.
  $base_cid = $this->staticCache
    ->get(__CLASS__ . '::' . __FUNCTION__);
  if (!isset($base_cid)) {

    // Get the cache ID from the selected params. We will use a complex cache
    // ID for smarter invalidation. The cache id will be like:
    // v<major version>.<minor version>::uu<user uid>::pa<params array>
    // The code before every bit is a 2 letter representation of the label.
    // For instance, the params array will be something like:
    // fi:id,title::re:admin
    // When the request has ?fields=id,title&restrict=admin
    $version = $this
      ->getVersion();
    $account = $this
      ->getAccount();
    $cache_info = $this
      ->getPluginKey('render_cache');
    if ($cache_info['granularity'] == DRUPAL_CACHE_PER_USER) {
      $account_cid = '::uu' . $account->uid;
    }
    elseif ($cache_info['granularity'] == DRUPAL_CACHE_PER_ROLE) {

      // Instead of encoding the user ID in the cache ID add the role ids.
      $account_cid = '::ur' . implode(',', array_keys($account->roles));
    }
    else {
      throw new \RestfulNotImplementedException(format_string('The selected cache granularity (@granularity) is not supported.', array(
        '@granularity' => $cache_info['granularity'],
      )));
    }
    $base_cid = 'v' . $version['major'] . '.' . $version['minor'] . '::' . $this
      ->getResourceName() . $account_cid . '::pa';
    $this->staticCache
      ->set(__CLASS__ . '::' . __FUNCTION__, $base_cid);
  }

  // Now add the context part to the cid
  $cid_params = static::addCidParams($context);
  if ($this
    ->isReadMethod($this
    ->getMethod())) {

    // We don't want to split the cache with the body data on write requests.
    $request = $this
      ->getRequest();
    static::cleanRequest($request);
    $cid_params = array_merge($cid_params, static::addCidParams($request));
  }
  return $base_cid . implode('::', $cid_params);
}