Skip to content
Snippets Groups Projects

Initial proof of concept for delayed multiple loading of entities.

Files

@@ -89,6 +89,11 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor
*/
protected $memoryCacheTag;
/**
* Entity IDs awaiting loading.
*/
protected array $entityIdsToLoad = [];
/**
* Constructs an EntityStorageBase instance.
*
@@ -285,6 +290,34 @@ public function loadMultiple(?array $ids = NULL) {
$ids = array_keys(array_diff_key($flipped_ids, $entities));
}
if ($ids && $this->entityType->isStaticallyCacheable()) {
if (\Fiber::getCurrent() !== NULL) {
// Before suspending the fiber, add the IDs passed in to the full list
// of entities to load, so that another call can load everything at
// once.
$this->entityIdsToLoad = array_merge($this->entityIdsToLoad, $ids);
\Fiber::suspend();
// Another call to this method may have reset the entityIdsToLoad
// property after loading entities. Combine it with the passed in IDs
// again in case this has happened.
$this->entityIdsToLoad = array_merge($this->entityIdsToLoad, $ids);
$entities += $this->getFromStaticCache($this->entityIdsToLoad);
if ($entities) {
// Remove any entities found in the static cache from the IDs to load.
$ids = array_keys(array_diff_key(array_flip($this->entityIdsToLoad), $entities));
}
else {
// If nothing was found in the static cache, load every entity ID
// requested so far.
$ids = array_unique($this->entityIdsToLoad);
}
// Now that we've reached this point, unset the list of entity IDs to
// load so that further calls start with a blank slate (apart from the
// entity static cache itself).
$this->entityIdsToLoad = [];
}
}
// Try to gather any remaining entities from a 'preload' method. This method
// can invoke a hook to be used by modules that need, for example, to swap
// the default revision of an entity with a different one. Even though the
Loading