diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index f583121..28d18d3 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Entity; use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\Core\Cache\CacheBackendInterface; /** * A base entity storage class. @@ -73,18 +74,28 @@ protected $entityClass; /** + * The static cache backend. + * + * @var \Drupal\Core\Cache\CacheBackendInterface + */ + protected $cache_backend; + + /** * Constructs an EntityStorageBase instance. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. */ - public function __construct(EntityTypeInterface $entity_type) { + public function __construct(EntityTypeInterface $entity_type, CacheBackendInterface $cache_backend = NULL) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->idKey = $this->entityType->getKey('id'); $this->uuidKey = $this->entityType->getKey('uuid'); $this->langcodeKey = $this->entityType->getKey('langcode'); $this->entityClass = $this->entityType->getClass(); + if (!isset($cache_backend)) { + $this->cacheBackend = \Drupal::service('cache.static'); + } } /** @@ -102,6 +113,13 @@ public function getEntityType() { } /** + * Build a cache ID for an entity. + */ + protected function getCacheId($id) { + return 'entity_storage_cache:' . $this->entityTypeId . ':' . $id; + } + + /** * {@inheritdoc} */ public function loadUnchanged($id) { @@ -115,7 +133,7 @@ public function loadUnchanged($id) { public function resetCache(array $ids = NULL) { if ($this->entityType->isStaticallyCacheable() && isset($ids)) { foreach ($ids as $id) { - unset($this->entities[$id]); + $this->cacheBackend->delete($this->getCacheId($id)); } } else { @@ -135,8 +153,12 @@ public function resetCache(array $ids = NULL) { protected function getFromStaticCache(array $ids) { $entities = array(); // Load any available entities from the internal cache. - if ($this->entityType->isStaticallyCacheable() && !empty($this->entities)) { - $entities += array_intersect_key($this->entities, array_flip($ids)); + if ($this->entityType->isStaticallyCacheable()) { + foreach ($ids as $id) { + if ($cached = $this->cacheBackend->get($this->getCacheId($id))) { + $entities[$id] = $cached->data; + } + } } return $entities; } @@ -149,7 +171,9 @@ protected function getFromStaticCache(array $ids) { */ protected function setStaticCache(array $entities) { if ($this->entityType->isStaticallyCacheable()) { - $this->entities += $entities; + foreach ($entities as $id => $entity) { + $this->cacheBackend->set($this->getCacheId($id), $entity); + } } }