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' => array(
'info' => t('Display flash game for auth. users'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_winner' => array(
'info' => t('Show the winner of the last week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_leader' => array(
'info' => t('Show the leader of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_top' => array(
'info' => t('Show the top 10 of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
);
}
function pref_block_view($block_name = '') {
global $user;
$viewer_id = $user->uid;
if ($block_name == 'pref_main') {
.... # may not be cached
return array(
'subject' => '',
'content' => '...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 > 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++ > 0 ? '' : ' »');
}
$theme_args = array('items' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);
return array(
'subject' => 'Семерка лучших',
'content' => $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)?