SearchApiCachePluginTrait.php in Search API 8
File
src/Plugin/views/cache/SearchApiCachePluginTrait.php
View source
<?php
namespace Drupal\search_api\Plugin\views\cache;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\Utility\QueryHelperInterface;
trait SearchApiCachePluginTrait {
protected $cacheBackend;
protected $cacheContextsManager;
protected $queryHelper;
public function getCacheBackend() {
return $this->cacheBackend ?: \Drupal::cache($this->resultsBin);
}
public function setCacheBackend(CacheBackendInterface $cache_backend) {
$this->cacheBackend = $cache_backend;
return $this;
}
public function getCacheContextsManager() {
return $this->cacheContextsManager ?: \Drupal::service('cache_contexts_manager');
}
public function setCacheContextsManager(CacheContextsManager $cache_contexts_manager) {
$this->cacheContextsManager = $cache_contexts_manager;
return $this;
}
public function getQueryHelper() {
return $this->queryHelper ?: \Drupal::service('search_api.query_helper');
}
public function setQueryHelper(QueryHelperInterface $query_helper) {
$this->queryHelper = $query_helper;
return $this;
}
public function cacheSet($type) {
if ($type != 'results') {
parent::cacheSet($type);
return;
}
$view = $this
->getView();
$data = [
'result' => $view->result,
'total_rows' => $view->total_rows ?? 0,
'current_page' => $view
->getCurrentPage(),
'search_api results' => $this
->getQuery()
->getSearchApiResults(),
];
$expire = $this
->cacheSetMaxAge($type);
if ($expire !== Cache::PERMANENT) {
$expire += (int) $view
->getRequest()->server
->get('REQUEST_TIME');
}
$this
->getCacheBackend()
->set($this
->generateResultsKey(), $data, $expire, $this
->getCacheTags());
}
public function cacheGet($type) {
if ($type != 'results') {
return parent::cacheGet($type);
}
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;
$results = $cache->data['search_api results'];
$this
->getQueryHelper()
->addResults($results);
try {
$this
->getQuery()
->setSearchApiQuery($results
->getQuery());
} catch (SearchApiException $e) {
}
return TRUE;
}
}
return FALSE;
}
public function generateResultsKey() {
if (!isset($this->resultsKey)) {
$this
->getQuery()
->getSearchApiQuery()
->preExecute();
$view = $this
->getView();
$build_info = $view->build_info;
$key_data = [
'build_info' => $build_info,
'pager' => [
'page' => $view
->getCurrentPage(),
'items_per_page' => $view
->getItemsPerPage(),
'offset' => $view
->getOffset(),
],
];
$display_handler_cache_contexts = $this->displayHandler
->getCacheMetadata()
->getCacheContexts();
$key_data += $this
->getCacheContextsManager()
->convertTokensToKeys($display_handler_cache_contexts)
->getKeys();
$this->resultsKey = $view->storage
->id() . ':' . $this->displayHandler->display['id'] . ':results:' . Crypt::hashBase64(serialize($key_data));
}
return $this->resultsKey;
}
protected function getView() {
return $this->view;
}
protected function getQuery() {
$query = $this
->getView()
->getQuery();
if ($query instanceof SearchApiQuery) {
return $query;
}
throw new SearchApiException('No matching Search API Views query found in view.');
}
}