ContentEntityStorageBase::loadRevision() should use the static and the persistent entity cache like ContentEntityStorageBase::load()

Created on 23 November 2015, almost 9 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

#597236: Add entity caching to core β†’ added the entity cache to core, but unfortunately it did not consider enabling it when loading an entity by revision ID.
There are contrib modules such as Entity Reference Revisions β†’ , Paragraphs and core modules such as Content Moderation, which rely heavily on loading entity revisions.

A developer might build a system where she is using ContentEntityStorageBase::loadRevision() to load a default entity revision and by doing so one will currently not profit from the entity cache.

ContentEntityStorageBase::loadRevision() is neither using the static entity cache nor the persistent entity cache, which has as a consequence that the function is inconsistent with ContentEntityStorageBase::load(), as if you retrieve an entity multiple times in a single request with the latter one it will always return a reference to the same entity object, but if you retrieve it multiple times with the former one you will always get a new entity object and thus a completely different object reference.

The attached test demonstrates all the problems.

Proposed resolution

  1. Enable static entity caching for ContentEntityStorageBase::loadRevision() for all revisions.
  2. Enable persistent entity caching for ContentEntityStorageBase::loadRevision() for all revisions.
  3. Ensure that ContentEntityStorageBase::load() and ContentEntityStorageBase::loadRevision() return a reference to the same entity object, if loading the default entity revision.
  4. To ensure backwards compatibility $storage::resetCache() will be invalidating all cached revisions. Include an optimization to invalidate only the default revision when the entity has only revisionable fields.

Remaining tasks

Review.

User interface changes

none

API changes

  1. \Drupal\Core\Entity\RevisionableStorageInterface::loadRevisionUnchanged() to load an entity revision bypassing the static cache.
  2. \Drupal\Core\Entity\RevisionableStorageInterface::resetRevisionCache($revision_id) for reseting the static and persistent cache for specific revision IDs
  3. $storage->loadRevision($revision_id) === $storage->loadRevision($revision_id)now evaluates to TRUE
  4. $storage->load($id) === $storage->loadRevision($revision_id)now evaluates to TRUE if $revision_id references the current default revision

Data model changes

none

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

  • needs profiling

    It may affect performance, and thus requires in-depth technical reviews and profiling.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States muriqui

    Reroll of #168, resolving two conflicts:

    • In ContentEntityStorageBase::setPersistentCache, cache tags are now set using the $items array and $this->cacheBackend->setMultiple($items), rather than this->cacheBackend->set() as in #168.
    • In SqlContentEntityStorageTest::testLoadMultiplePersistentCacheMiss, changed $entity->expects($this->any())->method('isDefaultRevision')->will($this->returnValue(TRUE)); to $entity->expects($this->any())->method('isDefaultRevision')->willReturn(TRUE);
  • last update over 1 year ago
    Patch Failed to Apply
  • Status changed to Needs work about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium weseze

    Patch applies correctly to latest releases of both D9 and D10. (haven't tested dev)
    We are using it to fix serious performance issues in a heavy customised layout builder setup. Layout builder pages can contains up to 100 inline blocks. (but really there is no actual hard limit for us)

    Before the patch (while editing and translating pages with LB) we had 5-10.000 SQL queries taking 3-5seconds of loading time. (depending on the complexity of the layout builder setup on the page)
    After the patch this went down to few hundred SQL queries and e few hundred ms of loading time.

    This is a lifesaver for us, so thanks to everyone here for all the work put in!

  • First commit to issue fork.
  • Pipeline finished with Failed
    9 months ago
    Total: 178s
    #106060
  • Pipeline finished with Failed
    9 months ago
    Total: 201s
    #106067
  • Pipeline finished with Failed
    9 months ago
    Total: 176s
    #106140
  • Pipeline finished with Failed
    9 months ago
    Total: 562s
    #106194
  • πŸ‡ΊπŸ‡ΈUnited States capysara

    I rerolled patch #172 and moved it into a MR, but it still needs work to get all the tests to pass.

    I'm hiding the patches so that future fixes can continue in the MR.

  • πŸ‡³πŸ‡±Netherlands dennisdk

    Rerolled the patch to 10.3, since the current changes are aimed toward 11.x

  • πŸ‡³πŸ‡±Netherlands dennisdk
  • πŸ‡³πŸ‡±Netherlands basvredeling Amsterdam

    Rerolled patch for 10.3.x

Production build 0.71.5 2024