diff --git a/search_api_db/tests/src/Kernel/BackendTest.php b/search_api_db/tests/src/Kernel/BackendTest.php index 8e3d417..3f5146c 100644 --- a/search_api_db/tests/src/Kernel/BackendTest.php +++ b/search_api_db/tests/src/Kernel/BackendTest.php @@ -878,7 +878,6 @@ class BackendTest extends KernelTestBase { // Regression test for #1916474. /** @var \Drupal\search_api\IndexInterface $index */ $index = $this->getIndex(); - $index->resetCaches(); $this->addField($index, 'prices', 'decimal'); $success = $index->save(); $this->assertTrue($success, 'The index field settings were successfully changed.'); diff --git a/src/Entity/Index.php b/src/Entity/Index.php index c7f22e6..614e0fe 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -595,7 +595,6 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->field_settings[$field_id] = $field->getSettings(); - $this->resetCaches(); return $this; } @@ -619,7 +618,6 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->field_settings[$new_field_id] = $this->field_settings[$old_field_id]; unset($this->field_settings[$old_field_id]); - $this->resetCaches(); return $this; } @@ -638,7 +636,6 @@ class Index extends ConfigEntityBase implements IndexInterface { unset($this->field_settings[$field_id]); - $this->resetCaches(); return $this; } @@ -646,14 +643,10 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getFields() { - $fields = $this->getCache(__FUNCTION__, FALSE); - if (!$fields) { - $fields = array(); - foreach ($this->field_settings as $key => $field_info) { - $fields[$key] = Utility::createField($this, $key, $field_info); - } + $fields = array(); + foreach ($this->field_settings as $key => $field_info) { + $fields[$key] = Utility::createField($this, $key, $field_info); } - $this->setCache(__FUNCTION__, $fields, FALSE); return $fields; } @@ -670,14 +663,10 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getFieldsByDatasource($datasource_id) { - $datasource_fields = $this->getCache(__FUNCTION__); - if (!$datasource_fields) { - $datasource_fields = array_fill_keys(array_keys($this->datasource_settings), array()); - $datasource_fields[NULL] = array(); - foreach ($this->getFields() as $field_id => $field) { - $datasource_fields[$field->getDatasourceId()][$field_id] = $field; - } - $this->setCache(__FUNCTION__, $datasource_fields); + $datasource_fields = array_fill_keys(array_keys($this->datasource_settings), array()); + $datasource_fields[NULL] = array(); + foreach ($this->getFields() as $field_id => $field) { + $datasource_fields[$field->getDatasourceId()][$field_id] = $field; } return $datasource_fields[$datasource_id]; @@ -687,15 +676,11 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getFulltextFields() { - $fulltext_fields = $this->getCache(__FUNCTION__); - if (!$fulltext_fields) { - $fulltext_fields = array(); - foreach ($this->field_settings as $key => $field_info) { - if (Utility::isTextType($field_info['type'])) { - $fulltext_fields[] = $key; - } + $fulltext_fields = array(); + foreach ($this->field_settings as $key => $field_info) { + if (Utility::isTextType($field_info['type'])) { + $fulltext_fields[] = $key; } - $this->setCache(__FUNCTION__, $fulltext_fields); } return $fulltext_fields; } @@ -705,22 +690,18 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getPropertyDefinitions($datasource_id, $alter = TRUE) { $alter = $alter ? 1 : 0; - $properties = $this->getCache(__FUNCTION__); - if (!isset($properties[$datasource_id][$alter])) { - if (isset($datasource_id)) { - $datasource = $this->getDatasource($datasource_id); - $properties[$datasource_id][$alter] = $datasource->getPropertyDefinitions(); - } - else { - $datasource = NULL; - $properties[$datasource_id][$alter] = array(); - } - if ($alter) { - foreach ($this->getProcessors() as $processor) { - $processor->alterPropertyDefinitions($properties[$datasource_id][$alter], $datasource); - } + if (isset($datasource_id)) { + $datasource = $this->getDatasource($datasource_id); + $properties[$datasource_id][$alter] = $datasource->getPropertyDefinitions(); + } + else { + $datasource = NULL; + $properties[$datasource_id][$alter] = array(); + } + if ($alter) { + foreach ($this->getProcessors() as $processor) { + $processor->alterPropertyDefinitions($properties[$datasource_id][$alter], $datasource); } - $this->setCache(__FUNCTION__, $properties); } return $properties[$datasource_id][$alter]; } @@ -961,73 +942,6 @@ class Index extends ConfigEntityBase implements IndexInterface { } /** - * Retrieves data from the static and/or stored cache. - * - * @param string $method - * The name of the method for which data is requested. Used to construct the - * cache ID. - * @param bool $static_only - * (optional) If TRUE, only consult the static cache for this page request, - * don't attempt to load the value from the stored cache. - * - * @return mixed|null - * The cached data, or NULL if it could not be found. - */ - protected function getCache($method, $static_only = TRUE) { - if (isset($this->cache[$method])) { - return $this->cache[$method]; - } - - if (!$static_only) { - $cid = $this->getCacheId($method); - if ($cached = \Drupal::cache()->get($cid)) { - if (is_array($cached->data)) { - $this->updateFieldsIndex($cached->data); - } - $this->cache[$method] = $cached->data; - return $this->cache[$method]; - } - } - - return NULL; - } - - /** - * Sets data in the static and/or stored cache. - * - * @param string $method - * The name of the method for which cached data should be set. Used to - * construct the cache ID. - * @param mixed $value - * The value to set for the cache. - * @param bool $static_only - * (optional) If TRUE, only set the value in the static cache for this page - * request, not in the stored cache. - * - * @return $this - */ - protected function setCache($method, $value, $static_only = TRUE) { - $this->cache[$method] = $value; - if (!$static_only) { - $cid = $this->getCacheId($method); - \Drupal::cache() - ->set($cid, $value, Cache::PERMANENT, $this->getCacheTags()); - } - return $this; - } - - /** - * {@inheritdoc} - */ - public function resetCaches($include_stored = TRUE) { - $this->trackerInstance = NULL; - $this->cache = array(); - if ($include_stored) { - Cache::invalidateTags($this->getCacheTags()); - } - } - - /** * Sets this object as the index for all fields contained in the given array. * * This is important when loading fields from the cache, because their index @@ -1089,9 +1003,6 @@ class Index extends ConfigEntityBase implements IndexInterface { } } - // Reset the cache so the used processors and fields will be up-to-date. - $this->resetCaches(); - // Call the preIndexSave() method of all applicable processors. foreach ($this->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE) as $processor) { $processor->preIndexSave(); @@ -1103,7 +1014,6 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); - $this->resetCaches(); try { // Fake an original for inserts to make code cleaner. @@ -1154,7 +1064,7 @@ class Index extends ConfigEntityBase implements IndexInterface { \Drupal::cache('discovery')->delete('views:wizard'); } - $this->resetCaches(); + Cache::invalidateTags($this->getCacheTags()); } catch (SearchApiException $e) { watchdog_exception('search_api', $e); @@ -1550,6 +1460,10 @@ class Index extends ConfigEntityBase implements IndexInterface { 'settings' => array(), ) ); + + // Repopulate the tracker instance with the new settings. + $this->trackerInstance = NULL; + $this->getTrackerInstance(); } // There also always needs to be a datasource, but here we have no easy way // out – if we had to remove all datasources, the operation fails. Return @@ -1575,10 +1489,6 @@ class Index extends ConfigEntityBase implements IndexInterface { } } - if ($changed) { - $this->resetCaches(); - } - return $changed; } @@ -1602,16 +1512,6 @@ class Index extends ConfigEntityBase implements IndexInterface { } /** - * Implements the magic __clone() method. - * - * Prevents the cached plugins and fields from being cloned, too (since they - * would then point to the wrong index object). - */ - public function __clone() { - $this->resetCaches(FALSE); - } - - /** * Implements the magic __sleep() method. * * Prevents the cached plugins and fields from being serialized. diff --git a/src/IndexInterface.php b/src/IndexInterface.php index 5c8a07a..85570ae 100644 --- a/src/IndexInterface.php +++ b/src/IndexInterface.php @@ -576,15 +576,6 @@ interface IndexInterface extends ConfigEntityInterface { public function isReindexing(); /** - * Resets the static and stored caches associated with this index. - * - * @param bool $include_stored - * (optional) If set to FALSE, only the static caches will be cleared, the - * stored cache will remain untouched. - */ - public function resetCaches($include_stored = TRUE); - - /** * Creates a query object for this index. * * @param array $options diff --git a/src/Item/Field.php b/src/Item/Field.php index 8580c59..2d14eb4 100644 --- a/src/Item/Field.php +++ b/src/Item/Field.php @@ -167,7 +167,7 @@ class Field implements \IteratorAggregate, FieldInterface { * {@inheritdoc} */ public function setIndex(IndexInterface $index) { - if ($this->index->id() != $index->id()) { + if ($this->index instanceof IndexInterface && $this->index->id() != $index->id()) { throw new \InvalidArgumentException('Attempted to change the index of a field object.'); } $this->index = $index; diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index b639e9c..9ea4230 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -501,10 +501,6 @@ class IntegrationTest extends WebTestBase { $this->drupalPostForm(NULL, $edit, $this->t('Save and continue')); $this->drupalPostForm(NULL, array(), $this->t('Save field settings')); - // @todo This should not be necessary, the field cache should be invalidated - // automatically when available fields change. See #2637032. - $this->getIndex()->resetCaches(); - $url_options['query']['datasource'] = 'entity:node'; $this->drupalGet($this->getIndexPath('fields/add'), $url_options); $this->assertHtmlEscaped($field_name); diff --git a/src/UnsavedIndexConfiguration.php b/src/UnsavedIndexConfiguration.php index bb93c57..1f1045e 100644 --- a/src/UnsavedIndexConfiguration.php +++ b/src/UnsavedIndexConfiguration.php @@ -431,13 +431,6 @@ class UnsavedIndexConfiguration implements IndexInterface, UnsavedConfigurationI /** * {@inheritdoc} */ - public function resetCaches($include_stored = TRUE) { - return $this->entity->resetCaches($include_stored); - } - - /** - * {@inheritdoc} - */ public function query(array $options = array()) { return $this->entity->query($options); } diff --git a/tests/src/Kernel/CustomDataTypesTest.php b/tests/src/Kernel/CustomDataTypesTest.php index 56b9647..5ba3ec7 100644 --- a/tests/src/Kernel/CustomDataTypesTest.php +++ b/tests/src/Kernel/CustomDataTypesTest.php @@ -116,8 +116,8 @@ class CustomDataTypesTest extends KernelTestBase { $item = $this->index->loadItem('entity:entity_test/1:en'); $item = Utility::createItemFromObject($this->index, $item, 'entity:entity_test/1:en'); - $name_field = $item->getField('name'); + $name_field = $item->getField('name'); $processed_value = $name_field->getValues()[0]; $processed_type = $name_field->getType();