Unverified Commit b1be147d authored by alexpott's avatar alexpott

Issue #3013917 by tstoeckler, hchonov, alexpott: Minor improvements for (Content|)EntityStorageBase

parent 86513445
...@@ -1042,12 +1042,10 @@ public function loadUnchanged($id) { ...@@ -1042,12 +1042,10 @@ public function loadUnchanged($id) {
// entity directly from the persistent cache. // entity directly from the persistent cache.
$this->postLoad($entities); $this->postLoad($entities);
if ($this->entityType->isStaticallyCacheable()) { // As we've removed the entity from the static cache already we have to
// As we've removed the entity from the static cache already we have to // put the loaded unchanged entity there to simulate the behavior of the
// put the loaded unchanged entity there to simulate the behavior of the // parent.
// parent. $this->setStaticCache($entities);
$this->setStaticCache($entities);
}
} }
return $entities[$id]; return $entities[$id];
......
...@@ -161,7 +161,7 @@ public function resetCache(array $ids = NULL) { ...@@ -161,7 +161,7 @@ public function resetCache(array $ids = NULL) {
* If not empty, return entities that match these IDs. * If not empty, return entities that match these IDs.
* *
* @return \Drupal\Core\Entity\EntityInterface[] * @return \Drupal\Core\Entity\EntityInterface[]
* Array of entities from the entity cache. * Array of entities from the entity cache, keyed by entity ID.
*/ */
protected function getFromStaticCache(array $ids) { protected function getFromStaticCache(array $ids) {
$entities = []; $entities = [];
...@@ -259,17 +259,15 @@ public function loadMultiple(array $ids = NULL) { ...@@ -259,17 +259,15 @@ public function loadMultiple(array $ids = NULL) {
// Create a new variable which is either a prepared version of the $ids // Create a new variable which is either a prepared version of the $ids
// array for later comparison with the entity cache, or FALSE if no $ids // array for later comparison with the entity cache, or FALSE if no $ids
// were passed. The $ids array is reduced as items are loaded from cache, // were passed. The $ids array is reduced as items are loaded from cache,
// and we need to know if it's empty for this reason to avoid querying the // and we need to know if it is empty for this reason to avoid querying the
// database when all requested entities are loaded from cache. // database when all requested entities are loaded from cache.
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE; $flipped_ids = $ids ? array_flip($ids) : FALSE;
// Try to load entities from the static cache, if the entity type supports // Try to load entities from the static cache, if the entity type supports
// static caching. // static caching.
if ($this->entityType->isStaticallyCacheable() && $ids) { if ($ids) {
$entities += $this->getFromStaticCache($ids); $entities += $this->getFromStaticCache($ids);
// If any entities were loaded, remove them from the ids still to load. // If any entities were loaded, remove them from the IDs still to load.
if ($passed_ids) { $ids = array_keys(array_diff_key($flipped_ids, $entities));
$ids = array_keys(array_diff_key($passed_ids, $entities));
}
} }
// Gather entities from a 'preload' method. This method can invoke a hook to // Gather entities from a 'preload' method. This method can invoke a hook to
...@@ -281,10 +279,13 @@ public function loadMultiple(array $ids = NULL) { ...@@ -281,10 +279,13 @@ public function loadMultiple(array $ids = NULL) {
$preloaded_entities = $this->preLoad($ids); $preloaded_entities = $this->preLoad($ids);
if (!empty($preloaded_entities)) { if (!empty($preloaded_entities)) {
$entities += $preloaded_entities; $entities += $preloaded_entities;
// Add pre-loaded entities to the cache.
$this->setStaticCache($preloaded_entities);
} }
// Load any remaining entities from the database. This is the case if $ids // Load any remaining entities from the database. This is the case if $ids
// is set to NULL (so we load all entities) or if there are any ids left to // is set to NULL (so we load all entities) or if there are any IDs left to
// load. // load.
if ($ids === NULL || $ids) { if ($ids === NULL || $ids) {
$queried_entities = $this->doLoadMultiple($ids); $queried_entities = $this->doLoadMultiple($ids);
...@@ -296,28 +297,17 @@ public function loadMultiple(array $ids = NULL) { ...@@ -296,28 +297,17 @@ public function loadMultiple(array $ids = NULL) {
if (!empty($queried_entities)) { if (!empty($queried_entities)) {
$this->postLoad($queried_entities); $this->postLoad($queried_entities);
$entities += $queried_entities; $entities += $queried_entities;
}
if ($this->entityType->isStaticallyCacheable()) {
// Add pre-loaded entities to the cache.
if (!empty($preloaded_entities)) {
$this->setStaticCache($preloaded_entities);
}
// Add queried entities to the cache. // Add queried entities to the cache.
if (!empty($queried_entities)) { $this->setStaticCache($queried_entities);
$this->setStaticCache($queried_entities);
}
} }
// Ensure that the returned array is ordered the same as the original // Ensure that the returned array is ordered the same as the original
// $ids array if this was passed in and remove any invalid ids. // $ids array if this was passed in and remove any invalid IDs.
if ($passed_ids) { if ($flipped_ids) {
// Remove any invalid ids from the array. // Remove any invalid IDs from the array and preserve the order passed in.
$passed_ids = array_intersect_key($passed_ids, $entities); $flipped_ids = array_intersect_key($flipped_ids, $entities);
foreach ($entities as $entity) { $entities = array_replace($flipped_ids, $entities);
$passed_ids[$entity->id()] = $entity;
}
$entities = $passed_ids;
} }
return $entities; return $entities;
......
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