You are here

class SearchApiViewsCache in Search API 7

Plugin class for caching Search API views.

Hierarchy

Expanded class hierarchy of SearchApiViewsCache

1 string reference to 'SearchApiViewsCache'
search_api_views_views_plugins in contrib/search_api_views/search_api_views.views.inc
Implements hook_views_plugins().

File

contrib/search_api_views/includes/plugin_cache.inc, line 11
Contains the SearchApiViewsCache class.

View source
class SearchApiViewsCache extends views_plugin_cache_time {

  /**
   * Static cache for get_results_key().
   *
   * @var string
   */
  protected $_results_key = NULL;

  /**
   * Static cache for getSearchApiQuery().
   *
   * @var SearchApiQueryInterface
   */
  protected $search_api_query = NULL;

  /**
   * Overrides views_plugin_cache::cache_set().
   *
   * Also stores Search API's internal search results.
   */
  public function cache_set($type) {
    if ($type != 'results') {
      return parent::cache_set($type);
    }
    $cid = $this
      ->get_results_key();
    $results = NULL;
    $query_plugin = $this->view->query;
    if ($query_plugin instanceof SearchApiViewsQuery) {
      $results = $query_plugin
        ->getSearchApiResults();
    }
    $data = array(
      'result' => $this->view->result,
      'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
      'current_page' => $this->view
        ->get_current_page(),
      'search_api results' => $results,
    );
    cache_set($cid, $data, $this->table, $this
      ->cache_set_expire($type));
  }

  /**
   * Overrides views_plugin_cache::cache_get().
   *
   * Additionally stores successfully retrieved results with
   * search_api_current_search().
   */
  public function cache_get($type) {
    if ($type != 'results') {
      return parent::cache_get($type);
    }

    // Values to set: $view->result, $view->total_rows, $view->execute_time,
    // $view->current_page.
    if ($cache = cache_get($this
      ->get_results_key(), $this->table)) {
      $cutoff = $this
        ->cache_expire($type);
      if (!$cutoff || $cache->created > $cutoff) {
        $this->view->result = $cache->data['result'];
        $this->view->total_rows = $cache->data['total_rows'];
        $this->view
          ->set_current_page($cache->data['current_page']);
        $this->view->execute_time = 0;

        // Trick Search API into believing a search happened, to make facetting
        // et al. work.
        $query = $this
          ->getSearchApiQuery();
        search_api_current_search($query
          ->getOption('search id'), $query, $cache->data['search_api results']);
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Overrides views_plugin_cache::get_cache_key().
   *
   * Use the Search API query as the main source for the key. Note that in
   * Views < 3.8, this method does not exist.
   */
  public function get_cache_key($key_data = array()) {
    global $user;
    if (!isset($this->_results_key)) {
      $query = $this
        ->getSearchApiQuery();
      $query
        ->preExecute();
      $key_data += array(
        'query' => $query,
        'roles' => array_keys($user->roles),
        'super-user' => $user->uid == 1,
        // special caching for super user.
        'language' => $GLOBALS['language']->language,
        'base_url' => $GLOBALS['base_url'],
        'offset' => $this->view
          ->get_current_page() . '*' . $this->view
          ->get_items_per_page() . '+' . $this->view
          ->get_offset(),
      );

      // Not sure what gets passed in exposed_info, so better include it. All
      // other parameters used in the parent method are already reflected in the
      // Search API query object we use.
      if (isset($_GET['exposed_info'])) {
        $key_data['exposed_info'] = $_GET['exposed_info'];
      }
    }
    $key = drupal_hash_base64(serialize($key_data));
    return $key;
  }

  /**
   * Overrides views_plugin_cache::get_results_key().
   *
   * This is unnecessary for Views >= 3.8.
   */
  public function get_results_key() {
    if (!isset($this->_results_key)) {
      $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . $this
        ->get_cache_key();
    }
    return $this->_results_key;
  }

  /**
   * Retrieves the Search API query object associated with the current view.
   *
   * @return SearchApiQueryInterface|null
   *   The Search API query object associated with the current view; or NULL if
   *   there is none.
   */
  protected function getSearchApiQuery() {
    if (!isset($this->search_api_query)) {
      $this->search_api_query = FALSE;
      if (isset($this->view->query) && $this->view->query instanceof SearchApiViewsQuery) {
        $this->search_api_query = $this->view->query
          ->getSearchApiQuery();
      }
    }
    return $this->search_api_query ? $this->search_api_query : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiViewsCache::$search_api_query protected property Static cache for getSearchApiQuery().
SearchApiViewsCache::$_results_key protected property Static cache for get_results_key().
SearchApiViewsCache::cache_get public function Overrides views_plugin_cache::cache_get(). Overrides views_plugin_cache_time::cache_get
SearchApiViewsCache::cache_set public function Overrides views_plugin_cache::cache_set(). Overrides views_plugin_cache_time::cache_set
SearchApiViewsCache::getSearchApiQuery protected function Retrieves the Search API query object associated with the current view.
SearchApiViewsCache::get_cache_key public function Overrides views_plugin_cache::get_cache_key(). Overrides views_plugin_cache::get_cache_key
SearchApiViewsCache::get_results_key public function Overrides views_plugin_cache::get_results_key(). Overrides views_plugin_cache::get_results_key
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_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_output_key public function
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.
views_plugin_cache_time::cache_expire public function Determine the expiration time of the cache type, or NULL if no expire. Overrides views_plugin_cache::cache_expire
views_plugin_cache_time::cache_set_expire public function Determine expiration time in the cache table of the cache type. Overrides views_plugin_cache::cache_set_expire
views_plugin_cache_time::get_lifespan public function
views_plugin_cache_time::options_form public function Provide a form to edit options for this plugin. Overrides views_plugin::options_form
views_plugin_cache_time::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_cache_time::option_definition public function Information about options for all kinds of purposes will be held here. Overrides views_object::option_definition
views_plugin_cache_time::summary_title public function Return a string to display as the clickable title for the access control. Overrides views_plugin_cache::summary_title