taxonomy_get_vocabularies needs static caching

robertDouglass - July 29, 2007 - 20:57
Project:Drupal
Version:7.x-dev
Component:taxonomy.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:duplicate
Description

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

<?php
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;
+
}
?>

AttachmentSizeStatusTest resultOperations
taxonomy_get_vocabularies_cache.patch1.48 KBIgnoredNoneNone

#1

bennybobw - August 1, 2007 - 01:21

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.

AttachmentSizeStatusTest resultOperations
taxonomy_get_vocabularies_cache2.patch1.47 KBIgnoredNoneNone

#2

catch - October 26, 2007 - 21:52

patch still applies with offset...

#3

moshe weitzman - October 30, 2007 - 00:20
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.

#4

robertDouglass - November 9, 2007 - 16:58
Status:postponed (maintainer needs more info)» needs review

grep -R taxonomy_get_vocabularies . | wc -l
     244

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

AttachmentSizeStatusTest resultOperations
taxonomy_get_vocabularies_cache2.patch.txt1.39 KBIgnoredNoneNone

#5

catch - December 28, 2008 - 01:23
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.

#6

catch - April 11, 2009 - 16:30
Status:needs work» duplicate

taxonomy_vocabulary_load_multiple() is in.

 
 

Drupal is a registered trademark of Dries Buytaert.