--- blockcache.module 2006-10-06 17:19:03.000000000 -0400 +++ blockcache.module 2007-12-23 17:37:24.000000000 -0500 @@ -38,6 +38,12 @@ '#default_value' => variable_get('bc_life_'. $delta, ''), '#description' => t('Lifetime (in seconds) of this item in the cache. How often is the content of this block refreshed? Leave blank to refresh automatically when new content is created.'), ); + $form['bc_cron'] = array( + '#type' => 'checkbox', + '#title' => t('Refresh on Cron only'), + '#default_value' => variable_get('bc_cron_'. $delta, ''), + '#description' => t('Should this item be refreshed only on Cron. It will be stored in the permanent cache.'), + ); $r = db_fetch_object(db_query('SELECT * FROM {bc_blocks} WHERE my_delta = %d', $delta)); $form['bc_origlink'] = array( '#value' => t("
The Block Cache module acts as a wrapper around the original block. Please visit the original block's configuration page to change its configurations.
$RCSfile: blockcache.module,v $ version: $Revision: 1.1.2.4 $, $Date: 2006/10/06 21:19:03 $
'); -} \ No newline at end of file +} + +/** + * Cache this block. Called from multiple places + */ +function blockcache_block_cache($cache_name, $delta, $cron = FALSE) { +// create block + $r = db_fetch_object(db_query('SELECT * FROM {bc_blocks} WHERE my_delta = %d', $delta)); + $block = module_invoke($r->module, 'block', 'view', $r->mod_delta); + $cache = serialize($block); + // calculate expiration and create cache + $expire = is_numeric(variable_get('bc_life_'.$delta, '')) ? time() + variable_get('bc_life_'.$delta, '') : CACHE_TEMPORARY; + if ($cron) { + cache_set($cache_name, $cache, CACHE_PERMANENT) + cache_set($cache_name.'_valid', NULL, $expire); + } + else { + cache_set($cache_name, $cache, $expire); + } + +// display debug message + if (variable_get('bc_debug', 0) && user_access('administer nodes')) { + drupal_set_message("Refreshed cache for module: $r->module delta: $r->mod_delta subject: {$block[subject]} as $cache_name"); + } + return $block; +} + +/** + * View a cached block using hook_block(). + * + * Simply shows the block's current contents without regard to status + */ +function blockcache_block_show($delta) { + $cache_name = _blockcache_get_name($delta); + $cached = cache_get($cache_name); + + if ($cached && $cached->expire == CACHE_PERMANENT ) { + $block = unserialize($cached->data); + } else { + $block = blockcache_block_cache($cache_name, $delta, TRUE); + } + return $block; +} + +/** + * Cron retrieves blocks that need to be updated + */ +function blockcache_cron() { + // join cache table on itself + $sql = "SELECT cperm.cid AS cid FROM cache AS cperm "; + $sql .= "LEFT JOIN cache AS cvalid ON cvalid.cid = CONCAT(cperm.cid, '_valid' ) "; + $sql .= "WHERE cperm.cid LIKE 'bc%' AND cperm.expire = 0 AND cvalid.cid IS NULL"; + $result = db_query($sql); + while($r = db_fetch_object($result)){ + // get cid and delta + $cache_name = $r->cid; + preg_match ("/bc_(.*)_/i", $cache_name, $match); + $delta=$match[1]; + // re-generate the block + $block = blockcache_block_cache($cache_name, $delta, TRUE); + } +}