This is an API function but if you use it you risk duplicating queries + processing. This patch adds static caching.

 function taxonomy_get_vocabularies($type = NULL) {
+  static $vocabularies_cache = array();
+  static $vocabularies_null;
+
   if ($type) {
+    if (is_array($vocabularies_cache[$type])) {
+      return $vocabularies_cache[$type];
+    }
     $result = db_query(db_rewrite_sql("SELECT v.vid, v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $type);
   }
   else {
+    if (is_array($vocabularies_null)) {
+      return $vocabularies_null;
+    }
     $result = db_query(db_rewrite_sql('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid ORDER BY v.weight, v.name', 'v', 'vid'));
   }
 
@@ -695,7 +704,15 @@ function taxonomy_get_vocabularies($type
     $vocabularies[$voc->vid] = $voc;
   }
 
+  if ($type) {
+    $vocabularies_cache[$type] = $vocabularies;
+  }
+  else {
+    $vocabularies_null = $vocabularies;
+  }
+
   return $vocabularies;
+
 }

Comments

bennybobw’s picture

StatusFileSize
new1.47 KB

I looked through this patch and everything looks good. I agree this should be cached. The only thing I wasn't sure of was the variable name $vocabularies_null, which as I understand it, gets set if a type isn't specified.
What about $vocabularies_all or something like that? Maybe someone else with a weightier opinion can give an opinion and change the patch code status.
Attaching a patch rolled against latest HEAD without offsets.

catch’s picture

patch still applies with offset...

moshe weitzman’s picture

Status: Needs review » Postponed (maintainer needs more info)

Is there a or two modules out thee that calls this function repeatedly? Just want some justification for the cache.

robertdouglass’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.39 KB
grep -R taxonomy_get_vocabularies . | wc -l
     244

that's on the D5 contrib repository. Rerolled patch to apply without offsets.

catch’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs review » Needs work

Still valid in D7.

We should go the whole hog and change this to taxonomy_vocabulary_load_multiple() so that hook_taxonomy_vocabulary_load() works with it and we can share the static between taxonomy_get_vocabulary() too.

catch’s picture

Status: Needs work » Closed (duplicate)

taxonomy_vocabulary_load_multiple() is in.

catch’s picture

Version: 7.x-dev » 6.x-dev
Status: Closed (duplicate) » Needs review

This no longer applies to Drupal 7, but it's still valid for D6.

Status: Needs review » Needs work

The last submitted patch, taxonomy_get_vocabularies_cache2.patch, failed testing.

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB

Here's a re-roll that also turns taxonomy_vocabulary_load() into a wrapper around taxonomy_get_vocabularies() - this means more vocabularies loaded if you were to only try to load one, but that is not often the case.

catch’s picture

StatusFileSize
new2.15 KB

Fixed a notice in taxonomy_get_vocabularies().

mr.j’s picture

Status: Needs review » Reviewed & tested by the community

Patch is good. It saves over 150 queries on loading the home page of a site I am working on.

However I don't think its a good idea to change taxonomy_vocabulary_load() because it has a reset flag. Assuming it is used somewhere, every time you call the function with $reset = TRUE, it will just grab the cached taxonomies from taxonomy_get_vocabularies().

xano’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

This is now done by the entity API.

xano’s picture

Status: Closed (won't fix) » Reviewed & tested by the community

Oops. Didn't see this was D6.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: taxonomy-163018.patch, failed testing.

xano’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community

Status: Needs work » Needs review

Xano queued 10: taxonomy-163018.patch for re-testing.

djbobbydrake’s picture

Issue summary: View changes

I used this patch in our environment, and it appears that patching the taxonomy_vocabulary_load() function breaks two simpletests: PathautoTaxonomyTokenTestCase and TokenTaxonomyTestCase. It seems like we should pull the patch to taxonomy_vocabulary_load(), but leave the patch to taxonomy_get_vocabularies().

The setup method for TokenTaxonomyTestCase has taxonomy_vocabulary_load(0, TRUE), which return different values b/n the patched vs non-patched taxonomy_vocabulary_load() versions.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.