#4543 closed Enhancement (fixed)
More aggressive entity caching
| Reported by: | ewinslow | Owned by: | ewinslow |
|---|---|---|---|
| Priority: | high | Milestone: | Elgg 1.8.9 |
| Component: | Core | Version: | 1.7 |
| Severity: | major | Keywords: | |
| Cc: | brett@… | Difficulty: | moderate |
Description
$entities = elgg_get_entities(array(...)); // complicated db call get_entity($entities[0]->guid); //hits db again! get_entity($entities[0]->guid); //hits db again!
The above situation is common and pointless. We already loaded the entity, we should just be able to hit a local cache (i.e. it should not require a shared memory cache like memcache).
Once we have this optimization in place for elgg_get_entities, we can (ab)use it as the basis for other optimizations. E.g. currently when you are displaying the river, it's 1 query to get all the relevant river entries, and then 1 more query per owner, and object, plus another for each set of comments, plus another for each comment's owner, etc.
Instead, in elgg_get_river, we can preload all the entities referenced from the results by looping over them, pulling out owner_guid and object_guid into an array, and then doing elgg_get_entities(array('guids' => $guids)); Now $item->getOwnerEntity() and $item->getObjectEntity() will just hit the cache.
Since this is invisible to the API, we can include it in a bugfix release, so I'm slating for 1.8.x.
Change History (6)
comment:1 Changed 12 months ago by ewinslow
- Owner set to ewinslow
- Status changed from new to assigned
comment:2 Changed 12 months ago by mrclay
Yes! I had an idea like the but was worried about the possibilities of having multiple instances of the same entity pointing to different objects. E.g. Using the constructor will always produce a distinct object. The answer to this is, that's already possible. It might be worth considering having all entity construction done via a public static method (making constructor private), which would mean the cache can always be checked and identical GUIDs always point to the same object (unless low on memory). I wonder if the patch could be a bit more clever instead of a static limit on the cache size. Even as is, this is awesome.
comment:3 Changed 12 months ago by mrclay
Just sent Evan a PR with some fixes to the patch: https://github.com/ewinslow/Elgg/pull/1
comment:4 Changed 11 months ago by mrclay
Latest PR for Evan's branch https://github.com/ewinslow/Elgg/pull/2
Adds missing invalidation points and caches during elgg_get_entities. Can get in 1.8.6?
comment:5 Changed 9 months ago by Evan Winslow
- Resolution set to fixed
- Status changed from assigned to closed
Fixes #4543: Cache entities fetched with get_entity and elgg_get_entities
Changeset: 100d7181bcc9814078748272b0df182c95631bca
comment:6 Changed 9 months ago by mrclay
- Milestone changed from Elgg 1.8.x to Elgg 1.8.9

https://github.com/Elgg/Elgg/pull/241