You are here

class views_custom_cache_plugin_cache in Views custom cache 7

Views caching given view's first argument.

Hierarchy

Expanded class hierarchy of views_custom_cache_plugin_cache

See also

views_plugin_cache()

1 string reference to 'views_custom_cache_plugin_cache'
views_custom_cache_views_plugins in views/views_custom_cache.views.inc
Implements hook_views_plugins().

File

views/views_custom_cache_plugin_cache.inc, line 13
Views custom cache first argument plugin.

View source
class views_custom_cache_plugin_cache extends views_plugin_cache {

  /**
   * Implements views_plugin_cache#summary_title().
   */
  function summary_title() {
    $time = $this
      ->get_time();
    $role = $this->options['per_role'] ? 'Yes' : 'No';
    $entities = isset($this->options['entities']) ? $this->options['entities'] : 'none';
    if ($this->options['cache_time'] == '-1') {
      return t('Never cache');
    }
    elseif ($this->options['cache_time'] == '0') {
      return t('Cache permanent' . ' / ' . t('Per role') . ': ' . $role . ' / ' . t('Updates awareness') . ': ' . $entities);
    }
    else {
      return t('Time') . ': ' . format_interval($time, 1) . ' / ' . t('Per role') . ': ' . $role . ' / ' . t('Updates awareness') . ': ' . $entities;
    }
  }

  /**
   * Implements views_object#option_definition().
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['cache_time'] = array(
      'default' => 3600,
    );
    $options['cache_time_custom'] = array(
      'default' => 0,
    );
    $options['per_role'] = array(
      'default' => 1,
    );
    $options['entities'] = array(
      'default' => 'none',
    );
    $options['bundles'] = array(
      'default' => 'all',
    );
    return $options;
  }

  /**
   * Implements views_object#option_defaults().
   */
  function option_defaults(&$options) {
    $options['cache_time'] = 3600;
    $options['cache_time_custom'] = 0;
    $options['per_role'] = 1;
    $options['entities'] = 'none';
    $options['bundles'] = 'all';
  }

  /**
   * Implements views_plugin#options_form().
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $options = array(
      60,
      300,
      1800,
      3600,
      21600,
      86400,
      518400,
    );
    $options = drupal_map_assoc($options, 'format_interval');
    $options = array(
      -1 => t('Never cache'),
    ) + array(
      0 => t('Permanent cache'),
    ) + $options + array(
      'custom' => t('Custom'),
    );
    $options_per_user = array(
      1 => t('Yes'),
      0 => t('No'),
    );
    $options_entities = array(
      'none' => t('Unaware'),
      'node' => t('Node aware'),
    );
    $options_bundles = $this
      ->get_node_bundles();
    $form['cache_time'] = array(
      '#type' => 'select',
      '#title' => t('Cache duration time.'),
      '#options' => $options,
      '#default_value' => $this->options['cache_time'],
    );
    $form['cache_time_custom'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom time.'),
      '#size' => '25',
      '#maxlength' => '30',
      '#description' => t('Cache duration time in seconds.'),
      '#default_value' => $this->options['cache_time_custom'],
      '#process' => array(
        'ctools_dependent_process',
      ),
      '#dependency' => array(
        'edit-cache-options-cache-time' => array(
          'custom',
        ),
      ),
    );
    $form['per_role'] = array(
      '#type' => 'select',
      '#title' => t('Cache per role?'),
      '#options' => $options_per_user,
      '#default_value' => $this->options['per_role'],
    );
    $form['entities'] = array(
      '#type' => 'select',
      '#title' => t('Updates awareness'),
      '#options' => $options_entities,
      '#default_value' => $this->options['entities'],
    );
    $form['bundles'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Be aware of following bundles'),
      '#options' => $options_bundles,
      '#description' => t('Empty selection means to be aware of all bundles.'),
      '#default_value' => $this->options['bundles'],
      '#dependency' => array(
        'edit-cache-options-entities' => array(
          'node',
        ),
      ),
    );
  }

  /**
   * Validates options form.
   */
  function options_validate(&$form, &$form_state) {
    $custom_fields = array(
      'cache_time',
    );
    foreach ($custom_fields as $field) {
      if ($form_state['values']['cache_options'][$field] == 'custom' && !is_numeric($form_state['values']['cache_options'][$field . '_custom'])) {
        form_error($form[$field . '_custom'], t('Custom time values must be numeric.'));
      }
      if ($form_state['values']['cache_options'][$field] == 'custom' && $form_state['values']['cache_options'][$field . '_custom'] <= 0) {
        $mssg = t('Custom cache time must be greater than zero.');
        form_error($form[$field . '_custom'], $mssg);
      }
    }
  }

