diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index dea8865..b3305cd 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -301,6 +301,8 @@ protected function doPostSave(EntityInterface $entity, $update) { $this->invokeTranslationHooks($entity); } + $this->doResetInternalState(array($entity->id())); + parent::doPostSave($entity, $update); // The revision is stored, it should no longer be marked as new now. @@ -318,6 +320,8 @@ protected function doDelete($entities) { $this->invokeFieldMethod('delete', $entity); } $this->doDeleteFieldItems($entities); + + $this->doResetInternalState(array_keys($entities)); } /** @@ -620,22 +624,31 @@ protected function setPersistentCache($entities) { /** * {@inheritdoc} */ - public function resetCache(array $ids = NULL) { - if ($ids) { - $cids = array(); - foreach ($ids as $id) { - unset($this->entities[$id]); - $cids[] = $this->buildCacheId($id); - } - if ($this->entityType->isPersistentlyCacheable()) { - $this->cacheBackend->deleteMultiple($cids); - } + protected function doResetInternalState(array $entity_ids = NULL) { + parent::doResetInternalState($entity_ids); + $this->clearPersistentCache($entity_ids); + } + + /** + * Clears the persistent cache for an entity if it uses persistent caching. + * + * @param mixed[] $entity_ids + * An array of entity IDs to clear the persistent cache for. + */ + protected function clearPersistentCache(array $entity_ids = NULL) { + if (!$this->entityType->isPersistentlyCacheable()) { + return; + } + + if ($entity_ids) { + $cache_ids = array_map(function($entity_id) { + return $this->buildCacheId($entity_id); + }, $entity_ids); + + $this->cacheBackend->deleteMultiple($cache_ids); } else { - $this->entities = array(); - if ($this->entityType->isPersistentlyCacheable()) { - Cache::invalidateTags(array($this->entityTypeId . '_values')); - } + 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 70e4f7c..1ccb495 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -105,7 +105,7 @@ public function getEntityType() { * {@inheritdoc} */ public function loadUnchanged($id) { - $this->resetCache(array($id)); + $this->doResetInternalState(array($id)); return $this->load($id); } @@ -361,7 +361,7 @@ public function delete(array $entities) { // Perform the delete and reset the static cache for the deleted entities. $this->doDelete($keyed_entities); - $this->resetCache(array_keys($keyed_entities)); + $this->doResetInternalState(array_keys($keyed_entities)); // Allow code to run after deleting. $entity_class::postDelete($this, $keyed_entities); @@ -460,7 +460,7 @@ protected function doPreSave(EntityInterface $entity) { * Specifies whether the entity is being updated or created. */ protected function doPostSave(EntityInterface $entity, $update) { - $this->resetCache(array($entity->id())); + $this->doResetInternalState(array($entity->id())); // The entity is no longer new. $entity->enforceIsNew(FALSE); @@ -494,6 +494,19 @@ protected function buildPropertyQuery(QueryInterface $entity_query, array $value } /** + * Reset internal state for entities. + * + * By default this only resets the static cache, but content entities will + * also clear the persistent cache. + * + * @param mixed[] $entity_ids + * An array of entity IDs to clear storage state for. + */ + protected function doResetInternalState(array $entity_ids = NULL) { + $this->resetCache($entity_ids); + } + + /** * {@inheritdoc} */ public function loadByProperties(array $values = array()) { diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php index 9759d27..2483a6c 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -43,8 +43,9 @@ public function updateLastLoginTimestamp(UserInterface $account) { ->fields(array('login' => $account->getLastLoginTime())) ->condition('uid', $account->id()) ->execute(); + // Ensure that the entity cache is cleared. - $this->resetCache(array($account->id())); + $this->doResetInternalState([$account->id()]); } /** @@ -58,7 +59,7 @@ public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) ->condition('uid', $account->id()) ->execute(); // Ensure that the entity cache is cleared. - $this->resetCache(array($account->id())); + $this->doResetInternalState([$account->id()]); } /**