Drupal Gotchya: Cache_get() Returns Expired Items

1 Jan 0001 One-minute read

cache_get() returns $cache objects even if the cached item is stale (expired). The cached data will not be rebuilt every hour in the following example:

<?php
/**
* Builds complicated data for the monkey grip.
*/
function custom_monkey_grip_data() {
 
// Return the cached data
 
$cache = cache_get('custom:monkey_grip');
  if (!
$cache) {
   
// Some expensive processing to build the data.
   
$data = complicated_recursion_and_loops_on_lots_of_data();
   
   
// Cache the data and rebuild it every hour
   
$expire = time() + (60 * 60);
   
cache_set('custom:monkey_grip', $data, 'cache', $expire);
  }
  else {
   
$data = $cache->data;
  }
  return
$data;
}
?>

read more