From e3ede9464301b18d25d2eb39ee83cb048c9bf9e7 Mon Sep 17 00:00:00 2001 From: Mark Wienk Date: Wed, 19 Jul 2017 14:15:01 +0200 Subject: [PATCH] Added caching support --- src/Plugin/views/area/SpellCheck.php | 81 ++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Plugin/views/area/SpellCheck.php b/src/Plugin/views/area/SpellCheck.php index 515aa7c..59141fc 100644 --- a/src/Plugin/views/area/SpellCheck.php +++ b/src/Plugin/views/area/SpellCheck.php @@ -2,10 +2,13 @@ namespace Drupal\search_api_spellcheck\Plugin\views\area; +use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\search_api\Plugin\views\filter\SearchApiFulltext; +use Drupal\search_api\Query\ResultSetInterface; use Drupal\views\Plugin\views\area\AreaPluginBase; +use Drupal\views\Views; /** * Provides an area for messages. @@ -16,6 +19,14 @@ */ class SpellCheck extends AreaPluginBase { + const SPELLCHECK_CACHE_SUFFIX = ":spellcheck"; + const SPELLCHECK_CACHE_BIN = "data"; + + /** + * @var \Drupal\views\Plugin\views\cache\CachePluginBase + */ + private $cache; + /** * The available filters for the current view. * @@ -47,15 +58,15 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); $form['search_api_spellcheck_title'] = array( - '#type' => 'textfield', - '#title' => $this->t('The title to announce the suggestions. Default: suggestions:'), - '#default_value' => isset($this->options['search_api_spellcheck_title']) ? $this->options['search_api_spellcheck_title'] : '', + '#type' => 'textfield', + '#title' => $this->t('The title to announce the suggestions. Default: suggestions:'), + '#default_value' => isset($this->options['search_api_spellcheck_title']) ? $this->options['search_api_spellcheck_title'] : '', ); $form['search_api_spellcheck_hide_on_result'] = array( - '#type' => 'checkbox', - '#title' => $this->t('Hide when the view has results.'), - '#default_value' => isset($this->options['search_api_spellcheck_hide_on_result']) ? $this->options['search_api_spellcheck_hide_on_result'] : TRUE, + '#type' => 'checkbox', + '#title' => $this->t('Hide when the view has results.'), + '#default_value' => isset($this->options['search_api_spellcheck_hide_on_result']) ? $this->options['search_api_spellcheck_hide_on_result'] : TRUE, ); } @@ -69,6 +80,45 @@ public function preQuery() { parent::preQuery(); } + /** + * @return mixed + */ + protected function getCache() + { + if(!$this->cache) { + if (!empty($this->live_preview)) { + $this->cache = Views::pluginManager('cache')->createInstance('none'); + } else { + $this->cache = $this->view->display_handler->getPlugin('cache'); + } + } + return $this->cache; + } + + /** + * Saves the solr response to the cache + * @param $values + */ + public function postExecute(&$values) + { + /** @var ResultSetInterface $result */ + $result = $this->query->getSearchApiResults(); + $response = $result->getExtraData('search_api_solr_response'); + $tags = $this->getCache()->getCacheTags(); + \Drupal::cache(self::SPELLCHECK_CACHE_BIN)->set($this->getCacheKey(), $response, Cache::PERMANENT, $tags); + parent::postExecute($values); + } + + /** + * Returns a generated cache key (based on the views cache key) + * @return string + */ + public function getCacheKey() + { + $cache = $this->getCache(); + return $cache->generateResultsKey() . self::SPELLCHECK_CACHE_SUFFIX; + } + /** * Render the area. * @@ -80,13 +130,12 @@ public function preQuery() { */ public function render($empty = FALSE) { if ($this->options['search_api_spellcheck_hide_on_result'] == FALSE || ($this->options['search_api_spellcheck_hide_on_result'] && $empty)) { - $result = $this->query->getSearchApiResults(); - // Check if extraData is there. - if ($extra_data = $result->getExtraData('search_api_solr_response')) { + $cacheItem = \Drupal::cache(self::SPELLCHECK_CACHE_BIN)->get($this->getCacheKey()); + if ($extra_data = $cacheItem->data) { // Initialize our array. $suggestions = []; // Check that we have suggestions. - if (!empty($extra_data['spellcheck'])) { + if (!empty($extra_data['spellcheck']) && !empty($extra_data['spellcheck']['suggestions'])) { // Loop over the suggestions and print them as links. foreach ($extra_data['spellcheck'] as $suggestion) { // If we have a match within our filters we add the suggestion. @@ -97,18 +146,18 @@ public function render($empty = FALSE) { } // Add the suggestion. $suggestions[] = [ - '#type' => 'link', - '#title' => reset($filter), - '#url' => Url::fromRoute('', [], ['query' => $filter]), + '#type' => 'link', + '#title' => reset($filter), + '#url' => Url::fromRoute('', [], ['query' => $filter]), ]; } } } if (!empty($suggestions)) { return [ - '#title' => $this->getSuggestionLabel(), - '#theme' => 'item_list', - '#items' => $suggestions, + '#title' => $this->getSuggestionLabel(), + '#theme' => 'item_list', + '#items' => $suggestions, ]; } }