You are here

public function SearchApiCachePluginTrait::cacheGet in Search API 8

File

src/Plugin/views/cache/SearchApiCachePluginTrait.php, line 136

Class

SearchApiCachePluginTrait
Provides a trait to use in Views cache plugins for Search API queries.

Namespace

Drupal\search_api\Plugin\views\cache

Code

public function cacheGet($type) {
  if ($type != 'results') {
    return parent::cacheGet($type);
  }

  // Values to set: $view->result, $view->total_rows, $view->execute_time,
  // $view->current_page.
  if ($cache = $this
    ->getCacheBackend()
    ->get($this
    ->generateResultsKey())) {
    $cutoff = $this
      ->cacheExpire($type);
    if (!$cutoff || $cache->created > $cutoff) {
      $view = $this
        ->getView();
      $view->result = $cache->data['result'];
      $view->total_rows = $cache->data['total_rows'];
      $view
        ->setCurrentPage($cache->data['current_page']);
      $view->execute_time = 0;

      // Trick Search API into believing a search happened, to make faceting
      // et al. work.

      /** @var \Drupal\search_api\Query\ResultSetInterface $results */
      $results = $cache->data['search_api results'];
      $this
        ->getQueryHelper()
        ->addResults($results);
      try {
        $this
          ->getQuery()
          ->setSearchApiQuery($results
          ->getQuery());
      } catch (SearchApiException $e) {

        // Ignore.
      }
      return TRUE;
    }
  }
  return FALSE;
}