Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static cache is not used when loading all entities of a type more than once in a single page request #5440

Open
argiepiano opened this issue Jan 5, 2022 · 0 comments
Assignees

Comments

@argiepiano
Copy link

Description of the bug

(This issue is a follow-up to #5439, which I closed to focus more globally on this problem.)

When loading all entities of a specific type more than once in a single page request (e.g. by using entity_load_multiple('node')), Backdrop does not make use the static cache built in DefaultEntityController. Instead, it queries the database every single time.

There are instances when two or more modules need to load all entities of a type, so this bug can result in a big drag on performance, especially if you are dealing with hundreds of entities.

Steps To Reproduce

The following steps will show that the static cache is not being used on the second request to load all entities in a single page request:

  1. Enable Devel
  2. Edit DefaultEntityController::cacheGet within entity.controller.inc by inserting the following code right before return $entities;:
    dpm($this->entityCache, 'Static cache content');
    dpm($entities, 'Entities from cache to be returned');
  1. Go to admin/devel/php and run the following to load all nodes TWICE in a single page request:
entity_load_multiple('node');
entity_load_multiple('node');

(After testing be sure to remove the dpm you added to entity.controller.inc)

Actual behavior

You will see four array outputs for each dpm:
Screen Shot 2022-01-04 at 11 10 59 PM

As you can see above, the static cache contains two nodes in the second call, yet the $entities array to be returned is empty. This is evidence that the entities ARE being cached in the static cache, but they are not being returned.

Expected behavior

The method should return the nodes in the static cache after the second call.

Additional information

Two fixes are needed to allow DefaultEntityController::load to use the static cache when loading all entities of a type:

  1. Fix DefaultEntityController::cacheGet() to return all the cached entities when no $ids ar eprovided, AND
  2. Fix DefaultEntityControler::load() to use a flag to indicate that all entities have been cached in the static cache, so that the database is not queried unnecessarily. This is the approach that Drupal 7 Entity API took (see the flag $this->cacheComplete over there) and the approach that Entity Plus still takes.

I will work on providing a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant