--- 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("<div class=\"form-item\"><label>Further configuration:</label><p>The Block Cache module acts as a wrapper around the original block. Please visit <a href=\"%url\">the original block's configuration page</a> to change its configurations.</p></div>", array('%url' => url('admin/block/configure/'. $r->module .'/'. $r->mod_delta))),

@@ -56,14 +62,21 @@
       }

       return $form;

     case 'save':

+      variable_set('bc_cron_'. $delta, trim($edit['bc_cron']));

       variable_set('bc_type_'. $delta, $edit['bc_type']);

       variable_set('bc_life_'. $delta, trim($edit['bc_life']));

       if ($edit['bc_refresh']) {

         blockcache_refresh($delta);

       }

       break;

-    case 'view':

-      return blockcache_block_view($delta);

+

+   case 'view':

+      if (variable_get('bc_cron_'. $delta, '')) {

+	     $block = blockcache_block_show($delta);  	

+      } else {

+	     $block = blockcache_block_view($delta);  	

+      }

+	  return $block;

   }

 }

 

@@ -220,4 +233,65 @@
  */

 function blockcache_version(){

   return str_replace(array('$RCSf'.'ile:', ',v', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile: blockcache.module,v $ version: <b>$Revision: 1.1.2.4 $</b>, $Date: 2006/10/06 21:19:03 $</p>');

-}
\ 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: <strong>$r->module</strong> delta: <strong>$r->mod_delta</strong> subject: <strong>{$block[subject]}</strong> 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);

+  }	

+}

