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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drumm’s picture

Version: 5.x-dev » 6.x-dev
Category: feature » task
Status: Needs review » Needs work

The static variables should be named for what they store, instead of $cache.

This should go into 6.x andthen be backported if necessary.

moonray’s picture

Status: Needs work » Needs review
FileSize
3.13 KB

Updated the patch with changed static variable names. Also added measures to prevent corruption of the cached objects.

zoo33’s picture

I 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?

Dries’s picture

The 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 ...

zoo33’s picture

Maybe a $cachable argument would do it?

RobRoy’s picture

Please 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.

catch’s picture

Status: Needs review » Needs work

Still applies but marking as needs work per Dries.

catch’s picture

Version: 6.x-dev » 7.x-dev

Bumping to 7.

catch’s picture

Issue tags: +Performance

tagging.

catch’s picture

Title: caching for taxonomy_get_parents and taxonomy_get_children » Static cache for taxonomy_get_parents and taxonomy_get_children
Status: Needs work » Needs review
FileSize
3.22 KB

This is still valid, fresh patch.

brianV’s picture

Status: Needs review » Needs work
+++ modules/taxonomy/taxonomy.module	2 Jan 2010 13:50:32 -0000
@@ -610,19 +612,25 @@ function taxonomy_vocabulary_get_names()
+    $tids = &drupal_static(__FUNCTION__, array());
+    if (isset($tids[$key][$tid])) {
+      $parents = $tids[$key][$tid];
+    }
+    else {
+      // Long code to set value of $parents.
+    }
+++ modules/taxonomy/taxonomy.module	2 Jan 2010 13:50:32 -0000
@@ -660,23 +668,30 @@ function taxonomy_get_parents_all($tid) 
+    $tids = &drupal_static(__FUNCTION__, array());
+  if (!isset($tids[$vid][$tid])) {
+    // Long code to set value of $children.
+  }
+  else {
+    $children = $tids[$vid][$tid];
   }

Looks 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.

catch’s picture

Status: Needs work » Needs review
FileSize
3.22 KB

Yeah 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.

brianV’s picture

Status: Needs review » Reviewed & tested by the community

Yay - looks great!

webchick’s picture

Hm. 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?

catch’s picture

That's covered by drupal_static() and taxonomy_static_reset().

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Comitted to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)
Issue tags: -Performance

Automatically closed -- issue fixed for 2 weeks with no activity.