Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added caching support #3

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions src/Plugin/views/area/SpellCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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,
);
}

Expand All @@ -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.
*
Expand All @@ -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.
Expand All @@ -97,18 +146,18 @@ public function render($empty = FALSE) {
}
// Add the suggestion.
$suggestions[] = [
'#type' => 'link',
'#title' => reset($filter),
'#url' => Url::fromRoute('<current>', [], ['query' => $filter]),
'#type' => 'link',
'#title' => reset($filter),
'#url' => Url::fromRoute('<current>', [], ['query' => $filter]),
];
}
}
}
if (!empty($suggestions)) {
return [
'#title' => $this->getSuggestionLabel(),
'#theme' => 'item_list',
'#items' => $suggestions,
'#title' => $this->getSuggestionLabel(),
'#theme' => 'item_list',
'#items' => $suggestions,
];
}
}
Expand Down