Index: sections.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/sections/sections.module,v
retrieving revision 1.21.2.27
diff -u -r1.21.2.27 sections.module
--- sections.module	20 Dec 2009 21:23:05 -0000	1.21.2.27
+++ sections.module	8 Aug 2010 10:49:56 -0000
@@ -229,20 +229,19 @@
  *
  * This API is a function that lets you find out about settings.
  *
- * @param
- *  Optional $setting a string containing the section you what to test against.
- *
+ * @param $section
+ *   Optional $setting a string containing the section you what to test against.
  * @return
- *   Depends on the parameter.
- *   If you do not give $section, it will return the section object, if found.
- *   If you give $section, it will return TRUE if you are in that section
- *   Otherwise it will return FALSE
+ *   Depends on the $section parameter:
+ *   - If you do not provide $section, it will return the section object, if found.
+ *   - If you provide $section, it will return TRUE if you are in that section.
+ *   - Otherwise it will return FALSE.
  */
 function _sections_in_section($section = NULL) {
   global $user;
 
   if (is_string($section)) {
-    // Caller wants to know if shes in the section she provided.
+    // Caller wants to know if she's in the section she provided.
     if ($section == _sections_in_section()) {
       return TRUE;
     }
@@ -250,8 +249,8 @@
   else {
     // Caller wants to know in which section she is.
     $rids = array_keys($user->roles);
-    $res = db_query(db_rewrite_sql('SELECT DISTINCT s.* FROM {sections_data} s LEFT JOIN {sections_roles} r ON s.sid = r.sid WHERE s.status = 1 AND (r.rid IN ('. db_placeholders($rids) .') OR r.rid IS NULL) ORDER BY s.weight', 's', 'sid'), $rids);
-    while ($row = db_fetch_object($res)) {
+    $res = _sections_cache_get($rids);
+    foreach ($res as $sid => $row) {
       if ($row->visibility < 2) {
         $path = drupal_get_path_alias($_GET['q']);
         // Compare with the internal and path alias (if any).
@@ -280,6 +279,36 @@
 }
 
 /**
+ * An internal caching function that statically caches the sections data.
+ *
+ * @param $rids
+ *   Array of role id's, the current user belongs too.
+ * @param $reset
+ *   Manually resets the sections cache for the current user roles.
+ * @return
+ *   Cached sections row data.
+ */
+function _sections_cache_get($rids, $reset = FALSE) {
+  static $sections = array();
+
+  // Create a key to cache the sections data by user roles.
+  $rids_key = implode('_', $rids);
+
+  if (!isset($sections[$rids_key]) || $reset) {
+    $sections[$rids_key] = array();
+
+    // Collect sections data from database and statically cache the data by rids.
+    $result = db_query(db_rewrite_sql('SELECT DISTINCT s.* FROM {sections_data} s LEFT JOIN {sections_roles} r ON s.sid = r.sid WHERE s.status = 1 AND (r.rid IN ('. db_placeholders($rids) .') OR r.rid IS NULL) ORDER BY s.weight', 's', 'sid'), $rids);
+    while ($row = db_fetch_object($result)) {
+      $sections[$rids_key][$row->sid] = $row;
+    }
+  }
+
+  // Return cached sections data.
+  return $sections[$rids_key];
+}
+
+/**
  * Implementation of hook_preprocess().
  */
 function sections_preprocess(&$variables, $hook) {
