Commit e7862f1e authored by catch's avatar catch

Issue #2983887 by Berdir: Add static cache to...

Issue #2983887 by Berdir: Add static cache to \Drupal\Core\Entity\ContentEntityStorageBase::getLatest*RevisionId()

(cherry picked from commit 8f00a9a1)
parent 38139005
......@@ -7,6 +7,7 @@
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\TranslationStatusInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -36,6 +37,13 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
*/
protected $cacheBackend;
/**
* Stores the latest revision IDs for entities.
*
* @var array
*/
protected $latestRevisionIds = [];
/**
* Constructs a ContentEntityStorageBase object.
*
......@@ -350,13 +358,17 @@ public function getLatestRevisionId($entity_id) {
return NULL;
}
$result = $this->getQuery()
->latestRevision()
->condition($this->entityType->getKey('id'), $entity_id)
->accessCheck(FALSE)
->execute();
if (!isset($this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT])) {
$result = $this->getQuery()
->latestRevision()
->condition($this->entityType->getKey('id'), $entity_id)
->accessCheck(FALSE)
->execute();
return key($result);
$this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT] = key($result);
}
return $this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT];
}
/**
......@@ -371,16 +383,19 @@ public function getLatestTranslationAffectedRevisionId($entity_id, $langcode) {
return $this->getLatestRevisionId($entity_id);
}
$result = $this->getQuery()
->allRevisions()
->condition($this->entityType->getKey('id'), $entity_id)
->condition($this->entityType->getKey('revision_translation_affected'), 1, '=', $langcode)
->range(0, 1)
->sort($this->entityType->getKey('revision'), 'DESC')
->accessCheck(FALSE)
->execute();
return key($result);
if (!isset($this->latestRevisionIds[$entity_id][$langcode])) {
$result = $this->getQuery()
->allRevisions()
->condition($this->entityType->getKey('id'), $entity_id)
->condition($this->entityType->getKey('revision_translation_affected'), 1, '=', $langcode)
->range(0, 1)
->sort($this->entityType->getKey('revision'), 'DESC')
->accessCheck(FALSE)
->execute();
$this->latestRevisionIds[$entity_id][$langcode] = key($result);
}
return $this->latestRevisionIds[$entity_id][$langcode];
}
/**
......@@ -1005,6 +1020,7 @@ public function resetCache(array $ids = NULL) {
if ($this->entityType->isPersistentlyCacheable()) {
$cids = [];
foreach ($ids as $id) {
unset($this->latestRevisionIds[$id]);
$cids[] = $this->buildCacheId($id);
}
$this->cacheBackend->deleteMultiple($cids);
......@@ -1015,6 +1031,7 @@ public function resetCache(array $ids = NULL) {
if ($this->entityType->isPersistentlyCacheable()) {
Cache::invalidateTags([$this->entityTypeId . '_values']);
}
$this->latestRevisionIds = [];
}
}
......
......@@ -482,6 +482,9 @@ protected function submitNodeForm($title, $moderation_state, $default_translatio
protected function loadTranslation(NodeInterface $node, $langcode) {
/** @var \Drupal\node\NodeStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')->getStorage('node');
// Explicitly invalidate the cache for that node, as the call below is
// statically cached.
$storage->resetCache([$node->id()]);
/** @var \Drupal\node\NodeInterface $node */
$node = $storage->loadRevision($storage->getLatestRevisionId($node->id()));
return $node->getTranslation($langcode);
......
......@@ -127,6 +127,9 @@ protected function setupBundle() {
* The active revision translation or NULL if none could be identified.
*/
protected function loadRevisionTranslation(ContentEntityInterface $entity, $langcode) {
// Explicitly invalidate the cache for that node, as the call below is
// statically cached.
$this->storage->resetCache([$entity->id()]);
$revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode);
/** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
$revision = $revision_id ? $this->storage->loadRevision($revision_id) : NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment