Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Unlike some other functions in taxonomy module, the database results for taxonomy_get_parents and taxonomy_get_children don't get cached, which in some cases results in requesting the same information multiple times on a page load.
Attached patch adds a static variable that holds the results for these functions and returns the stored value when available.
Comment | File | Size | Author |
---|---|---|---|
#12 | taxonomy_static.patch | 3.22 KB | catch |
#10 | taxonomy_static.patch | 3.22 KB | catch |
#2 | drupal_taxonomy_static_1.patch | 3.13 KB | moonray |
drupal_taxonomy_static.patch | 2.92 KB | moonray | |
Comments
Comment #1
drummThe static variables should be named for what they store, instead of $cache.
This should go into 6.x andthen be backported if necessary.
Comment #2
moonray CreditAttribution: moonray commentedUpdated the patch with changed static variable names. Also added measures to prevent corruption of the cached objects.
Comment #3
zoo33 CreditAttribution: zoo33 commentedI tested a cached version of taxonomy_get_parents on Drupal 5 and Ubercart's catalog block benefits a lot from it. I'm sure similar taxonomy based menu structures will to. I agree that an optimization such as this one would be worth backporting to 5.x.
However, I not sure why the static variable needs protection?
Comment #4
Dries CreditAttribution: Dries commentedThe problem with this patch is that it also can break the API; when you do 'get - alter - save - get' you'll get an old copy of the data, not the updated version. This might be a little tricky ...
Comment #5
zoo33 CreditAttribution: zoo33 commentedMaybe a $cachable argument would do it?
Comment #6
RobRoy CreditAttribution: RobRoy commentedPlease see my taxonomy_get_tree() cache patch at http://drupal.org/node/106015. Maybe that would help. At least they both should use the same implementation. I removed the static var there but may need to bring it back so we only pull from the cache once per page request.
Comment #7
catchStill applies but marking as needs work per Dries.
Comment #8
catchBumping to 7.
Comment #9
catchtagging.
Comment #10
catchThis is still valid, fresh patch.
Comment #11
brianV CreditAttribution: brianV commentedLooks good from an 'the code works' perspective, however, the anal part of me noted that the logic is reversed between instances. For taxonomy_get_children(), I would put the return of the cached $children variable in the 'if', and generate the value in the 'else' to be consistent with the other function.
10 days to code freeze. Better review yourself.
Comment #12
catchYeah I thought about that when writing it, but couldn't decide which was best, sometimes it's better to just pick one though :p here it is as requested.
Comment #13
brianV CreditAttribution: brianV commentedYay - looks great!
Comment #14
webchickHm. It's not clear to me in my current, bleary-eyed state whether the most recent patch addresses Dries's concern at #4. Could someone enlighten me?
Comment #15
catchThat's covered by drupal_static() and taxonomy_static_reset().
Comment #16
Dries CreditAttribution: Dries commentedComitted to CVS HEAD. Thanks.