Index: chessboard.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/chessboard/chessboard.module,v
retrieving revision 1.72
diff -u -p -r1.72 chessboard.module
--- chessboard.module	15 Nov 2010 20:30:32 -0000	1.72
+++ chessboard.module	21 Nov 2010 15:11:29 -0000
@@ -23,9 +23,32 @@ function chessboard_help($path, $arg) {
  * Implementation of hook_init().
  */
 function chessboard_init() {
+  $formats = array();
   $result = db_query('SELECT * FROM {filter_formats}');
   while ($format = db_fetch_object($result)) {
-    _chessboard_filter_add_structures($format);
+    $formats[$format->format] = $format;
+  }
+
+  // Build list cache IDs.
+  $cids = _chessboard_filter_page_structures_list_cids($formats);
+
+  // Retrieve lists.
+  $cache = _chessboard_cache_get_multiple($cids, 'cache_filter');
+
+  // Build structure cache IDs.
+  $cids = array();
+  foreach($cache as $item) {
+    $cids += array_keys($item->data);
+  }
+
+  // Retrieve structures.
+  $cache = _chessboard_cache_get_multiple($cids, 'cache_filter');
+
+  // Process structures.
+  foreach ($cache as $item) {
+    if (isset($item->data['#attached'])) {
+      chessboard_process_attached($item->data);
+    }
   }
 }
 
@@ -188,6 +211,29 @@ function chessboard_render_elements(&$el
 }
 
 /**
+ * Return data from the persistent cache when given an array of cache IDs.
+ *
+ * @param $cids
+ *   An array of cache IDs for the data to retrieve. This is passed by
+ *   reference, and will have the IDs successfully returned from cache removed.
+ * @param $bin
+ *   The cache bin where the data is stored.
+ * @return
+ *   An array of the items successfully returned from cache indexed by cid.
+ */
+function _chessboard_cache_get_multiple(&$cids, $bin = 'cache') {
+  $cache = array();
+  foreach ($cids as $cid) {
+    $item = cache_get($cid, $bin);
+    if ($item) {
+      $cache[$item->cid] = $item;
+    }
+  }
+  $cids = array_diff($cids, array_keys($cache));
+  return $cache;
+}
+
+/**
  * Chessboard filter. Provides filtering of input into chessboard diagrams.
  *
  * @param $filter
@@ -324,18 +370,14 @@ function _chessboard_filter_callback($ma
   return $output;
 }
 
-function _chessboard_filter_add_structures($format) {
-  if (filter_format_allowcache($format->format)) {
-    $list = cache_get($format->format . ':chessboard-filter-attached-cids', 'cache_filter');
-    if (isset($list->data)) {
-      foreach ($list->data as $cid => $status) {
-        $cache = cache_get($cid, 'cache_filter');
-        if (isset($cache->data['#attached'])) {
-          chessboard_process_attached($cache->data);
-        }
-      }
+function _chessboard_filter_page_structures_list_cids($formats) {
+  $cids = array();
+  foreach ($formats as $format) {
+    if ($format->cache) {
+      $cids[] = $format->format . ':chessboard-filter-attached-cids';
     }
   }
+  return $cids;
 }
 
 function chessboard_formatter_view($items) {
