Issue #2983887 by Berdir: Add static cache to \Drupal\Core\Entity\ContentEntityStorageBase::getLatest*RevisionId()
parent
f0d889eb3a
commit
8f00a9a1ee
|
|
@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheBackendInterface;
|
||||||
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
|
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||||
|
use Drupal\Core\Language\LanguageInterface;
|
||||||
use Drupal\Core\TypedData\TranslationStatusInterface;
|
use Drupal\Core\TypedData\TranslationStatusInterface;
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
|
@ -36,6 +37,13 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
*/
|
*/
|
||||||
protected $cacheBackend;
|
protected $cacheBackend;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the latest revision IDs for entities.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $latestRevisionIds = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a ContentEntityStorageBase object.
|
* Constructs a ContentEntityStorageBase object.
|
||||||
*
|
*
|
||||||
|
|
@ -350,13 +358,17 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isset($this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT])) {
|
||||||
$result = $this->getQuery()
|
$result = $this->getQuery()
|
||||||
->latestRevision()
|
->latestRevision()
|
||||||
->condition($this->entityType->getKey('id'), $entity_id)
|
->condition($this->entityType->getKey('id'), $entity_id)
|
||||||
->accessCheck(FALSE)
|
->accessCheck(FALSE)
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
return key($result);
|
$this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT] = key($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -371,6 +383,7 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
return $this->getLatestRevisionId($entity_id);
|
return $this->getLatestRevisionId($entity_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isset($this->latestRevisionIds[$entity_id][$langcode])) {
|
||||||
$result = $this->getQuery()
|
$result = $this->getQuery()
|
||||||
->allRevisions()
|
->allRevisions()
|
||||||
->condition($this->entityType->getKey('id'), $entity_id)
|
->condition($this->entityType->getKey('id'), $entity_id)
|
||||||
|
|
@ -380,7 +393,9 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
->accessCheck(FALSE)
|
->accessCheck(FALSE)
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
return key($result);
|
$this->latestRevisionIds[$entity_id][$langcode] = key($result);
|
||||||
|
}
|
||||||
|
return $this->latestRevisionIds[$entity_id][$langcode];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1005,6 +1020,7 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
if ($this->entityType->isPersistentlyCacheable()) {
|
if ($this->entityType->isPersistentlyCacheable()) {
|
||||||
$cids = [];
|
$cids = [];
|
||||||
foreach ($ids as $id) {
|
foreach ($ids as $id) {
|
||||||
|
unset($this->latestRevisionIds[$id]);
|
||||||
$cids[] = $this->buildCacheId($id);
|
$cids[] = $this->buildCacheId($id);
|
||||||
}
|
}
|
||||||
$this->cacheBackend->deleteMultiple($cids);
|
$this->cacheBackend->deleteMultiple($cids);
|
||||||
|
|
@ -1015,6 +1031,7 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
|
||||||
if ($this->entityType->isPersistentlyCacheable()) {
|
if ($this->entityType->isPersistentlyCacheable()) {
|
||||||
Cache::invalidateTags([$this->entityTypeId . '_values']);
|
Cache::invalidateTags([$this->entityTypeId . '_values']);
|
||||||
}
|
}
|
||||||
|
$this->latestRevisionIds = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -482,6 +482,9 @@ class ModerationLocaleTest extends ModerationStateTestBase {
|
||||||
protected function loadTranslation(NodeInterface $node, $langcode) {
|
protected function loadTranslation(NodeInterface $node, $langcode) {
|
||||||
/** @var \Drupal\node\NodeStorageInterface $storage */
|
/** @var \Drupal\node\NodeStorageInterface $storage */
|
||||||
$storage = $this->container->get('entity_type.manager')->getStorage('node');
|
$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 */
|
/** @var \Drupal\node\NodeInterface $node */
|
||||||
$node = $storage->loadRevision($storage->getLatestRevisionId($node->id()));
|
$node = $storage->loadRevision($storage->getLatestRevisionId($node->id()));
|
||||||
return $node->getTranslation($langcode);
|
return $node->getTranslation($langcode);
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,9 @@ abstract class ContentTranslationPendingRevisionTestBase extends ContentTranslat
|
||||||
* The active revision translation or NULL if none could be identified.
|
* The active revision translation or NULL if none could be identified.
|
||||||
*/
|
*/
|
||||||
protected function loadRevisionTranslation(ContentEntityInterface $entity, $langcode) {
|
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);
|
$revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode);
|
||||||
/** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
|
/** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
|
||||||
$revision = $revision_id ? $this->storage->loadRevision($revision_id) : NULL;
|
$revision = $revision_id ? $this->storage->loadRevision($revision_id) : NULL;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue