You are here

public function ViewsPhp::cacheGet in Views PHP 8

Retrieve data from the cache.

A plugin should override this to provide specialized caching behavior.

Parameters

$type: The cache type, either 'query', 'result'.

Return value

bool TRUE if data has been taken from the cache, otherwise FALSE.

Overrides CachePluginBase::cacheGet

File

src/Plugin/views/cache/ViewsPhp.php, line 79
Contains \Drupal\views_php\Plugin\views\cache\ViewsPhp.

Class

ViewsPhp
Caching of query results for Views displays based on custom PHP code.

Namespace

Drupal\views_php\Plugin\views\cache

Code

public function cacheGet($type) {
  switch ($type) {
    case 'query':

      // Not supported currently, but this is certainly where we'd put it.
      return FALSE;
    case 'results':
      $cache = \Drupal::cache($this->resultsBin)
        ->get($this
        ->generateResultsKey());
      $fresh = !empty($cache);
      if ($fresh && !empty($this->options['php_cache_results'])) {
        $function = create_function('$view, $plugin, $cache', $this->options['php_cache_results'] . ';');
        ob_start();
        $fresh = $function($this->view, $this, $cache);
        ob_end_clean();
      }

      // Values to set: $view->result, $view->total_rows, $view->execute_time,
      // $view->current_page.
      if ($fresh) {
        $this->view->result = $cache->data['result'];

        // Load entities for each result.
        $this->view->query
          ->loadEntities($this->view->result);
        $this->view->total_rows = $cache->data['total_rows'];
        $this->view
          ->setCurrentPage($cache->data['current_page']);
        $this->view->execute_time = 0;
        return TRUE;
      }
      return FALSE;
    case 'output':
      $cache = \Drupal::cache($this->outputBin)
        ->get($this
        ->generateOutputKey());
      $fresh = !empty($cache);
      if ($fresh && !empty($this->options['php_cache_output'])) {
        $function = create_function('$view, $plugin, $cache', $this->options['php_cache_output'] . ';');
        ob_start();
        $fresh = $function($this->view, $this, $cache);
        ob_end_clean();
      }
      if ($fresh) {
        $this->storage = $cache->data;
        $this->view->display_handler->output = $this->storage;
        $this->view->element['#attached'] =& $this->view->display_handler->output['#attached'];
        $this->view->element['#cache']['tags'] =& $this->view->display_handler->output['#cache']['tags'];
        $this->view->element['#post_render_cache'] =& $this->view->display_handler->output['#post_render_cache'];
        return TRUE;
      }
      return FALSE;
  }
}