You are here

public function CachePluginBase::cacheGet in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/cache/CachePluginBase.php \Drupal\views\Plugin\views\cache\CachePluginBase::cacheGet()

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.

1 method overrides CachePluginBase::cacheGet()
None::cacheGet in core/modules/views/src/Plugin/views/cache/None.php
Overrides \Drupal\views\Plugin\views\cache\CachePluginBase::cacheGet().

File

core/modules/views/src/Plugin/views/cache/CachePluginBase.php, line 133

Class

CachePluginBase
The base plugin to handle caching.

Namespace

Drupal\views\Plugin\views\cache

Code

public function cacheGet($type) {
  $cutoff = $this
    ->cacheExpire($type);
  switch ($type) {
    case 'query':

      // Not supported currently, but this is certainly where we'd put it.
      return FALSE;
    case 'results':

      // Values to set: $view->result, $view->total_rows, $view->execute_time,
      // $view->current_page.
      if ($cache = \Drupal::cache($this->resultsBin)
        ->get($this
        ->generateResultsKey())) {
        if (!$cutoff || $cache->created > $cutoff) {
          $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'], TRUE);
          $this->view->execute_time = 0;
          return TRUE;
        }
      }
      return FALSE;
  }
}