You are here

render_cache.module in Render cache 7.2

Same filename and directory in other branches
  1. 7 render_cache.module

Main module file for the Render Cache caching system.

File

render_cache.module
View source
<?php

/**
 * @file
 * Main module file for the Render Cache caching system.
 */

// Include Drupal 8 render helper functions.
require_once __DIR__ . '/includes/drupal_render_8.inc';

// -----------------------------------------------------------------------
// Core Hooks

/**
 * Implements hook_flush_caches().
 */
function render_cache_flush_caches() {
  return array(
    'cache_render',
  );
}

// -----------------------------------------------------------------------
// Contrib Hooks

/**
 * Implements hook_ctools_plugin_type().
 */
function render_cache_ctools_plugin_type() {
  $items['Controller'] = array(
    'cache' => FALSE,
  );
  $items['ValidationStrategy'] = array(
    'cache' => FALSE,
  );
  $items['RenderStrategy'] = array(
    'cache' => FALSE,
  );
  return $items;
}

/**
 * Implements hook_ctools_plugin_directory().
 *
 * @param string $owner
 * @param string $plugin_type
 *
 * @return null|string
 */
function render_cache_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'service_container') {
    return 'src/ServiceContainer/' . $plugin_type;
  }
  if ($owner == 'render_cache') {
    return 'src/RenderCache/' . $plugin_type;
  }
  return NULL;
}

// -----------------------------------------------------------------------
// Public API

/**
 * Returns a render cache controller.
 *
 * @param string $type
 *
 * @return \Drupal\render_cache\RenderCache\Controller\ControllerInterface
 */
function render_cache_get_controller($type) {
  return RenderCache::getController($type);
}

/**
 * Returns a render cache validation strategy plugin.
 *
 * @param string $type
 *
 * @return \Drupal\render_cache\RenderCache\ValidationStrategy\ValidationStrategyInterface
 */
function render_cache_get_validator($type) {
  return RenderCache::getValidationStrategy($type);
}

/**
 * Returns a render cache render strategy plugin.
 *
 * @param string $type
 *   The type of the plugin, e.g. "esi_validate", "esi", "ajax2,
 *   "ajax_lstorage", ...
 *
 * @return \Drupal\render_cache\RenderCache\RenderStrategy\RenderStrategyInterface
 */
function render_cache_get_renderer($type) {
  return RenderCache::getRenderStrategy($type);
}

/**
 * Check if this call should / can be served from the cache.
 *
 * By default only GET and / or HEAD requests are cacheable.
 *
 * @param bool $allow_caching
 *   Set to FALSE if you want to prevent this call to get the cached version and
 *   / or fill the cache.
 * @param bool $ignore_request_method_check
 *   Set to TRUE if you want to ignore the request method check.
 *
 * @return bool
 *   TRUE if the current call can be cached, FALSE otherwise.
 *
 * @see drupal_page_is_cacheable()
 */
function render_cache_call_is_cacheable($allow_caching = NULL, $ignore_request_method_check = FALSE) {
  $allow_caching_static =& drupal_static(__FUNCTION__, TRUE);
  if (isset($allow_caching)) {
    $allow_caching_static = $allow_caching;
  }
  return $allow_caching_static && ($ignore_request_method_check || ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')) && !drupal_is_cli();
}

// -----------------------------------------------------------------------
// Helper functions

/**
 * Overrides drupal_render().
 *
 * If we really need to render early, at least collect the cache tags, etc.
 *
 * @param array $render
 *
 * @return string
 */
function render_cache_drupal_render(&$render) {
  return RenderCache::drupalRender($render);
}

/**
 * Returns default values for cache info.
 *
 * @deprecated
 * @todo remove when all modules are converted.
 *
 * @return array
 */
function render_cache_cache_info_defaults() {

  // Setup defaults.
  return array(
    'bin' => 'cache_render',
    'expire' => CACHE_PERMANENT,
    'granularity' => DRUPAL_CACHE_PER_ROLE,
    // Use per role to support contextual and its safer anyway.
    'keys' => array(),
    // Special keys that are only related to our implementation.
    'render_cache_render_to_markup' => FALSE,
  );
}

Functions

Namesort descending Description
render_cache_cache_info_defaults Deprecated Returns default values for cache info.
render_cache_call_is_cacheable Check if this call should / can be served from the cache.
render_cache_ctools_plugin_directory Implements hook_ctools_plugin_directory().
render_cache_ctools_plugin_type Implements hook_ctools_plugin_type().
render_cache_drupal_render Overrides drupal_render().
render_cache_flush_caches Implements hook_flush_caches().
render_cache_get_controller Returns a render cache controller.
render_cache_get_renderer Returns a render cache render strategy plugin.
render_cache_get_validator Returns a render cache validation strategy plugin.