I'm caching our hefty menu output with the function show below, as each menu item loads two views (menu popups with sub-taxonomy data as a viewfield). This rids us of about 800 extra queries per pageload since Views Argument Caching doesn't work properly, and static caching seemed liked the easiest way to work around the problem. Also the data doesn't need to be that fresh.

Now the issue is, we want the data to be cleared whenever cron runs -- and I'm setting the expire timestamp to be +1 hour from the moment it gets cached, but when checking custom_data CID from cache table the expire timestamps shows a 0 (never), instead of the now + hour. Any ideas?

 * Check  if we already have main navigation in cache, if not, render it
function mymodule_render_main_menu() {
  $navigation = &drupal_static(__FUNCTION__);

  if ( !isset($navigation) ) {
    // Check if we have cached data
    if ( $cache = cache_get('custom_data') ) {
      dsm("Nav from cache");
      $navigation = $cache->data;
    // Else render the main navigation
    else {
      // Get main menu block
      $navigation = menu_block_block_view('1');
      $expire = time() + 3600;

      // Set it's caching to 60min
      $navigation['#cache'] = array(
        'cid' => 'custom_data',
        'bin' => 'cache',
        'expire' => $expire

      // Render the menu
      $navigation = render($navigation['content']);

      // Push rendered data to cache
      cache_set('custom_data', $navigation, 'cache');

  return $navigation;

Worked using cache_set('custom_data', $navigation, 'cache', $expire);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.