This module provides a simple API for enabling caching for menu callbacks. This is useful if you have a custom module that defines its own page callbacks in hook_menu(). Reverse proxy caches like Varnish can provide caching for anonymous users, but sometimes you need to cache certain pages for all users. This module, when used in conjunction with the Memcache API and Integration module can be a simple way to increase performance.

You are not limited to your own modules, however. You can add menu callback caching to menu callbacks defined in other modules by implementing hook_menu_alter().

The following example modifies the path "node/%node" to cache page nodes per user for an hour.

<?php
/**
*  Implementation of hook_menu_alter().
*/
function my_module_menu_alter(&$items) {
 
$items['node/%node']['cache'] = MENU_CALLBACK_CACHE_PER_USER;
 
$items['node/%node']['cache max age'] = 60 * 60;
 
$items['node/%node']['cache key callback'] = 'my_module_cache_key_callback';
}
/**
*  Cache key callback for path node/%node.
*/
function my_module_cache_key_callback($node) {
 
// Set the cache key if this node is of type 'page'.
 
if ($node->type == 'page') {
    return
__FUNCTION__ . '-' . $node->nid;
  }
 
// Don't cache nodes that aren't type 'page'.
 
else {
    throw new
DoNotCacheException();
  }
}
?>

This next example illustrates how you can add caching to your module's custom menu items.

<?php
/**
* Implementation of hook_menu().
*/
function my_module_menu() {
 
$items = array();
 
$items['foo'] = array(
   
'title' => 'Foo',
   
'description' => 'Foooooooooooooo.',
   
'page callback' => 'my_module_foo',
   
'access arguments' => array('access content'),
   
'type' => MENU_NORMAL_ITEM,
   
'cache' => MENU_CALLBACK_CACHE_PER_USER,
  );
  return
$items;
}
/**
* Page callback for 'foo/'
*/
function my_module_foo() {
  return
"This is some content, yo.";
}
?>

Enabling this module provides you with 3 new keys that you can define for your menu items in hook_menu:

cache
One of the 3 following values (similar to the block module):
  • MENU_CALLBACK_CACHE_GLOBAL - Cache this menu callback globally for all users.
  • MENU_CALLBACK_CACHE_PER_ROLE - Cache this menu callback per user role.
  • MENU_CALLBACK_CACHE_PER_USER - Cache this menu callback per user.
cache max age (optional)
How long to keep the item in the cache before it becomes invalidated. If not specified, the default of 5 minutes is used.
cache key callback (optional)
A callback that will generate the key to use for the cached callback. This is useful as it lets you define the key in a way that you can force invalidation early if you want to. You can also throw a DoNotCacheException within the callback to prevent the menu callback from caching. The cache key callback takes the exact same parameters as the menu item's page callback. If no cache key callback is specified, the cache key is automatically generated by using the page callback and hashing its parameters.

Project Information

Downloads