Need help with Block Caching I run a flash card game embedded as a block in a Drupal 7 site.

Below that main block I display 3 further blocks for the winners of the previous and current week and top 7 best players:



And these 3 blocks are really dragging the performance of my web site (loading page is taking 5-10 seconds more than without them). I'd prefer them to be cached every 15-30 minutes because the weekly top 7 doesn't change often.

How could I do that - without jeopardizing my main block with the game? I.e. the main block contains flash vars specific to each user (his authentication hash) - this one block may not cached and served to any other user.

Below is my code:

pref.info -

name = Pref
description = Pref module
package = Pref
core = 7.x
files[] = pref.module


pref.module -

function pref_block_info() {
  return array(
    'pref_main' =&gt; array(
      'info' =&gt; t('Display flash game for auth. users'),
      'cache' =&gt; DRUPAL_NO_CACHE,
    ),
    'pref_winner' =&gt; array(
      'info' =&gt; t('Show the winner of the last week.'),
      'cache' =&gt; DRUPAL_NO_CACHE,
    ),
    'pref_leader' =&gt; array(
      'info' =&gt; t('Show the leader of the current week.'),
      'cache' =&gt; DRUPAL_NO_CACHE,
    ),
    'pref_top' =&gt; array(
      'info' =&gt; t('Show the top 10 of the current week.'),
      'cache' =&gt; DRUPAL_NO_CACHE,
    ),
  );
}

function pref_block_view($block_name = '') {
  global $user;
  $viewer_id = $user-&gt;uid;

  if ($block_name == 'pref_main') {
      .... # may not be cached
      return array(
        'subject' =&gt; '',
        'content' =&gt; '...here I return the .swf file + user-specific hash...');
  } else if ($block_name == 'pref_winner') {
      .... # should be cached
  } else if ($block_name == 'pref_leader') {
      .... # should be cached
  } else if ($block_name == 'pref_top') {
      .... # should be cached
    $result = db_query("
select u.id,
        u.first_name,
        u.female,
        u.city,
        m.money,
        u.login &gt; u.logout as online
from pref_users u, pref_money m where
        m.yw=to_char(current_timestamp, 'IYYY-IW') and
        u.id=m.id
order by m.money desc
limit 7
");
    $i = 0;
    $list = array();
    foreach ($result as $record) {
      $list[] = user_link($record) . ($i++ &gt; 0 ? '' : '&amp;nbsp;&amp;raquo;');
    }
    $theme_args = array('items' =&gt; $list, 'type' =&gt; 'ol');
    $content = theme('item_list', $theme_args);
    return array(
      'subject' =&gt; 'Семерка лучших',
      'content' =&gt; $content,
    );
  }
}


For module caching I probably should enable it in the /admin/config/development/performance and then use some other constant instead of the DRUPAL_NO_CACHE (but which one to take from the includes/common.inc)?
A simple solution would be to cache the output instead of caching the block. This would be done in your hook_block_view implementation.

It could look like this:

...
$cache = cache_get('pref_pref_leader', 'cache_block');
// Return cache if possible.
if (!empty($cache) &amp;&amp; isset($cache-&gt;data) &amp;&amp; !empty($cache-&gt;data)) {
  return $cache-&gt;data;
}

// no cache, generate the output.
...
$content = ...;
$block = array(
  'subject' =&gt; 'Семерка лучших',
  'content' =&gt; $content,
);
// Cache for 30 mins.
cache_set('pref_pref_leader', $block 'cache_block', time() + 1800);
return $block;
...


See documentation for


cache_set
cache_get