  /**
   * Returns cache time defined by user.
   */
  function get_time() {
    $time = $this->options['cache_time'] == 'custom' ? $this->options['cache_time_custom'] : $this->options['cache_time'];
    return $time;
  }

  /**
   * Implements views_plugin_cache#get_results_key().
   */
  function get_results_key() {
    $per_role = isset($this->options['per_role']) ? $this->options['per_role'] : 1;
    $entities = isset($this->options['entities']) ? $this->options['entities'] : 'none';
    $bundles = isset($this->options['bundles']) ? $this->options['bundles'] : array();
    if (!isset($this->_results_key)) {
      $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . $this
        ->get_first_arg();
      if ($per_role) {
        $this->_results_key .= ':' . $this
          ->get_cache_key();
      }
      if (!empty($entities) && $entities == 'node') {
        $last_changed = $this
          ->node_get_last_changed_date($bundles);
        $this->_results_key .= ':' . $last_changed;
      }
    }
    return $this->_results_key;
  }

  /**
   * Implements views_plugin_cache#get_output_key().
   */
  function get_output_key() {
    $per_role = isset($this->options['per_role']) ? $this->options['per_role'] : 1;
    $entities = isset($this->options['entities']) ? $this->options['entities'] : 'none';
    $bundles = isset($this->options['bundles']) ? $this->options['bundles'] : array();
    if (!isset($this->_output_key)) {
      $key_data = array(
        'theme' => $GLOBALS['theme'],
      );
      $this->_output_key = $this->view->name . ':' . $this->display->id . ':output:' . $this
        ->get_first_arg();
      if ($per_role) {
        $this->_output_key .= ':' . $this
          ->get_cache_key($key_data);
      }
      if (!empty($entities) && $entities == 'node') {
        $last_changed = $this
          ->node_get_last_changed_date($bundles);
        $this->_output_key .= ':' . $last_changed;
      }
    }
    return $this->_output_key;
  }

  /**
   * Implements views_plugin_cache#cache_expire().
   *
   * Determine the expiration time of the cache type, or NULL if no expire.
   *
   * @param string $type
   *   The cache type, either 'query', 'result' or 'output'.
   */
  function cache_expire($type) {
    $expiration_time = $this
      ->get_time();
    if ($expiration_time) {
      $cutoff = REQUEST_TIME - $expiration_time;
      return $cutoff;
    }
    else {
      return NULL;
    }
  }

  /**
   * Implements views_plugin_cache#cache_set_expire().
   *
   * Determine expiration time in the cache table of the cache type
   * or CACHE_PERMANENT if item shouldn't be removed automatically from cache.
   *
   * @param string $type
   *   The cache type, either 'query', 'result' or 'output'.
   */
  function cache_set_expire($type) {
    $expiration_time = $this
      ->get_time();
    if ($expiration_time) {
      return REQUEST_TIME + $expiration_time;
    }
    else {
      return CACHE_PERMANENT;
    }
  }

