Skip to content

Instantly share code, notes, and snippets.

@niksmac
Created December 26, 2014 08:15
Show Gist options
  • Select an option

  • Save niksmac/6c7ff6e33dca9948d56e to your computer and use it in GitHub Desktop.

Select an option

Save niksmac/6c7ff6e33dca9948d56e to your computer and use it in GitHub Desktop.
Drupal Gotcha : Cache_get() Returns Expired Items
cache_get() returns $cache objects even if the cached item is stale (expired)
function custom_fn_get_data() {
// Return the cached data
$cache = cache_get('custom:fn_get_data');
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:fn_get_data', $data, 'cache', $expire);
}
else {
$data = $cache->data;
}
return $data;
}
You can make it better by this way
if (!$cache or time() > $cache->expire) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment