diff --git a/core/lib/Drupal/Core/Cache/CacheFactory.php b/core/lib/Drupal/Core/Cache/CacheFactory.php index 8547ac4..de8a2c7 100644 --- a/core/lib/Drupal/Core/Cache/CacheFactory.php +++ b/core/lib/Drupal/Core/Cache/CacheFactory.php @@ -67,12 +67,12 @@ public function get($bin) { if (isset($cache_settings['bins'][$bin])) { $service_name = $cache_settings['bins'][$bin]; } - elseif (isset($cache_settings['default'])) { - $service_name = $cache_settings['default']; - } elseif (isset($this->defaultBinBackends[$bin])) { $service_name = $this->defaultBinBackends[$bin]; } + elseif (isset($cache_settings['default'])) { + $service_name = $cache_settings['default']; + } else { $service_name = 'cache.backend.database'; } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 3834dae..7b638e5 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -317,43 +317,10 @@ protected function has($id, EntityInterface $entity) { } /** - * Gets entities from the static cache. - * - * @param array $ids - * If not empty, return entities that match these IDs. - * - * @return \Drupal\Core\Entity\EntityInterface[] - * Array of entities from the entity cache. - */ - protected function getFromStaticCache(array $ids) { - $entities = array(); - // Load any available entities from the internal cache. - if ($this->entityType->isStaticallyCacheable() && !empty($this->entities)) { - $config_overrides_key = $this->overrideFree ? '' : implode(':', $this->configFactory->getCacheKeys()); - foreach ($ids as $id) { - if (!empty($this->entities[$id])) { - if (isset($this->entities[$id][$config_overrides_key])) { - $entities[$id] = $this->entities[$id][$config_overrides_key]; - } - } - } - } - return $entities; - } - - /** - * Stores entities in the static entity cache. - * - * @param \Drupal\Core\Entity\EntityInterface[] $entities - * Entities to store in the cache. - */ - protected function setStaticCache(array $entities) { - if ($this->entityType->isStaticallyCacheable()) { - $config_overrides_key = $this->overrideFree ? '' : implode(':', $this->configFactory->getCacheKeys()); - foreach ($entities as $id => $entity) { - $this->entities[$id][$config_overrides_key] = $entity; - } - } + * {@inheritdoc} + */ + protected function getCacheId($id) { + return parent::getCacheId($id) . ':' . $config_overrides_key = $this->overrideFree ? '' : implode(':', $this->configFactory->getCacheKeys()); } /** diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index dea8865..7efdaa6 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -622,17 +622,17 @@ protected function setPersistentCache($entities) { */ public function resetCache(array $ids = NULL) { if ($ids) { - $cids = array(); - foreach ($ids as $id) { - unset($this->entities[$id]); - $cids[] = $this->buildCacheId($id); - } + parent::resetCache($ids); if ($this->entityType->isPersistentlyCacheable()) { + $cids = array(); + foreach ($ids as $id) { + $cids[] = $this->buildCacheId($id); + } $this->cacheBackend->deleteMultiple($cids); } } else { - $this->entities = array(); + parent::resetCache(); if ($this->entityType->isPersistentlyCacheable()) { Cache::invalidateTags(array($this->entityTypeId . '_values')); } diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index f583121..3b78371 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -3,6 +3,8 @@ namespace Drupal\Core\Entity; use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Cache\Cache; /** * A base entity storage class. @@ -73,18 +75,28 @@ protected $entityClass; /** + * The static cache backend. + * + * @var \Drupal\Core\Cache\CacheBackendInterface + */ + protected $staticCacheBackend; + + /** * Constructs an EntityStorageBase instance. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. + * @param \Drupal\Core\Cache\CacheBackendInterface + * The static cache backend. */ - public function __construct(EntityTypeInterface $entity_type) { + public function __construct(EntityTypeInterface $entity_type, CacheBackendInterface $static_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(); + $this->staticCacheBackend = isset($static_cache_backend) ? $static_cache_backend : \Drupal::service('cache.static'); } /** @@ -102,6 +114,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,11 +134,12 @@ 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->staticCacheBackend->delete($this->getCacheId($id)); } } else { - $this->entities = array(); + // Call the backend method directly. + $this->staticCacheBackend->invalidateTags(['entity_static_cache']); } } @@ -135,8 +155,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->staticCacheBackend->get($this->getCacheId($id))) { + $entities[$id] = $cached->data; + } + } } return $entities; } @@ -149,7 +173,9 @@ protected function getFromStaticCache(array $ids) { */ protected function setStaticCache(array $entities) { if ($this->entityType->isStaticallyCacheable()) { - $this->entities += $entities; + foreach ($entities as $id => $entity) { + $this->staticCacheBackend->set($this->getCacheId($entity->id()), $entity, CacheBackendInterface::CACHE_PERMANENT, ['entity_static_cache']); + } } } @@ -530,4 +556,8 @@ public function getAggregateQuery($conjunction = 'AND') { */ abstract protected function getQueryServiceName(); + public function __destruct() { + $this->staticCacheBackend->invalidateTags(['entity_static_cache']); + } + }