  /**
   * Custom function which takes the first argument of view.
   */
  function get_first_arg() {
    global $language;
    $key = '';
    if (!empty($_GET['page'])) {
      $key .= $_GET['page'];
    }
    elseif (!empty($_POST['page'])) {
      $key .= $_POST['page'];
    }
    if (!empty($_GET['f'])) {

      // search api facets.
      $key .= serialize($_GET['f']);
    }
    if (!empty($this->view->args)) {
      $values = array_values($this->view->args);
      $key .= $values[0];
    }
    if (!empty($this->view->exposed_raw_input)) {
      $values = array_values($this->view->exposed_raw_input);
      $key .= $values[0];
    }
    if (empty($key)) {
      $key = 'none';
    }
    else {
      $key = md5($key);
    }
    $key .= ':' . $language->language;
    return $key;
  }

  /**
   * Returns the change date of most recent node.
   *
   * @param array $bundles
   *   Bundles to check out.
   *
   * @return int
   *   Change date in unixtime.
   */
  function node_get_last_changed_date($bundles) {
    $query = db_select('node', 'n');
    if (empty($bundles)) {
      $result = $query
        ->fields('n', array(
        'changed',
      ))
        ->orderBy('n.changed', 'DESC')
        ->range(0, 1)
        ->execute()
        ->fetchAssoc();
    }
    else {
      $result = $query
        ->fields('n', array(
        'changed',
      ))
        ->orderBy('n.changed', 'DESC')
        ->condition('n.type', array_keys($bundles), 'IN')
        ->range(0, 1)
        ->execute()
        ->fetchAssoc();
    }
    return $result['changed'];
  }

  /**
   * Returns the list of available node bundles.
   *
   * @return array
   *   List of bundles.
   */
  function get_node_bundles() {
    $bundles = array();
    $ctypes = node_type_get_types();
    foreach ($ctypes as $key => $ct) {
      $bundles[$key] = $ct->name;
    }
    return $bundles;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_custom_cache_plugin_cache::cache_expire function Implements views_plugin_cache#cache_expire(). Overrides views_plugin_cache::cache_expire
views_custom_cache_plugin_cache::cache_set_expire function Implements views_plugin_cache#cache_set_expire(). Overrides views_plugin_cache::cache_set_expire
views_custom_cache_plugin_cache::get_first_arg function Custom function which takes the first argument of view.
views_custom_cache_plugin_cache::get_node_bundles function Returns the list of available node bundles.
views_custom_cache_plugin_cache::get_output_key function Implements views_plugin_cache#get_output_key(). Overrides views_plugin_cache::get_output_key
views_custom_cache_plugin_cache::get_results_key function Implements views_plugin_cache#get_results_key(). Overrides views_plugin_cache::get_results_key
views_custom_cache_plugin_cache::get_time function Returns cache time defined by user.
views_custom_cache_plugin_cache::node_get_last_changed_date function Returns the change date of most recent node.
views_custom_cache_plugin_cache::options_form function Implements views_plugin#options_form(). Overrides views_plugin::options_form
views_custom_cache_plugin_cache::options_validate function Validates options form. Overrides views_plugin::options_validate
views_custom_cache_plugin_cache::option_defaults function Implements views_object#option_defaults().
views_custom_cache_plugin_cache::option_definition function Implements views_object#option_definition(). Overrides views_object::option_definition
views_custom_cache_plugin_cache::summary_title function Implements views_plugin_cache#summary_title(). Overrides views_plugin_cache::summary_title
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::query public function Add anything to the query that we might need to. 7
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_cache::$storage public property Contains all data that should be written/read from cache.
views_plugin_cache::$table public property What table to store data in.
views_plugin_cache::assetDiff protected function Computes the differences between two JS/CSS asset arrays.
views_plugin_cache::cache_flush public function Clear out cached data for a view.
views_plugin_cache::cache_get public function Retrieve data from the cache. 2
views_plugin_cache::cache_set public function Save data to the cache. 2
views_plugin_cache::cache_start public function Start caching JavaScript, css and other out of band info. 1
views_plugin_cache::gather_headers public function Gather out of band data, compare it to the start data and store the diff.
views_plugin_cache::get_cache_key public function Returns cache key.
views_plugin_cache::init public function Initialize the plugin.
views_plugin_cache::post_render public function Post process any rendered data.
views_plugin_cache::restore_headers public function Restore out of band data saved to cache. Copied from Panels.