diff -u b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php --- b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -238,13 +238,13 @@ // having to load the entity object from the storage, then check if there // is already an entry in the static entity revisions cache for it and if // not load the entity from the storage. - if ($this->entityType->isStaticallyCacheable() && $entity->getRevisionId()) { + if ($this->entityType->isStaticallyCacheable() && $this->entityType->isRevisionable()) { // If the default revision is not present in the static entity revision // cache we add it and return the entity. - if (!isset($this->entityRevisions[$entity->getRevisionId()])) { - $this->entityRevisions[$entity->getRevisionId()] = $entity; + if (!$this->getFromStaticEntityRevisionCache($entity->getRevisionId())) { + $this->setStaticEntityRevisionCache($entity->getRevisionId(), $entity); } - return $this->entityRevisions[$entity->getRevisionId()]; + return $this->getFromStaticEntityRevisionCache($entity->getRevisionId()); } return $entity; @@ -255,19 +255,19 @@ */ public function loadRevision($revision_id) { if ($this->entityType->isStaticallyCacheable()) { - if (!isset($this->entityRevisions[$revision_id])) { + if (!($revision = $this->getFromStaticEntityRevisionCache($revision_id))) { if ($revision = $this->getRevisionFromPersistentCache($revision_id)) { - $this->entityRevisions[$revision_id] = $revision; + $this->setStaticEntityRevisionCache($revision_id, $revision); } else { $revision = $this->doLoadRevision($revision_id); - $this->entityRevisions[$revision_id] = $revision; + $this->setStaticEntityRevisionCache($revision_id, $revision); if ($revision->isDefaultRevision()) { $this->setPersistentCache([$revision->id() => $revision]); } } } - return $this->entityRevisions[$revision_id]; + return $revision; } return $this->doLoadRevision($revision_id); @@ -380,8 +380,8 @@ // Set the static entity revision cache reference, which has been removed // by the parent class call to ::resetCache. - if ($this->entityType->isStaticallyCacheable() && $entity->getOriginalRevisionId()) { - $this->entityRevisions[$entity->getOriginalRevisionId()] = $entity; + if ($this->entityType->isRevisionable()) { + $this->setStaticEntityRevisionCache($entity->getOriginalRevisionId(), $entity); } } @@ -756,6 +756,35 @@ } /** + * Gets entities from the static entity revision cache. + * + * @param int $revision_id + * The entity revision id, for which the entity should be loaded from the + * static entity revision cache. + * + * @return \Drupal\Core\Entity\ContentEntityInterface|NULL + * The entity from the entity revision cache or NULL if it is not present. + */ + protected function getFromStaticEntityRevisionCache($revision_id) { + return $this->entityType->isStaticallyCacheable() && isset($this->entityRevisions[$revision_id]) ? $this->entityRevisions[$revision_id] : NULL; + } + + /** + * Stores entities in the static entity revision cache. + * + * @param int $revision_id + * The entity revision id, for which the entity should be stored in the + * static entity revision cache. + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity to store in the static entity revision cache. + */ + protected function setStaticEntityRevisionCache($revision_id, ContentEntityInterface $entity) { + if ($this->entityType->isStaticallyCacheable()) { + $this->entityRevisions[$revision_id] = $entity; + } + } + + /** * {@inheritdoc} */ public function loadRevisionUnchanged($revision_id) { @@ -763,8 +792,8 @@ // and it represents a default revision, then delete the entity and the // revision cache for it, otherwise there will be no entity cache and in // this case we have to delete only the revision cache. - if (isset($this->entityRevisions[$revision_id]) && $this->entityRevisions[$revision_id]->isDefaultRevision()) { - $this->resetCache([$this->entityRevisions[$revision_id]->id()]); + if (($revision = $this->getFromStaticEntityRevisionCache($revision_id)) && $revision->isDefaultRevision()) { + $this->resetCache([$revision->id()]); } else { $this->resetRevisionCache([$revision_id]); diff -u b/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php --- b/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -483,9 +483,7 @@ // Set the static entity cache reference, which has been removed by the // call to ::resetCache. - if ($this->entityType->isStaticallyCacheable()) { - $this->entities[$entity->id()] = $entity; - } + $this->setStaticCache([$entity->id() => $entity]); } /**