How can I ensure this block is properly cached? I have been trying to get the some book menus displayed by nice menu, and after asking some questions on stackoverflow 1 and stackoverflow 2 settled on this code. My ultimate intention is to turn it into a module that creates blocks for different sets of books.

I am not sure whether this block will be cached so the queries are not computed every time. What additional code may be required to ensure this?

&lt;?php
$master_menu = '';
unset($four_books);
  $four_books_list = "91,323,47,149";
  $four_books = &amp;drupal_static(__FUNCTION__);
  if (!isset($four_books)) {
    $four_books = array();
    $nids = db_query("SELECT DISTINCT(bid) FROM {book} where bid in (91,323,47,149)")-&gt;fetchCol();

    if ($nids) {
      $query = db_select('book', 'b', array('fetch' =&gt; PDO::FETCH_ASSOC));
      $query-&gt;join('node', 'n', 'b.nid = n.nid');
      $query-&gt;join('menu_links', 'ml', 'b.mlid = ml.mlid');
      $query-&gt;addField('n', 'type', 'type');
      $query-&gt;addField('n', 'title', 'title');
      $query-&gt;fields('b');
      $query-&gt;fields('ml');
      $query-&gt;condition('n.nid', $nids, 'IN');
      $query-&gt;condition('n.status', 1);
      $query-&gt;orderBy('ml.weight');
      $query-&gt;orderBy('ml.link_title');
      $query-&gt;addTag('node_access');
      $result2 = $query-&gt;execute();
      foreach ($result2 as $link) {
        $link['href'] = $link['link_path'];
        $link['options'] = unserialize($link['options']);
        $four_books[$link['bid']] = $link;
      }
    }
  }

foreach($four_books as $book_nid=&gt;$book) {
  $menu = theme('nice_menus', array('id' =&gt; $book['mlid'], 'direction' =&gt; 'right', 'depth' =&gt; -1, 'menu_name' =&gt; $book['menu_name'], 'menu' =&gt; NULL));
  $master_menu .= $menu['content'];
}
print $master_menu;
?&gt;
Why not cache it yourself? The Cache API in Drupal 7 is fairly easy to implement and it sounds as if your problem might warrant it. I had this issue with a very expensive function that was used to generate relatively static data and did a quick cache implementation to avoid having to constantly rebuild the query. 

Let's say your existing function is something like this

function myfunction() { 

   $output = (my calculations and logic);
   return $output;

}


A rough way to wrap this in using cache layer would be 

function myfunction($reset = FALSE) { 

  static $output;

  //some unique idenitifier for your specific record, ex. content type+nid
  $cache_key = (your logic here)

  if (!isset($output) || $reset) {

      // check whether or not output has already been cached and if so, use it
      if (!$reset &amp;&amp; ($cache = cache_get($cache_key)) &amp;&amp; !empty($cache-&gt;data)) {

        $output = unserialize($cache-&gt;data);

  } else {

        $output = (my calculations and logic);

        cache_set($cache_key, serialize($output), 'cache', 3600);

        return $output;

    }
}


The cache_set function is the key to this, here's what each of the parameters is doing: 


$cache_key - your unique identifier for you content
$output - the output from your function
'cache' - the specific cache bin you want to use. Most likely, you'll want to use the  default 'cache' bin
3600 - the time in seconds that the item should live before being reset. In this case, this content is set to live for one hour. You can also set it to "CACHE_PERMANENT" to never needing reset or "CACHE_TEMPORARY" to reset the next time the general cache is clear.